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

[Spanmetrics] - Add exemplars to Sum metrics #28671

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Resolve conflicts in connector_test.go
  • Loading branch information
aishyandapalli committed Oct 31, 2023
commit 582a9a52a9fad4f18f35dc44fa8acc5f501d8e89
3 changes: 3 additions & 0 deletions connector/spanmetricsconnector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ func (p *connectorImp) aggregateMetrics(traces ptrace.Traces) {
}
// aggregate sums metrics
s := sums.GetOrCreate(key, attributes)
if p.config.Exemplars.Enabled && !span.TraceID().IsEmpty() {
s.AddExemplar(span.TraceID(), span.SpanID(), duration)
}
s.Add(1)

// aggregate events metrics
Expand Down
31 changes: 31 additions & 0 deletions connector/spanmetricsconnector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1318,3 +1318,34 @@ 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)
traces := buildSampleTrace()

// Test
ctx := metadata.NewIncomingContext(context.Background(), nil)

err := p.ConsumeTraces(ctx, traces)
require.NoError(t, err)
metrics := p.buildMetrics()

for i := 0; i < metrics.ResourceMetrics().Len(); i++ {
rm := metrics.ResourceMetrics().At(i)
ism := rm.ScopeMetrics()
// Checking all metrics, naming notice: ilmC/mC - C here is for Counter.
for ilmC := 0; ilmC < ism.Len(); ilmC++ {
m := ism.At(ilmC).Metrics()
for mC := 0; mC < m.Len(); mC++ {
metric := m.At(mC)
if metric.Type() == pmetric.MetricTypeSum {
dps := metric.Sum().DataPoints()
for dpi := 0; dpi < dps.Len(); dpi++ {
dp := dps.At(dpi)
assert.True(t, dp.Exemplars().Len() > 0)
aishyandapalli marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
}
}
13 changes: 13 additions & 0 deletions connector/spanmetricsconnector/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func (h *exponentialHistogram) AddExemplar(traceID pcommon.TraceID, spanID pcomm
type Sum struct {
attributes pcommon.Map
count uint64
exemplars pmetric.ExemplarSlice
}

func (s *Sum) Add(value uint64) {
Expand All @@ -251,12 +252,20 @@ func (m *SumMetrics) GetOrCreate(key Key, attributes pcommon.Map) *Sum {
if !ok {
s = &Sum{
attributes: attributes,
exemplars: pmetric.NewExemplarSlice(),
}
m.metrics[key] = s
}
return s
}

func (s *Sum) AddExemplar(traceID pcommon.TraceID, spanID pcommon.SpanID, value float64) {
e := s.exemplars.AppendEmpty()
e.SetTraceID(traceID)
e.SetSpanID(spanID)
e.SetDoubleValue(value)
}

func (m *SumMetrics) BuildMetrics(
metric pmetric.Metric,
start pcommon.Timestamp,
Expand All @@ -273,6 +282,10 @@ func (m *SumMetrics) BuildMetrics(
dp.SetStartTimestamp(start)
dp.SetTimestamp(timestamp)
dp.SetIntValue(int64(s.count))
for i := 0; i < s.exemplars.Len(); i++ {
s.exemplars.At(i).SetTimestamp(timestamp)
}
s.exemplars.CopyTo(dp.Exemplars())
s.attributes.CopyTo(dp.Attributes())
}
}
Expand Down