diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 70a75bfc9b..bed09d4623 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -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 diff --git a/pkg/plugin/dns/dns_linux.go b/pkg/plugin/dns/dns_linux.go index a27b000415..bba17d04e8 100644 --- a/pkg/plugin/dns/dns_linux.go +++ b/pkg/plugin/dns/dns_linux.go @@ -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" @@ -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 diff --git a/pkg/plugin/dns/dns_linux_test.go b/pkg/plugin/dns/dns_linux_test.go index 7c84f95518..a93fef6517 100644 --- a/pkg/plugin/dns/dns_linux_test.go +++ b/pkg/plugin/dns/dns_linux_test.go @@ -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 @@ -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 diff --git a/pkg/plugin/linuxutil/netstat_stats_linux.go b/pkg/plugin/linuxutil/netstat_stats_linux.go index 5b71ebf029..9366fc5048 100644 --- a/pkg/plugin/linuxutil/netstat_stats_linux.go +++ b/pkg/plugin/linuxutil/netstat_stats_linux.go @@ -22,6 +22,21 @@ const ( pathNetSnmp = "/proc/net/snmp" ) +var ( + nodeIP = os.Getenv("NODE_IP") + ignoreList = []string{ + "0.0.0.0", + "127.0.0.", + } +) + +func init() { + // Add node IP to the ignore list + if nodeIP != "" { + ignoreList = append(ignoreList, nodeIP) + } +} + type NetstatReader struct { l *log.ZapLogger connStats *ConnectionStats @@ -196,6 +211,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)) @@ -275,9 +293,11 @@ func validateRemoteAddr(addr string) bool { return false } - // ignore localhost addresses. - if strings.Contains(addr, "127.0.0") { - return false + // check if the address is in the ignore list + for _, addressToIgnore := range ignoreList { + if strings.HasPrefix(addr, addressToIgnore) { + return false + } } return true diff --git a/pkg/plugin/linuxutil/netstat_stats_linux_test.go b/pkg/plugin/linuxutil/netstat_stats_linux_test.go index 7491e994a6..20dcbd874c 100644 --- a/pkg/plugin/linuxutil/netstat_stats_linux_test.go +++ b/pkg/plugin/linuxutil/netstat_stats_linux_test.go @@ -5,6 +5,7 @@ package linuxutil import ( "fmt" "net" + "os" "testing" "github.com/cakturk/go-netstat/netstat" @@ -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{ @@ -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, }, } @@ -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) diff --git a/test/e2e/framework/prometheus/prometheus.go b/test/e2e/framework/prometheus/prometheus.go index 0014e2d368..3041d776cf 100644 --- a/test/e2e/framework/prometheus/prometheus.go +++ b/test/e2e/framework/prometheus/prometheus.go @@ -83,6 +83,12 @@ func verifyValidMetricPresent(metricName string, data map[string]*promclient.Met for _, label := range metric.GetLabel() { metricLabels[label.GetName()] = label.GetValue() } + + // if valid metric is empty, then we just need to make sure the metric and value is present + if len(validMetric) == 0 && len(metricLabels) > 0 { + return nil + } + if reflect.DeepEqual(metricLabels, validMetric) { return nil } diff --git a/test/e2e/scenarios/dns/validate-basic-dns-metric.go b/test/e2e/scenarios/dns/validate-basic-dns-metric.go index 6cdc2afed4..efbcd5af2f 100644 --- a/test/e2e/scenarios/dns/validate-basic-dns-metric.go +++ b/test/e2e/scenarios/dns/validate-basic-dns-metric.go @@ -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 { @@ -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 { diff --git a/test/e2e/scenarios/tcp/validate-tcp-connection-remote.go b/test/e2e/scenarios/tcp/validate-tcp-connection-remote.go index 4121d1b5d6..70c1c7fb97 100644 --- a/test/e2e/scenarios/tcp/validate-tcp-connection-remote.go +++ b/test/e2e/scenarios/tcp/validate-tcp-connection-remote.go @@ -21,9 +21,7 @@ type ValidateRetinaTCPConnectionRemoteMetric struct { func (v *ValidateRetinaTCPConnectionRemoteMetric) Run() error { promAddress := fmt.Sprintf("http://localhost:%s/metrics", v.PortForwardedRetinaPort) - validMetrics := []map[string]string{ - {address: "0.0.0.0", port: "0"}, - } + validMetrics := []map[string]string{} for _, metric := range validMetrics { err := prom.CheckMetric(promAddress, tcpConnectionRemoteMetricName, metric) @@ -32,7 +30,7 @@ func (v *ValidateRetinaTCPConnectionRemoteMetric) Run() error { } } - log.Printf("found metrics matching %+v\n", validMetrics) + log.Printf("found metrics matching %+v\n", tcpConnectionRemoteMetricName) return nil }