From a4018970388f532e06a19fc2a0aec042668b5cab Mon Sep 17 00:00:00 2001 From: Yang Song Date: Thu, 21 Nov 2024 03:54:07 -0500 Subject: [PATCH] [pkg/datadog] Refactor the API that provides metrics translator (#36474) #### Description Refactor the API that provides metrics translator so that it can be used in the datadog-agent repo. --- .chloggen/dd-config-api.yaml | 27 ++++++++++++++++++ exporter/datadogexporter/metrics_exporter.go | 25 ++++++++++++++-- pkg/datadog/config/metrics.go | 30 ++------------------ pkg/datadog/go.mod | 4 +-- pkg/datadog/go.sum | 4 --- 5 files changed, 54 insertions(+), 36 deletions(-) create mode 100644 .chloggen/dd-config-api.yaml diff --git a/.chloggen/dd-config-api.yaml b/.chloggen/dd-config-api.yaml new file mode 100644 index 000000000000..653aceee4b9e --- /dev/null +++ b/.chloggen/dd-config-api.yaml @@ -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: breaking + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: pkg/datadog + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Refactor the API that provides metrics translator" + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [36474] + +# (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: "This is API change only and does not affect end users" + +# 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: [api] diff --git a/exporter/datadogexporter/metrics_exporter.go b/exporter/datadogexporter/metrics_exporter.go index 18f5b76fe122..ed2938e0a474 100644 --- a/exporter/datadogexporter/metrics_exporter.go +++ b/exporter/datadogexporter/metrics_exporter.go @@ -19,6 +19,7 @@ import ( "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/source" otlpmetrics "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics" "go.opentelemetry.io/collector/exporter" + "go.opentelemetry.io/collector/featuregate" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/pmetric" "go.uber.org/zap" @@ -29,9 +30,20 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metrics" "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metrics/sketches" "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/scrub" - datadogconfig "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/datadog/config" ) +var metricRemappingDisableddFeatureGate = featuregate.GlobalRegistry().MustRegister( + "exporter.datadogexporter.metricremappingdisabled", + featuregate.StageAlpha, + featuregate.WithRegisterDescription("When enabled the Datadog Exporter remaps OpenTelemetry semantic conventions to Datadog semantic conventions. This feature gate is only for internal use."), + featuregate.WithRegisterReferenceURL("https://docs.datadoghq.com/opentelemetry/schema_semantics/metrics_mapping/"), +) + +// isMetricRemappingDisabled returns true if the datadogexporter should generate Datadog-compliant metrics from OpenTelemetry metrics +func isMetricRemappingDisabled() bool { + return metricRemappingDisableddFeatureGate.IsEnabled() +} + type metricsExporter struct { params exporter.Settings cfg *Config @@ -61,7 +73,16 @@ func newMetricsExporter( metadataReporter *inframetadata.Reporter, statsOut chan []byte, ) (*metricsExporter, error) { - tr, err := datadogconfig.TranslatorFromConfig(params.TelemetrySettings, cfg.Metrics, attrsTranslator, sourceProvider, statsOut) + options := cfg.Metrics.ToTranslatorOpts() + options = append(options, otlpmetrics.WithFallbackSourceProvider(sourceProvider)) + options = append(options, otlpmetrics.WithStatsOut(statsOut)) + if isMetricRemappingDisabled() { + params.TelemetrySettings.Logger.Warn("Metric remapping is disabled in the Datadog exporter. OpenTelemetry metrics must be mapped to Datadog semantics before metrics are exported to Datadog (ex: via a processor).") + } else { + options = append(options, otlpmetrics.WithRemapping()) + } + + tr, err := otlpmetrics.NewTranslator(params.TelemetrySettings, attrsTranslator, options...) if err != nil { return nil, err } diff --git a/pkg/datadog/config/metrics.go b/pkg/datadog/config/metrics.go index fc2c307a1be3..c17a148af8e4 100644 --- a/pkg/datadog/config/metrics.go +++ b/pkg/datadog/config/metrics.go @@ -7,12 +7,8 @@ import ( "encoding" "fmt" - "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes" - "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/source" otlpmetrics "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics" - "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/confignet" - "go.opentelemetry.io/collector/featuregate" ) // MetricsConfig defines the metrics exporter specific configuration options @@ -212,29 +208,10 @@ type MetricsExporterConfig struct { InstrumentationScopeMetadataAsTags bool `mapstructure:"instrumentation_scope_metadata_as_tags"` } -var metricRemappingDisableddFeatureGate = featuregate.GlobalRegistry().MustRegister( - "exporter.datadogexporter.metricremappingdisabled", - featuregate.StageAlpha, - featuregate.WithRegisterDescription("When enabled the Datadog Exporter remaps OpenTelemetry semantic conventions to Datadog semantic conventions. This feature gate is only for internal use."), - featuregate.WithRegisterReferenceURL("https://docs.datadoghq.com/opentelemetry/schema_semantics/metrics_mapping/"), -) - -// isMetricRemappingDisabled returns true if the datadogexporter should generate Datadog-compliant metrics from OpenTelemetry metrics -func isMetricRemappingDisabled() bool { - return metricRemappingDisableddFeatureGate.IsEnabled() -} - -// TranslatorFromConfig creates a new metrics translator from the exporter -func TranslatorFromConfig(set component.TelemetrySettings, mcfg MetricsConfig, attrsTranslator *attributes.Translator, sourceProvider source.Provider, statsOut chan []byte) (*otlpmetrics.Translator, error) { +// ToTranslatorOpts returns a list of metrics translator options from the metrics config +func (mcfg MetricsConfig) ToTranslatorOpts() []otlpmetrics.TranslatorOption { options := []otlpmetrics.TranslatorOption{ otlpmetrics.WithDeltaTTL(mcfg.DeltaTTL), - otlpmetrics.WithFallbackSourceProvider(sourceProvider), - } - - if isMetricRemappingDisabled() { - set.Logger.Warn("Metric remapping is disabled in the Datadog exporter. OpenTelemetry metrics must be mapped to Datadog semantics before metrics are exported to Datadog (ex: via a processor).") - } else { - options = append(options, otlpmetrics.WithRemapping()) } if mcfg.HistConfig.SendAggregations { @@ -262,6 +239,5 @@ func TranslatorFromConfig(set component.TelemetrySettings, mcfg MetricsConfig, a options = append(options, otlpmetrics.WithInitialCumulMonoValueMode( otlpmetrics.InitialCumulMonoValueMode(mcfg.SumConfig.InitialCumulativeMonotonicMode))) - options = append(options, otlpmetrics.WithStatsOut(statsOut)) - return otlpmetrics.NewTranslator(set, attrsTranslator, options...) + return options } diff --git a/pkg/datadog/go.mod b/pkg/datadog/go.mod index 49a3971b006c..4a5aa1aa865d 100644 --- a/pkg/datadog/go.mod +++ b/pkg/datadog/go.mod @@ -4,7 +4,6 @@ go 1.22.0 require ( github.com/DataDog/datadog-agent/pkg/util/hostname/validate v0.59.0 - github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes v0.21.0 github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics v0.21.0 github.com/stretchr/testify v1.9.0 go.opentelemetry.io/collector/component v0.114.0 @@ -17,7 +16,6 @@ require ( go.opentelemetry.io/collector/config/configtls v1.20.0 go.opentelemetry.io/collector/confmap v1.20.0 go.opentelemetry.io/collector/exporter v0.114.0 - go.opentelemetry.io/collector/featuregate v1.20.0 go.uber.org/zap v1.27.0 ) @@ -25,6 +23,7 @@ require ( github.com/DataDog/datadog-agent/pkg/proto v0.52.0-devel // indirect github.com/DataDog/datadog-agent/pkg/util/log v0.59.0 // indirect github.com/DataDog/datadog-agent/pkg/util/scrubber v0.59.0 // indirect + github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes v0.21.0 // indirect github.com/DataDog/opentelemetry-mapping-go/pkg/quantile v0.21.0 // indirect github.com/DataDog/sketches-go v1.4.4 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect @@ -40,7 +39,6 @@ require ( github.com/golang/protobuf v1.5.3 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/uuid v1.6.0 // indirect - github.com/hashicorp/go-version v1.7.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.17.11 // indirect github.com/knadh/koanf/maps v0.1.1 // indirect diff --git a/pkg/datadog/go.sum b/pkg/datadog/go.sum index 1b0b76693743..9b3316a54d65 100644 --- a/pkg/datadog/go.sum +++ b/pkg/datadog/go.sum @@ -53,8 +53,6 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= -github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= @@ -160,8 +158,6 @@ go.opentelemetry.io/collector/extension/experimental/storage v0.114.0 h1:hLyX9Uv go.opentelemetry.io/collector/extension/experimental/storage v0.114.0/go.mod h1:WqYRQVJjJLE1rm+y/ks1wPdPRGWePEvE1VO07xm2J2k= go.opentelemetry.io/collector/extension/extensiontest v0.114.0 h1:ibXDms1qrswlvlR6b3d2BeyI8sXUXoFV11yOi9Sop8o= go.opentelemetry.io/collector/extension/extensiontest v0.114.0/go.mod h1:/bOYmqu5yTDfI1bJZUxFqm8ZtmcodpquebiSxiQxtDY= -go.opentelemetry.io/collector/featuregate v1.20.0 h1:Mi7nMy/q52eruI+6jWnMKUOeM55XvwoPnGcdB1++O8c= -go.opentelemetry.io/collector/featuregate v1.20.0/go.mod h1:47xrISO71vJ83LSMm8+yIDsUbKktUp48Ovt7RR6VbRs= go.opentelemetry.io/collector/pdata v1.20.0 h1:ePcwt4bdtISP0loHaE+C9xYoU2ZkIvWv89Fob16o9SM= go.opentelemetry.io/collector/pdata v1.20.0/go.mod h1:Ox1YVLe87cZDB/TL30i4SUz1cA5s6AM6SpFMfY61ICs= go.opentelemetry.io/collector/pdata/pprofile v0.114.0 h1:pUNfTzsI/JUTiE+DScDM4lsrPoxnVNLI2fbTxR/oapo=