Skip to content

Commit

Permalink
[pkg/datadog] Expose TranslatorFromConfig (#36300)
Browse files Browse the repository at this point in the history
#### Description
Expose an API `TranslatorFromConfig` in pkg/datadog that creates a new
metrics translator, so that other repo can reuse rather than duplicating
this API.
  • Loading branch information
songy23 authored Nov 12, 2024
1 parent 2f2ba8c commit 9e374b3
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 56 deletions.
27 changes: 27 additions & 0 deletions .chloggen/pkg-dd-tr-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: enhancement

# 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: "Expose an API `TranslatorFromConfig` that creates a new metrics translator"

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

# (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 only code refactor and has no user-facing impact"

# 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]
12 changes: 0 additions & 12 deletions exporter/datadogexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ var metricExportNativeClientFeatureGate = featuregate.GlobalRegistry().MustRegis
featuregate.WithRegisterDescription("When enabled, metric export in datadogexporter uses native Datadog client APIs instead of Zorkian APIs."),
)

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/"),
)

// noAPMStatsFeatureGate causes the trace consumer to skip APM stats computation.
var noAPMStatsFeatureGate = featuregate.GlobalRegistry().MustRegister(
"exporter.datadogexporter.DisableAPMStats",
Expand All @@ -72,11 +65,6 @@ func isMetricExportV2Enabled() bool {
return metricExportNativeClientFeatureGate.IsEnabled()
}

// isMetricRemappingDisabled returns true if the datadogexporter should generate Datadog-compliant metrics from OpenTelemetry metrics
func isMetricRemappingDisabled() bool {
return metricRemappingDisableddFeatureGate.IsEnabled()
}

func isLogsAgentExporterEnabled() bool {
return logsAgentExporterFeatureGate.IsEnabled()
}
Expand Down
46 changes: 2 additions & 44 deletions exporter/datadogexporter/metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"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/exporter"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
Expand All @@ -30,6 +29,7 @@ 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"
)

type metricsExporter struct {
Expand All @@ -50,48 +50,6 @@ type metricsExporter struct {
getPushTime func() uint64
}

// translatorFromConfig creates a new metrics translator from the exporter
func translatorFromConfig(set component.TelemetrySettings, cfg *Config, attrsTranslator *attributes.Translator, sourceProvider source.Provider, statsOut chan []byte) (*otlpmetrics.Translator, error) {
options := []otlpmetrics.TranslatorOption{
otlpmetrics.WithDeltaTTL(cfg.Metrics.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 cfg.Metrics.HistConfig.SendAggregations {
options = append(options, otlpmetrics.WithHistogramAggregations())
}

if cfg.Metrics.SummaryConfig.Mode == SummaryModeGauges {
options = append(options, otlpmetrics.WithQuantiles())
}

if cfg.Metrics.ExporterConfig.InstrumentationScopeMetadataAsTags {
options = append(options, otlpmetrics.WithInstrumentationScopeMetadataAsTags())
}

options = append(options, otlpmetrics.WithHistogramMode(otlpmetrics.HistogramMode(cfg.Metrics.HistConfig.Mode)))

var numberMode otlpmetrics.NumberMode
switch cfg.Metrics.SumConfig.CumulativeMonotonicMode {
case CumulativeMonotonicSumModeRawValue:
numberMode = otlpmetrics.NumberModeRawValue
case CumulativeMonotonicSumModeToDelta:
numberMode = otlpmetrics.NumberModeCumulativeToDelta
}
options = append(options, otlpmetrics.WithNumberMode(numberMode))
options = append(options, otlpmetrics.WithInitialCumulMonoValueMode(
otlpmetrics.InitialCumulMonoValueMode(cfg.Metrics.SumConfig.InitialCumulativeMonotonicMode)))

options = append(options, otlpmetrics.WithStatsOut(statsOut))
return otlpmetrics.NewTranslator(set, attrsTranslator, options...)
}

func newMetricsExporter(
ctx context.Context,
params exporter.Settings,
Expand All @@ -103,7 +61,7 @@ func newMetricsExporter(
metadataReporter *inframetadata.Reporter,
statsOut chan []byte,
) (*metricsExporter, error) {
tr, err := translatorFromConfig(params.TelemetrySettings, cfg, attrsTranslator, sourceProvider, statsOut)
tr, err := datadogconfig.TranslatorFromConfig(params.TelemetrySettings, cfg.Metrics, attrsTranslator, sourceProvider, statsOut)
if err != nil {
return nil, err
}
Expand Down
59 changes: 59 additions & 0 deletions pkg/datadog/config/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ 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 @@ -206,3 +211,57 @@ type MetricsExporterConfig struct {
// instrumentation scope that created a metric to the metric tags
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) {
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 {
options = append(options, otlpmetrics.WithHistogramAggregations())
}

if mcfg.SummaryConfig.Mode == SummaryModeGauges {
options = append(options, otlpmetrics.WithQuantiles())
}

if mcfg.ExporterConfig.InstrumentationScopeMetadataAsTags {
options = append(options, otlpmetrics.WithInstrumentationScopeMetadataAsTags())
}

options = append(options, otlpmetrics.WithHistogramMode(otlpmetrics.HistogramMode(mcfg.HistConfig.Mode)))

var numberMode otlpmetrics.NumberMode
switch mcfg.SumConfig.CumulativeMonotonicMode {
case CumulativeMonotonicSumModeRawValue:
numberMode = otlpmetrics.NumberModeRawValue
case CumulativeMonotonicSumModeToDelta:
numberMode = otlpmetrics.NumberModeCumulativeToDelta
}
options = append(options, otlpmetrics.WithNumberMode(numberMode))
options = append(options, otlpmetrics.WithInitialCumulMonoValueMode(
otlpmetrics.InitialCumulMonoValueMode(mcfg.SumConfig.InitialCumulativeMonotonicMode)))

options = append(options, otlpmetrics.WithStatsOut(statsOut))
return otlpmetrics.NewTranslator(set, attrsTranslator, options...)
}
14 changes: 14 additions & 0 deletions pkg/datadog/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.22.0

require (
github.com/DataDog/datadog-agent/pkg/util/hostname/validate v0.58.2
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.113.0
go.opentelemetry.io/collector/config/configauth v0.113.0
Expand All @@ -14,23 +16,30 @@ require (
go.opentelemetry.io/collector/config/configtls v1.19.0
go.opentelemetry.io/collector/confmap v1.19.0
go.opentelemetry.io/collector/exporter v0.113.0
go.opentelemetry.io/collector/featuregate v1.19.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.58.2 // indirect
github.com/DataDog/datadog-agent/pkg/util/scrubber v0.58.2 // 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
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
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 All @@ -40,9 +49,12 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.11.1 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
go.opentelemetry.io/collector/client v1.19.0 // indirect
go.opentelemetry.io/collector/config/configcompression v1.19.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.113.0 // indirect
Expand All @@ -55,6 +67,7 @@ require (
go.opentelemetry.io/collector/pdata v1.19.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.113.0 // indirect
go.opentelemetry.io/collector/pipeline v0.113.0 // indirect
go.opentelemetry.io/collector/semconv v0.113.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
Expand All @@ -63,6 +76,7 @@ require (
go.opentelemetry.io/otel/trace v1.31.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
Expand Down
Loading

0 comments on commit 9e374b3

Please sign in to comment.