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

aggregation/spanmetrics: handle composite spans #5896

Merged
merged 1 commit into from
Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions changelogs/head.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ https://github.com/elastic/apm-server/compare/7.13\...master[View commits]

[float]
==== Added
- `service_destination` span metrics now take into account composite spans {pull}5896[5896]

[float]
==== Deprecated
14 changes: 12 additions & 2 deletions x-pack/apm-server/aggregation/spanmetrics/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,26 @@ func (a *Aggregator) processSpan(event *model.APMEvent) model.APMEvent {
return model.APMEvent{}
}

// For composite spans we use the composite sum duration, which is the sum of
// pre-aggregated spans and excludes time gaps that are counted in the reported
// span duration. For non-composite spans we just use the reported span duration.
count := 1
durationMillis := event.Span.Duration
if event.Span.Composite != nil {
count = event.Span.Composite.Count
durationMillis = event.Span.Composite.Sum
}
duration := time.Duration(durationMillis * float64(time.Millisecond))

key := aggregationKey{
serviceEnvironment: event.Service.Environment,
serviceName: event.Service.Name,
agentName: event.Agent.Name,
outcome: event.Event.Outcome,
resource: event.Span.DestinationService.Resource,
}
duration := time.Duration(event.Span.Duration * float64(time.Millisecond))
metrics := spanMetrics{
count: event.Span.RepresentativeCount,
count: float64(count) * event.Span.RepresentativeCount,
sum: float64(duration.Microseconds()) * event.Span.RepresentativeCount,
}
if a.active.storeOrUpdate(key, metrics) {
Expand Down
39 changes: 39 additions & 0 deletions x-pack/apm-server/aggregation/spanmetrics/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,45 @@ func TestAggregatorRun(t *testing.T) {
}
}

func TestAggregateCompositeSpan(t *testing.T) {
batches := make(chan model.Batch, 1)
agg, err := NewAggregator(AggregatorConfig{
BatchProcessor: makeChanBatchProcessor(batches),
Interval: 10 * time.Millisecond,
MaxGroups: 1000,
})
require.NoError(t, err)

span := makeSpan("service-A", "java", "final_destination", "success", time.Second, 2)
span.Span.Composite = &model.Composite{Count: 25, Sum: 700 /* milliseconds */}
err = agg.ProcessBatch(context.Background(), &model.Batch{span})
require.NoError(t, err)

// Start the aggregator after processing to ensure metrics are aggregated deterministically.
go agg.Run()
defer agg.Stop(context.Background())

batch := expectBatch(t, batches)
metricsets := batchMetricsets(t, batch)

assert.Equal(t, []model.APMEvent{{
Agent: model.Agent{Name: "java"},
Service: model.Service{Name: "service-A"},
Event: model.Event{Outcome: "success"},
Metricset: &model.Metricset{
Name: "service_destination",
Span: model.MetricsetSpan{
DestinationService: model.DestinationService{Resource: "final_destination"},
},
Samples: map[string]model.MetricsetSample{
"span.destination.service.response_time.count": {Value: 50.0},
"span.destination.service.response_time.sum.us": {Value: 1400000},
"metricset.period": {Value: 10},
},
},
}}, metricsets)
}

func TestAggregatorOverflow(t *testing.T) {
batches := make(chan model.Batch, 1)
agg, err := NewAggregator(AggregatorConfig{
Expand Down