Skip to content

Commit

Permalink
[connector/datadog] Improve performance in cases with many peer tags (o…
Browse files Browse the repository at this point in the history
…pen-telemetry#34945)

**Description:**
Improves performance of Datadog connector when there are many peer tags
and `connector.datadogconnector.NativeIngest` is enabled

**Link to tracking Issue:** 
Related to DataDog/datadog-agent#28908

**Testing:** 
Added new benchmark tests, results:
```
% go test -bench . -benchmem  -benchtime=1000x
goos: darwin
goarch: arm64
pkg: github.com/open-telemetry/opentelemetry-collector-contrib/connector/datadogconnector
cpu: Apple M1 Max
BenchmarkPeerTags_Native-10    	    1000	  22851735 ns/op	    9147 B/op	     118 allocs/op
BenchmarkPeerTags_Legacy-10    	    1000	  27704684 ns/op	   12157 B/op	     152 allocs/op
PASS
```
  • Loading branch information
songy23 authored and f7o committed Sep 12, 2024
1 parent 7798b7d commit 61a6dde
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 72 deletions.
27 changes: 27 additions & 0 deletions .chloggen/dd-conn-peer-tags.yaml
Original file line number Diff line number Diff line change
@@ -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: datadogconnector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Optimize Datadog connector when there are many peer tags and `connector.datadogconnector.NativeIngest` is enabled"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [34945]

# (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: "`connector.datadogconnector.NativeIngest` is currently enabled by default"

# 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: []
10 changes: 5 additions & 5 deletions cmd/otelcontribcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,12 @@ require (
github.com/DataDog/datadog-agent/pkg/logs/sources v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/logs/status/statusinterface v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/logs/status/utils v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.58.0-devel.0.20240830155027-f44a9da12951 // indirect
github.com/DataDog/datadog-agent/pkg/proto v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/status/health v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/telemetry v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/trace v0.57.0-devel.0.20240722160158-ad956a31a730 // indirect
github.com/DataDog/datadog-agent/pkg/trace v0.58.0-devel.0.20240830155027-f44a9da12951 // indirect
github.com/DataDog/datadog-agent/pkg/util/backoff v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/util/cgroups v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/util/executable v0.56.2 // indirect
Expand All @@ -352,7 +352,7 @@ require (
github.com/DataDog/datadog-api-client-go/v2 v2.29.0 // indirect
github.com/DataDog/datadog-go/v5 v5.5.0 // indirect
github.com/DataDog/dd-sensitive-data-scanner/sds-go/go v0.0.0-20240419161837-f1b2f553edfe // indirect
github.com/DataDog/go-sqllexer v0.0.12 // indirect
github.com/DataDog/go-sqllexer v0.0.13 // indirect
github.com/DataDog/go-tuf v1.1.0-0.5.2 // indirect
github.com/DataDog/gohai v0.0.0-20230524154621-4316413895ee // indirect
github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata v0.19.0 // indirect
Expand Down Expand Up @@ -826,14 +826,14 @@ require (
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.6.0 // indirect
golang.org/x/tools v0.23.0 // indirect
golang.org/x/tools v0.24.0 // indirect
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9 // indirect
gonum.org/v1/gonum v0.15.1 // indirect
google.golang.org/api v0.194.0 // indirect
Expand Down
20 changes: 10 additions & 10 deletions cmd/otelcontribcol/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions connector/datadogconnector/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package datadogconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/datadogconnector"

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/connector/connectortest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
)

func genTrace() ptrace.Traces {
start := time.Now().Add(-1 * time.Second)
end := time.Now()
traces := ptrace.NewTraces()
rspan := traces.ResourceSpans().AppendEmpty()
rattrs := rspan.Resource().Attributes()
rattrs.PutStr("deployment.environment", "test_env")
rattrs.PutStr("service.name", "test_svc")
sspan := rspan.ScopeSpans().AppendEmpty()
span := sspan.Spans().AppendEmpty()
span.SetTraceID(testTraceID)
span.SetSpanID(testSpanID1)
span.SetStartTimestamp(pcommon.NewTimestampFromTime(start))
span.SetEndTimestamp(pcommon.NewTimestampFromTime(end))
span.SetName("span_name")
span.SetKind(ptrace.SpanKindClient)
span.Attributes().PutStr("peer.service", "my_peer_svc")
span.Attributes().PutStr("rpc.service", "my_rpc_svc")
span.Attributes().PutStr("net.peer.name", "my_net_peer")
return traces
}

func BenchmarkPeerTags_Native(b *testing.B) {
benchmarkPeerTags(b)
}

func BenchmarkPeerTags_Legacy(b *testing.B) {
err := featuregate.GlobalRegistry().Set(NativeIngestFeatureGate.ID(), false)
assert.NoError(b, err)
defer func() {
_ = featuregate.GlobalRegistry().Set(NativeIngestFeatureGate.ID(), true)
}()

benchmarkPeerTags(b)
}

func benchmarkPeerTags(b *testing.B) {
cfg := NewFactory().CreateDefaultConfig().(*Config)
cfg.Traces.ComputeStatsBySpanKind = true
cfg.Traces.PeerTagsAggregation = true
cfg.Traces.BucketInterval = 1 * time.Millisecond
cfg.Traces.TraceBuffer = 0

factory := NewFactory()
creationParams := connectortest.NewNopSettings()
metricsSink := &consumertest.MetricsSink{}

tconn, err := factory.CreateTracesToMetrics(context.Background(), creationParams, cfg, metricsSink)
assert.NoError(b, err)

err = tconn.Start(context.Background(), componenttest.NewNopHost())
if err != nil {
b.Errorf("Error starting connector: %v", err)
return
}
defer func() {
require.NoError(b, tconn.Shutdown(context.Background()))
}()

b.ResetTimer()

for n := 0; n < b.N; n++ {
err = tconn.ConsumeTraces(context.Background(), genTrace())
assert.NoError(b, err)
for {
metrics := metricsSink.AllMetrics()
if len(metrics) > 0 {
assert.Len(b, metrics, 1)
break
}
}
metricsSink.Reset()
}
}
6 changes: 5 additions & 1 deletion connector/datadogconnector/connector_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type traceToMetricConnectorNative struct {
// ctagKeys are container tag keys
ctagKeys []string

// peerTagKeys are peer tag keys to group APM stats
peerTagKeys []string

// translator specifies the translator used to transform APM Stats Payloads
// from the agent to OTLP Metrics.
translator *metrics.Translator
Expand Down Expand Up @@ -75,6 +78,7 @@ func newTraceToMetricConnectorNative(set component.TelemetrySettings, cfg compon
translator: trans,
tcfg: tcfg,
ctagKeys: cfg.(*Config).Traces.ResourceAttributesAsContainerTags,
peerTagKeys: tcfg.ConfiguredPeerTags(),
concentrator: stats.NewConcentrator(tcfg, statsWriter, time.Now(), metricsClient),
statsout: statsout,
metricsConsumer: metricsConsumer,
Expand Down Expand Up @@ -114,7 +118,7 @@ func (c *traceToMetricConnectorNative) Capabilities() consumer.Capabilities {
}

func (c *traceToMetricConnectorNative) ConsumeTraces(_ context.Context, traces ptrace.Traces) error {
inputs := stats.OTLPTracesToConcentratorInputs(traces, c.tcfg, c.ctagKeys)
inputs := stats.OTLPTracesToConcentratorInputs(traces, c.tcfg, c.ctagKeys, c.peerTagKeys)
for _, input := range inputs {
c.concentrator.Add(input)
}
Expand Down
8 changes: 4 additions & 4 deletions connector/datadogconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/DataDog/datadog-agent/comp/otelcol/otlp/components/metricsclient v0.56.2
github.com/DataDog/datadog-agent/comp/otelcol/otlp/components/statsprocessor v0.56.2
github.com/DataDog/datadog-agent/pkg/proto v0.56.2
github.com/DataDog/datadog-agent/pkg/trace v0.57.0-devel.0.20240722160158-ad956a31a730
github.com/DataDog/datadog-agent/pkg/trace v0.58.0-devel.0.20240830155027-f44a9da12951
github.com/DataDog/datadog-go/v5 v5.5.0
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes v0.19.0
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics v0.19.0
Expand Down Expand Up @@ -70,7 +70,7 @@ require (
github.com/DataDog/datadog-agent/pkg/logs/sources v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/logs/status/statusinterface v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/logs/status/utils v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.58.0-devel.0.20240830155027-f44a9da12951 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/status/health v0.56.2 // indirect
github.com/DataDog/datadog-agent/pkg/telemetry v0.56.2 // indirect
Expand All @@ -93,7 +93,7 @@ require (
github.com/DataDog/datadog-agent/pkg/version v0.56.2 // indirect
github.com/DataDog/datadog-api-client-go/v2 v2.29.0 // indirect
github.com/DataDog/dd-sensitive-data-scanner/sds-go/go v0.0.0-20240419161837-f1b2f553edfe // indirect
github.com/DataDog/go-sqllexer v0.0.12 // indirect
github.com/DataDog/go-sqllexer v0.0.13 // indirect
github.com/DataDog/go-tuf v1.1.0-0.5.2 // indirect
github.com/DataDog/gohai v0.0.0-20230524154621-4316413895ee // indirect
github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata v0.19.0 // indirect
Expand Down Expand Up @@ -273,7 +273,7 @@ require (
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/time v0.6.0 // indirect
gonum.org/v1/gonum v0.15.1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect
Expand Down
Loading

0 comments on commit 61a6dde

Please sign in to comment.