From 4a34748305dbad31a41d48361f8c8a452c320a80 Mon Sep 17 00:00:00 2001 From: Nikola Grcevski <6207777+grcevski@users.noreply.github.com> Date: Tue, 10 Dec 2024 13:53:35 -0500 Subject: [PATCH] Backport 1441 to 1.9 (#1443) --- pkg/export/prom/prom.go | 6 +++++- pkg/export/prom/prom_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/export/prom/prom.go b/pkg/export/prom/prom.go index f623f97a3..d54f8bd5a 100644 --- a/pkg/export/prom/prom.go +++ b/pkg/export/prom/prom.go @@ -581,9 +581,13 @@ func (r *metricsReporter) otelSpanObserved(span *request.Span) bool { return r.cfg.OTelMetricsEnabled() && !span.Service.ExportsOTelMetrics() } +func (r *metricsReporter) otelSpanFiltered(span *request.Span) bool { + return span.InternalSignal() || span.IgnoreMetrics() +} + // nolint:cyclop func (r *metricsReporter) observe(span *request.Span) { - if span.InternalSignal() { + if r.otelSpanFiltered(span) { return } t := span.Timings() diff --git a/pkg/export/prom/prom_test.go b/pkg/export/prom/prom_test.go index ca8302446..66cda1272 100644 --- a/pkg/export/prom/prom_test.go +++ b/pkg/export/prom/prom_test.go @@ -316,31 +316,45 @@ func TestSpanMetricsDiscarded(t *testing.T) { svcExportTraces := svc.Attrs{} svcExportTraces.SetExportsOTelTraces() + ignoredSpan := request.Span{Service: svcExportTraces, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/v1/traces", RequestStart: 100, End: 200} + ignoredSpan.SetIgnoreMetrics() + tests := []struct { name string span request.Span discarded bool + filtered bool }{ { name: "Foo span is not filtered", span: request.Span{Service: svcNoExport, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/foo", RequestStart: 100, End: 200}, discarded: false, + filtered: false, }, { name: "/v1/metrics span is filtered", span: request.Span{Service: svcExportMetrics, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/v1/metrics", RequestStart: 100, End: 200}, discarded: true, + filtered: false, + }, + { + name: "/v1/traces span is filtered because we ignore this span", + span: ignoredSpan, + discarded: false, + filtered: true, }, { name: "/v1/traces span is not filtered", span: request.Span{Service: svcExportTraces, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/v1/traces", RequestStart: 100, End: 200}, discarded: false, + filtered: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { assert.Equal(t, tt.discarded, !mr.otelSpanObserved(&tt.span), tt.name) + assert.Equal(t, tt.filtered, mr.otelSpanFiltered(&tt.span), tt.name) }) } }