Skip to content

Commit

Permalink
[pkg/datadog] Refactor the API that provides metrics translator (#36474)
Browse files Browse the repository at this point in the history
#### Description

Refactor the API that provides metrics translator so that it can be used
in the datadog-agent repo.
  • Loading branch information
songy23 authored Nov 21, 2024
1 parent 9309021 commit a401897
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 36 deletions.
27 changes: 27 additions & 0 deletions .chloggen/dd-config-api.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: 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]
25 changes: 23 additions & 2 deletions exporter/datadogexporter/metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
30 changes: 3 additions & 27 deletions pkg/datadog/config/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
4 changes: 1 addition & 3 deletions pkg/datadog/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,14 +16,14 @@ 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
)

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
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions pkg/datadog/go.sum

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

0 comments on commit a401897

Please sign in to comment.