From dac29b10fae62bcb6f88a17ff21e888ec8f68cd2 Mon Sep 17 00:00:00 2001 From: Stanley Liu Date: Mon, 4 Mar 2024 03:38:07 -0500 Subject: [PATCH] [exporter/datadogexporter] Add source tag to metrics client (#31528) **Description:** Add `source:{datadogexporter|datadogconnector}` tag to trace agent telemetry metrics. **Link to tracking Issue:** **Testing:** **Documentation:** --------- Co-authored-by: Pablo Baeyens --- ...y.liu_source-metrics-client-connector.yaml | 27 +++++++++++++++++++ .../stanley.liu_source-metrics-client.yaml | 27 +++++++++++++++++++ connector/datadogconnector/factory.go | 2 +- exporter/datadogexporter/factory.go | 4 +-- internal/datadog/metrics_client.go | 23 +++++++++++----- internal/datadog/metrics_client_test.go | 16 +++++------ 6 files changed, 82 insertions(+), 17 deletions(-) create mode 100755 .chloggen/stanley.liu_source-metrics-client-connector.yaml create mode 100755 .chloggen/stanley.liu_source-metrics-client.yaml diff --git a/.chloggen/stanley.liu_source-metrics-client-connector.yaml b/.chloggen/stanley.liu_source-metrics-client-connector.yaml new file mode 100755 index 0000000000000..4f367db7dc229 --- /dev/null +++ b/.chloggen/stanley.liu_source-metrics-client-connector.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: 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: Add `source:datadogconnector` tag to trace agent telemetry metrics generated by the datadogconnector. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [31528] + +# (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: + +# 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: [] diff --git a/.chloggen/stanley.liu_source-metrics-client.yaml b/.chloggen/stanley.liu_source-metrics-client.yaml new file mode 100755 index 0000000000000..6dfe593975e7a --- /dev/null +++ b/.chloggen/stanley.liu_source-metrics-client.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: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: datadogexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Add `source:datadogexporter` tag to trace agent telemetry metrics generated by the datadogexporter. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [31528] + +# (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: + +# 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: [] diff --git a/connector/datadogconnector/factory.go b/connector/datadogconnector/factory.go index d91db0f1a38bf..bd7f2b7c04c2c 100644 --- a/connector/datadogconnector/factory.go +++ b/connector/datadogconnector/factory.go @@ -39,7 +39,7 @@ func createDefaultConfig() component.Config { // defines the consumer type of the connector // we want to consume traces and export metrics therefore define nextConsumer as metrics, consumer is the next component in the pipeline func createTracesToMetricsConnector(_ context.Context, params connector.CreateSettings, cfg component.Config, nextConsumer consumer.Metrics) (connector.Traces, error) { - metricsClient := datadog.InitializeMetricClient(params.MeterProvider) + metricsClient := datadog.InitializeMetricClient(params.MeterProvider, datadog.ConnectorSourceTag) timingReporter := timing.New(metricsClient) c, err := newTraceToMetricConnector(params.TelemetrySettings, cfg, nextConsumer, metricsClient, timingReporter) if err != nil { diff --git a/exporter/datadogexporter/factory.go b/exporter/datadogexporter/factory.go index cc338aedecfb2..cb9bcaf55697a 100644 --- a/exporter/datadogexporter/factory.go +++ b/exporter/datadogexporter/factory.go @@ -142,7 +142,7 @@ func (f *factory) StopReporter() { } func (f *factory) TraceAgent(ctx context.Context, params exporter.CreateSettings, cfg *Config, sourceProvider source.Provider, attrsTranslator *attributes.Translator) (*agent.Agent, error) { - agnt, err := newTraceAgent(ctx, params, cfg, sourceProvider, datadog.InitializeMetricClient(params.MeterProvider), attrsTranslator) + agnt, err := newTraceAgent(ctx, params, cfg, sourceProvider, datadog.InitializeMetricClient(params.MeterProvider, datadog.ExporterSourceTag), attrsTranslator) if err != nil { return nil, err } @@ -299,7 +299,7 @@ func (f *factory) createMetricsExporter( return nil, err } statsToAgent := make(chan *pb.StatsPayload) - metricsClient := datadog.InitializeMetricClient(set.MeterProvider) + metricsClient := datadog.InitializeMetricClient(set.MeterProvider, datadog.ExporterSourceTag) timingReporter := timing.New(metricsClient) statsWriter := writer.NewStatsWriter(acfg, statsToAgent, telemetry.NewNoopCollector(), metricsClient, timingReporter) diff --git a/internal/datadog/metrics_client.go b/internal/datadog/metrics_client.go index dec569d25d394..f2c9dcb474490 100644 --- a/internal/datadog/metrics_client.go +++ b/internal/datadog/metrics_client.go @@ -14,17 +14,24 @@ import ( "go.opentelemetry.io/otel/metric" ) +const ( + ExporterSourceTag = "datadogexporter" + ConnectorSourceTag = "datadogconnector" +) + type metricsClient struct { meter metric.Meter gauges map[string]float64 mutex sync.Mutex + source string } // InitializeMetricClient using a meter provider. -func InitializeMetricClient(mp metric.MeterProvider) statsd.ClientInterface { +func InitializeMetricClient(mp metric.MeterProvider, source string) statsd.ClientInterface { return &metricsClient{ meter: mp.Meter("datadog"), gauges: make(map[string]float64), + source: source, } } @@ -38,7 +45,7 @@ func (m *metricsClient) Gauge(name string, value float64, tags []string, _ float } m.gauges[name] = value _, err := m.meter.Float64ObservableGauge(name, metric.WithFloat64Callback(func(_ context.Context, f metric.Float64Observer) error { - attr := attributeFromTags(tags) + attr := m.attributeFromTags(tags) if v, ok := m.gauges[name]; ok { f.Observe(v, metric.WithAttributeSet(attr)) } @@ -55,13 +62,17 @@ func (m *metricsClient) Count(name string, value int64, tags []string, _ float64 if err != nil { return err } - attr := attributeFromTags(tags) + attr := m.attributeFromTags(tags) counter.Add(context.Background(), value, metric.WithAttributeSet(attr)) return nil } -func attributeFromTags(tags []string) attribute.Set { - attr := make([]attribute.KeyValue, 0, len(tags)) +func (m *metricsClient) attributeFromTags(tags []string) attribute.Set { + attr := make([]attribute.KeyValue, 0, len(tags)+1) + attr = append(attr, attribute.KeyValue{ + Key: "source", + Value: attribute.StringValue(m.source), + }) for _, t := range tags { kv := strings.Split(t, ":") attr = append(attr, attribute.KeyValue{ @@ -77,7 +88,7 @@ func (m *metricsClient) Histogram(name string, value float64, tags []string, _ f if err != nil { return err } - attr := attributeFromTags(tags) + attr := m.attributeFromTags(tags) hist.Record(context.Background(), value, metric.WithAttributeSet(attr)) return nil } diff --git a/internal/datadog/metrics_client_test.go b/internal/datadog/metrics_client_test.go index 11a8898486bc3..fe697ab625d02 100644 --- a/internal/datadog/metrics_client_test.go +++ b/internal/datadog/metrics_client_test.go @@ -20,7 +20,7 @@ import ( func setupMetricClient() (*metric.ManualReader, statsd.ClientInterface, timing.Reporter) { reader := metric.NewManualReader() meterProvider := metric.NewMeterProvider(metric.WithReader(reader)) - metricClient := InitializeMetricClient(meterProvider) + metricClient := InitializeMetricClient(meterProvider, ExporterSourceTag) timingReporter := timing.New(metricClient) return reader, metricClient, timingReporter } @@ -40,7 +40,7 @@ func TestGauge(t *testing.T) { Name: "test_gauge", Data: metricdata.Gauge[float64]{ DataPoints: []metricdata.DataPoint[float64]{ - {Value: 1, Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("service", "otelcol"))}, + {Value: 1, Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("service", "otelcol"), attribute.String("source", ExporterSourceTag))}, }, }, } @@ -65,7 +65,7 @@ func TestGaugeMultiple(t *testing.T) { Name: "test_gauge", Data: metricdata.Gauge[float64]{ DataPoints: []metricdata.DataPoint[float64]{ - {Value: 2, Attributes: attribute.NewSet(attribute.String("otlp", "true"))}, + {Value: 2, Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("source", ExporterSourceTag))}, }, }, } @@ -89,7 +89,7 @@ func TestCount(t *testing.T) { Temporality: metricdata.CumulativeTemporality, IsMonotonic: true, DataPoints: []metricdata.DataPoint[int64]{ - {Value: 1, Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("service", "otelcol"))}, + {Value: 1, Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("service", "otelcol"), attribute.String("source", ExporterSourceTag))}, }, }, } @@ -112,7 +112,7 @@ func TestCount(t *testing.T) { Temporality: metricdata.CumulativeTemporality, IsMonotonic: true, DataPoints: []metricdata.DataPoint[int64]{ - {Value: 6, Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("service", "otelcol"))}, + {Value: 6, Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("service", "otelcol"), attribute.String("source", ExporterSourceTag))}, }, }, } @@ -125,7 +125,7 @@ func TestCount(t *testing.T) { Temporality: metricdata.CumulativeTemporality, IsMonotonic: true, DataPoints: []metricdata.DataPoint[int64]{ - {Value: 3, Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("service", "otelcol"))}, + {Value: 3, Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("service", "otelcol"), attribute.String("source", ExporterSourceTag))}, }, }, } @@ -148,7 +148,7 @@ func TestHistogram(t *testing.T) { Data: metricdata.Histogram[float64]{ Temporality: metricdata.CumulativeTemporality, DataPoints: []metricdata.HistogramDataPoint[float64]{{ - Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("service", "otelcol")), + Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("service", "otelcol"), attribute.String("source", ExporterSourceTag)), Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000}, BucketCounts: []uint64{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Count: 1, @@ -177,7 +177,7 @@ func TestTiming(t *testing.T) { Data: metricdata.Histogram[float64]{ Temporality: metricdata.CumulativeTemporality, DataPoints: []metricdata.HistogramDataPoint[float64]{{ - Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("service", "otelcol")), + Attributes: attribute.NewSet(attribute.String("otlp", "true"), attribute.String("service", "otelcol"), attribute.String("source", ExporterSourceTag)), Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000}, BucketCounts: []uint64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, Count: 1,