Skip to content

Commit

Permalink
Refactor global labels removal in metrics aggregation process
Browse files Browse the repository at this point in the history
Signed-off-by: Up Neck <[email protected]>
  • Loading branch information
up2neck committed Jan 22, 2025
1 parent e3dd077 commit 4524f61
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
23 changes: 14 additions & 9 deletions x-pack/apm-server/aggregation/lsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ func (a *Aggregator) Stop(ctx context.Context) error {
// so that aggregator can consume events from intake.
func (a *Aggregator) ProcessBatch(ctx context.Context, b *modelpb.Batch) error {
for _, e := range *b {
removeRUMGlobalLabels(e)
// Remove global labels for RUM services to avoid explosion of metric groups
// to track for servicetxmetrics.
// For consistency, this will remove labels for other aggregated metrics as well.
if isRumAgent(e.Agent.GetName()) {
removeGlobalLabels(e)
}
}
return a.baseaggregator.AggregateBatch(ctx, [16]byte{}, b)
}
Expand All @@ -108,17 +113,17 @@ func wrapNextProcessor(processor modelpb.BatchProcessor) aggregators.Processor {
}
}

func removeRUMGlobalLabels(event *modelpb.APMEvent) {
// Remove global labels for RUM services to avoid explosion of metric groups
// to track for servicetxmetrics.
// For consistency, this will remove labels for other aggregated metrics as well.
switch event.GetAgent().GetName() {
// Check Elastic Agent belongs to rum-services
func isRumAgent(name string) bool {
switch name {
case "rum-js", "js-base", "android/java", "iOS/swift":
default:
return
return true
}
return false
}

// Setting the labels to non-global so that they are ignored by the aggregator.
// Setting the labels to non-global so that they are ignored by the aggregator.
func removeGlobalLabels(event *modelpb.APMEvent) {
for _, v := range event.Labels {
v.Global = false
}
Expand Down
28 changes: 28 additions & 0 deletions x-pack/apm-server/aggregation/lsm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package aggregation

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsRumAgent(t *testing.T) {
cases := []struct {
name string
expect bool
}{
{"rum-js", true},
{"js-base", true},
{"android/java", true},
{"iOS/swift", true},
{"nodejs", false},
{"python", false},
{"java", false},
{"dotnet", false},
}

for _, tt := range cases {
got := isRumAgent(tt.name)
assert.Equal(t, tt.expect, got)
}
}

0 comments on commit 4524f61

Please sign in to comment.