Skip to content

Commit d979075

Browse files
[Spanmetrics] - Add exemplars to events_total metric (open-telemetry#29090)
**Description:** Add exemplars to events_total metric for spanmetrics
1 parent 84c9959 commit d979075

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: spanmetricsconnector
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add exemplars to sum metric
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [27451]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

connector/spanmetricsconnector/connector.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ func (p *connectorImp) aggregateMetrics(traces ptrace.Traces) {
361361
p.metricKeyToDimensions.Add(eKey, eAttributes)
362362
}
363363
e := events.GetOrCreate(eKey, eAttributes)
364+
if p.config.Exemplars.Enabled && !span.TraceID().IsEmpty() {
365+
e.AddExemplar(span.TraceID(), span.SpanID(), duration)
366+
}
364367
e.Add(1)
365368
}
366369
}

connector/spanmetricsconnector/connector_test.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,19 @@ func enabledExemplarsConfig() ExemplarsConfig {
413413
}
414414
}
415415

416+
func enabledEventsConfig() EventsConfig {
417+
return EventsConfig{
418+
Enabled: true,
419+
Dimensions: []Dimension{{Name: "exception.type"}},
420+
}
421+
}
422+
423+
func disabledEventsConfig() EventsConfig {
424+
return EventsConfig{
425+
Enabled: false,
426+
}
427+
}
428+
416429
func explicitHistogramsConfig() HistogramConfig {
417430
return HistogramConfig{
418431
Unit: defaultUnit,
@@ -586,7 +599,7 @@ func TestConcurrentShutdown(t *testing.T) {
586599
ticker := mockClock.NewTicker(time.Nanosecond)
587600

588601
// Test
589-
p := newConnectorImp(t, new(consumertest.MetricsSink), nil, explicitHistogramsConfig, disabledExemplarsConfig, cumulative, logger, ticker)
602+
p := newConnectorImp(t, new(consumertest.MetricsSink), nil, explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, logger, ticker)
590603
err := p.Start(ctx, componenttest.NewNopHost())
591604
require.NoError(t, err)
592605

@@ -666,7 +679,7 @@ func TestConsumeMetricsErrors(t *testing.T) {
666679
}
667680
mockClock := clock.NewMock(time.Now())
668681
ticker := mockClock.NewTicker(time.Nanosecond)
669-
p := newConnectorImp(t, mcon, nil, explicitHistogramsConfig, disabledExemplarsConfig, cumulative, logger, ticker)
682+
p := newConnectorImp(t, mcon, nil, explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, logger, ticker)
670683

671684
ctx := metadata.NewIncomingContext(context.Background(), nil)
672685
err := p.Start(ctx, componenttest.NewNopHost())
@@ -828,7 +841,7 @@ func TestConsumeTraces(t *testing.T) {
828841
mockClock := clock.NewMock(time.Now())
829842
ticker := mockClock.NewTicker(time.Nanosecond)
830843

831-
p := newConnectorImp(t, mcon, stringp("defaultNullValue"), tc.histogramConfig, tc.exemplarConfig, tc.aggregationTemporality, zaptest.NewLogger(t), ticker)
844+
p := newConnectorImp(t, mcon, stringp("defaultNullValue"), tc.histogramConfig, tc.exemplarConfig, disabledEventsConfig, tc.aggregationTemporality, zaptest.NewLogger(t), ticker)
832845

833846
ctx := metadata.NewIncomingContext(context.Background(), nil)
834847
err := p.Start(ctx, componenttest.NewNopHost())
@@ -854,7 +867,7 @@ func TestConsumeTraces(t *testing.T) {
854867
func TestMetricKeyCache(t *testing.T) {
855868
mcon := consumertest.NewNop()
856869

857-
p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, cumulative, zaptest.NewLogger(t), nil)
870+
p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, zaptest.NewLogger(t), nil)
858871
traces := buildSampleTrace()
859872

860873
// Test
@@ -885,7 +898,7 @@ func BenchmarkConnectorConsumeTraces(b *testing.B) {
885898
// Prepare
886899
mcon := consumertest.NewNop()
887900

888-
conn := newConnectorImp(nil, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, cumulative, zaptest.NewLogger(b), nil)
901+
conn := newConnectorImp(nil, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, zaptest.NewLogger(b), nil)
889902

890903
traces := buildSampleTrace()
891904

@@ -899,7 +912,7 @@ func BenchmarkConnectorConsumeTraces(b *testing.B) {
899912
func TestExcludeDimensionsConsumeTraces(t *testing.T) {
900913
mcon := consumertest.NewNop()
901914
excludeDimensions := []string{"span.kind", "span.name", "totallyWrongNameDoesNotAffectAnything"}
902-
p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, cumulative, zaptest.NewLogger(t), nil, excludeDimensions...)
915+
p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, zaptest.NewLogger(t), nil, excludeDimensions...)
903916
traces := buildSampleTrace()
904917

905918
// Test
@@ -948,7 +961,7 @@ func TestExcludeDimensionsConsumeTraces(t *testing.T) {
948961

949962
}
950963

951-
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 {
964+
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 {
952965

953966
cfg := &Config{
954967
AggregationTemporality: temporality,
@@ -972,6 +985,7 @@ func newConnectorImp(t *testing.T, mcon consumer.Metrics, defaultNullValue *stri
972985
// Add a resource attribute to test "process" attributes like IP, host, region, cluster, etc.
973986
{regionResourceAttrName, nil},
974987
},
988+
Events: eventsConfig(),
975989
}
976990
c, err := newConnector(logger, cfg, ticker)
977991
require.NoError(t, err)
@@ -1066,7 +1080,7 @@ func TestConnectorConsumeTracesEvictedCacheKey(t *testing.T) {
10661080
ticker := mockClock.NewTicker(time.Nanosecond)
10671081

10681082
// Note: default dimension key cache size is 2.
1069-
p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, cumulative, zaptest.NewLogger(t), ticker)
1083+
p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, zaptest.NewLogger(t), ticker)
10701084

10711085
ctx := metadata.NewIncomingContext(context.Background(), nil)
10721086
err := p.Start(ctx, componenttest.NewNopHost())
@@ -1320,7 +1334,7 @@ func TestSpanMetrics_Events(t *testing.T) {
13201334
}
13211335
func TestExemplarsForSumMetrics(t *testing.T) {
13221336
mcon := consumertest.NewNop()
1323-
p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, enabledExemplarsConfig, cumulative, zaptest.NewLogger(t), nil)
1337+
p := newConnectorImp(t, mcon, stringp("defaultNullValue"), explicitHistogramsConfig, enabledExemplarsConfig, enabledEventsConfig, cumulative, zaptest.NewLogger(t), nil)
13241338
traces := buildSampleTrace()
13251339

13261340
// Test

0 commit comments

Comments
 (0)