Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(basic-metrics): Remove label from basic dns and filter out IPs from linux util plugins #877

Merged
merged 9 commits into from
Oct 26, 2024
2 changes: 0 additions & 2 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,11 @@ func InitializeMetrics() {
exporter.DefaultRegistry,
utils.DNSRequestCounterName,
dnsRequestCounterDescription,
utils.DNSRequestLabels...,
)
DNSResponseCounter = exporter.CreatePrometheusCounterVecForMetric(
exporter.DefaultRegistry,
utils.DNSResponseCounterName,
dnsResponseCounterDescription,
utils.DNSResponseLabels...,
)

// InfiniBand Metrics
Expand Down
9 changes: 2 additions & 7 deletions pkg/plugin/dns/dns_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"context"
"net"
"os"
"strconv"
"strings"

v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/trace/dns/tracer"
Expand Down Expand Up @@ -96,18 +94,15 @@ func (d *dns) eventHandler(event *types.Event) {
}
d.l.Debug("Event received", zap.Any("event", event))

responses := strings.Join(event.Addresses, ",")

// Update basic metrics. If the event is a request, the we don't need num_response, response, or return_code
// Update basic metrics
if event.Qr == types.DNSPktTypeQuery {
m = metrics.DNSRequestCounter
m.WithLabelValues(event.QType, event.DNSName).Inc()
} else if event.Qr == types.DNSPktTypeResponse {
m = metrics.DNSResponseCounter
m.WithLabelValues(event.Rcode, event.QType, event.DNSName, responses, strconv.Itoa(event.NumAnswers)).Inc()
} else {
return
}
m.WithLabelValues().Inc()

if !d.cfg.EnablePodLevel {
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/dns/dns_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestRequestEventHandler(t *testing.T) {

// Basic metrics.
mockCV := metrics.NewMockCounterVec(ctrl)
mockCV.EXPECT().WithLabelValues(event.QType, event.DNSName).Return(c).Times(1)
mockCV.EXPECT().WithLabelValues().Return(c).Times(1)
before := value(c)
metrics.DNSRequestCounter = mockCV

Expand Down Expand Up @@ -191,7 +191,7 @@ func TestResponseEventHandler(t *testing.T) {
// Basic metrics.
c := prometheus.NewCounter(prometheus.CounterOpts{})
mockCV := metrics.NewMockCounterVec(ctrl)
mockCV.EXPECT().WithLabelValues(event.Rcode, event.QType, event.DNSName, "1.1.1.1,2.2.2.2", "2").Return(c).Times(1)
mockCV.EXPECT().WithLabelValues().Return(c).Times(1)
before := value(c)
metrics.DNSResponseCounter = mockCV

Expand Down
11 changes: 9 additions & 2 deletions pkg/plugin/linuxutil/netstat_stats_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const (
pathNetSnmp = "/proc/net/snmp"
)

var (
nodeIP = os.Getenv("NODE_IP")
)

type NetstatReader struct {
l *log.ZapLogger
connStats *ConnectionStats
Expand Down Expand Up @@ -196,6 +200,9 @@ func (nr *NetstatReader) readSockStats() error {
}
addr := addrPort.Addr().String()
port := strconv.Itoa(int(addrPort.Port()))
if !validateRemoteAddr(addr) {
continue
}
// Check if the remote address is in the new sockStats map
if _, ok := sockStats.socketByRemoteAddr[remoteAddr]; !ok {
nr.l.Debug("Removing remote address from metrics", zap.String("remoteAddr", remoteAddr))
Expand Down Expand Up @@ -275,8 +282,8 @@ func validateRemoteAddr(addr string) bool {
return false
}

// ignore localhost addresses.
if strings.Contains(addr, "127.0.0") {
// ignore localhost addresses and the node's own IP
if strings.HasPrefix(addr, "127.0.0") || addr == nodeIP || addr == "0.0.0.0" {
return false
}

Expand Down
12 changes: 10 additions & 2 deletions pkg/plugin/linuxutil/netstat_stats_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package linuxutil
import (
"fmt"
"net"
"os"
"testing"

"github.com/cakturk/go-netstat/netstat"
Expand Down Expand Up @@ -339,6 +340,9 @@ func TestReadSockStats(t *testing.T) {

// Test IP that belongs to a closed connection being removed from the metrics
func TestReadSockStatsRemoveClosedConnection(t *testing.T) {
// Set os env variable to enable debug logs
os.Setenv("NODE_IP", "10.224.0.6")

_, err := log.SetupZapLogger(log.GetDefaultLogOpts())
require.NoError(t, err)
opts := &NetstatOpts{
Expand All @@ -363,7 +367,7 @@ func TestReadSockStatsRemoveClosedConnection(t *testing.T) {
// Initial value
nr.opts.PrevTCPSockStats = &SocketStats{
socketByRemoteAddr: map[string]int{
"127.0.0.1:80": 1,
"192.168.1.100:80": 1,
},
}

Expand All @@ -372,7 +376,11 @@ func TestReadSockStatsRemoveClosedConnection(t *testing.T) {
ns.EXPECT().UDPSocks(gomock.Any()).Return([]netstat.SockTabEntry{}, nil).Times(1)

// We are expecting the gauge to be called once for this value as it is removed
MockGaugeVec.EXPECT().WithLabelValues("127.0.0.1", "80").Return(testmetric).Times(1)
MockGaugeVec.EXPECT().WithLabelValues("192.168.1.100", "80").Return(testmetric).Times(1)

// We are not expecting the gauge to be called for localhost or node IP
MockGaugeVec.EXPECT().WithLabelValues("127.0.0.1", "80").Return(testmetric).Times(0)
MockGaugeVec.EXPECT().WithLabelValues("10.224.0.6", "80").Return(testmetric).Times(0)

err = nr.readSockStats()
require.NoError(t, err)
Expand Down
13 changes: 2 additions & 11 deletions test/e2e/scenarios/dns/validate-basic-dns-metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ type validateBasicDNSRequestMetrics struct {
func (v *validateBasicDNSRequestMetrics) Run() error {
metricsEndpoint := fmt.Sprintf("http://localhost:%d/metrics", common.RetinaPort)

validBasicDNSRequestMetricLabels := map[string]string{
"query": v.Query,
"query_type": v.QueryType,
}
validBasicDNSRequestMetricLabels := map[string]string{}

err := prom.CheckMetric(metricsEndpoint, dnsBasicRequestCountMetricName, validBasicDNSRequestMetricLabels)
if err != nil {
Expand Down Expand Up @@ -61,13 +58,7 @@ func (v *validateBasicDNSResponseMetrics) Run() error {
v.Response = ""
}

validBasicDNSResponseMetricLabels := map[string]string{
"num_response": v.NumResponse,
"query": v.Query,
"query_type": v.QueryType,
"return_code": v.ReturnCode,
"response": v.Response,
}
validBasicDNSResponseMetricLabels := map[string]string{}

err := prom.CheckMetric(metricsEndpoint, dnsBasicResponseCountMetricName, validBasicDNSResponseMetricLabels)
if err != nil {
Expand Down
Loading