From d97907571448addd11094f61e249c9e31628ee28 Mon Sep 17 00:00:00 2001 From: aishyandapalli Date: Tue, 28 Nov 2023 13:19:39 -0800 Subject: [PATCH] [Spanmetrics] - Add exemplars to events_total metric (#29090) **Description:** Add exemplars to events_total metric for spanmetrics --- .../add-exemplars-to-events-total-metric.yaml | 27 ++++++++++++++++ connector/spanmetricsconnector/connector.go | 3 ++ .../spanmetricsconnector/connector_test.go | 32 +++++++++++++------ 3 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 .chloggen/add-exemplars-to-events-total-metric.yaml diff --git a/.chloggen/add-exemplars-to-events-total-metric.yaml b/.chloggen/add-exemplars-to-events-total-metric.yaml new file mode 100644 index 000000000000..60ca23a4bc82 --- /dev/null +++ b/.chloggen/add-exemplars-to-events-total-metric.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: spanmetricsconnector + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Add exemplars to sum metric + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [27451] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] \ No newline at end of file diff --git a/connector/spanmetricsconnector/connector.go b/connector/spanmetricsconnector/connector.go index 3832dca63997..f3e82ffbefa9 100644 --- a/connector/spanmetricsconnector/connector.go +++ b/connector/spanmetricsconnector/connector.go @@ -361,6 +361,9 @@ func (p *connectorImp) aggregateMetrics(traces ptrace.Traces) { p.metricKeyToDimensions.Add(eKey, eAttributes) } e := events.GetOrCreate(eKey, eAttributes) + if p.config.Exemplars.Enabled && !span.TraceID().IsEmpty() { + e.AddExemplar(span.TraceID(), span.SpanID(), duration) + } e.Add(1) } } diff --git a/connector/spanmetricsconnector/connector_test.go b/connector/spanmetricsconnector/connector_test.go index c10078313795..947d13dc5d8d 100644 --- a/connector/spanmetricsconnector/connector_test.go +++ b/connector/spanmetricsconnector/connector_test.go @@ -413,6 +413,19 @@ func enabledExemplarsConfig() ExemplarsConfig { } } +func enabledEventsConfig() EventsConfig { + return EventsConfig{ + Enabled: true, + Dimensions: []Dimension{{Name: "exception.type"}}, + } +} + +func disabledEventsConfig() EventsConfig { + return EventsConfig{ + Enabled: false, + } +} + func explicitHistogramsConfig() HistogramConfig { return HistogramConfig{ Unit: defaultUnit, @@ -586,7 +599,7 @@ func TestConcurrentShutdown(t *testing.T) { ticker := mockClock.NewTicker(time.Nanosecond) // Test - p := newConnectorImp(t, new(consumertest.MetricsSink), nil, explicitHistogramsConfig, disabledExemplarsConfig, cumulative, logger, ticker) + p := newConnectorImp(t, new(consumertest.MetricsSink), nil, explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, logger, ticker) err := p.Start(ctx, componenttest.NewNopHost()) require.NoError(t, err) @@ -666,7 +679,7 @@ func TestConsumeMetricsErrors(t *testing.T) { } mockClock := clock.NewMock(time.Now()) ticker := mockClock.NewTicker(time.Nanosecond) - p := newConnectorImp(t, mcon, nil, explicitHistogramsConfig, disabledExemplarsConfig, cumulative, logger, ticker) + p := newConnectorImp(t, mcon, nil, explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, logger, ticker) ctx := metadata.NewIncomingContext(context.Background(), nil) err := p.Start(ctx, componenttest.NewNopHost()) @@ -828,7 +841,7 @@ func TestConsumeTraces(t *testing.T) { mockClock := clock.NewMock(time.Now()) ticker := mockClock.NewTicker(time.Nanosecond) - p := newConnectorImp(t, mcon, stringp("defaultNullValue"), tc.histogramConfig, tc.exemplarConfig, tc.aggregationTemporality, zaptest.NewLogger(t), ticker) + p := newConnectorImp(t, mcon, stringp("defaultNullValue"), tc.histogramConfig, tc.exemplarConfig, disabledEventsConfig, tc.aggregationTemporality, zaptest.NewLogger(t), ticker) ctx := metadata.NewIncomingContext(context.Background(), nil) err := p.Start(ctx, componenttest.NewNopHost()) @@ -854,7 +867,7 @@ func TestConsumeTraces(t *testing.T) { func TestMetricKeyCache(t *testing.T) { mcon := consumertest.NewNop() - p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, cumulative, zaptest.NewLogger(t), nil) + p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, zaptest.NewLogger(t), nil) traces := buildSampleTrace() // Test @@ -885,7 +898,7 @@ func BenchmarkConnectorConsumeTraces(b *testing.B) { // Prepare mcon := consumertest.NewNop() - conn := newConnectorImp(nil, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, cumulative, zaptest.NewLogger(b), nil) + conn := newConnectorImp(nil, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, zaptest.NewLogger(b), nil) traces := buildSampleTrace() @@ -899,7 +912,7 @@ func BenchmarkConnectorConsumeTraces(b *testing.B) { func TestExcludeDimensionsConsumeTraces(t *testing.T) { mcon := consumertest.NewNop() excludeDimensions := []string{"span.kind", "span.name", "totallyWrongNameDoesNotAffectAnything"} - p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, cumulative, zaptest.NewLogger(t), nil, excludeDimensions...) + p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, zaptest.NewLogger(t), nil, excludeDimensions...) traces := buildSampleTrace() // Test @@ -948,7 +961,7 @@ func TestExcludeDimensionsConsumeTraces(t *testing.T) { } -func newConnectorImp(t *testing.T, mcon consumer.Metrics, defaultNullValue *string, histogramConfig func() HistogramConfig, exemplarsConfig func() ExemplarsConfig, temporality string, logger *zap.Logger, ticker *clock.Ticker, excludedDimensions ...string) *connectorImp { +func newConnectorImp(t *testing.T, mcon consumer.Metrics, defaultNullValue *string, histogramConfig func() HistogramConfig, exemplarsConfig func() ExemplarsConfig, eventsConfig func() EventsConfig, temporality string, logger *zap.Logger, ticker *clock.Ticker, excludedDimensions ...string) *connectorImp { cfg := &Config{ AggregationTemporality: temporality, @@ -972,6 +985,7 @@ func newConnectorImp(t *testing.T, mcon consumer.Metrics, defaultNullValue *stri // Add a resource attribute to test "process" attributes like IP, host, region, cluster, etc. {regionResourceAttrName, nil}, }, + Events: eventsConfig(), } c, err := newConnector(logger, cfg, ticker) require.NoError(t, err) @@ -1066,7 +1080,7 @@ func TestConnectorConsumeTracesEvictedCacheKey(t *testing.T) { ticker := mockClock.NewTicker(time.Nanosecond) // Note: default dimension key cache size is 2. - p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, cumulative, zaptest.NewLogger(t), ticker) + p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, zaptest.NewLogger(t), ticker) ctx := metadata.NewIncomingContext(context.Background(), nil) err := p.Start(ctx, componenttest.NewNopHost()) @@ -1320,7 +1334,7 @@ func TestSpanMetrics_Events(t *testing.T) { } func TestExemplarsForSumMetrics(t *testing.T) { mcon := consumertest.NewNop() - p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, enabledExemplarsConfig, cumulative, zaptest.NewLogger(t), nil) + p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, enabledExemplarsConfig, enabledEventsConfig, cumulative, zaptest.NewLogger(t), nil) traces := buildSampleTrace() // Test