Skip to content

Commit

Permalink
[exporter/datadogexporter] Add source tag to metrics client (open-tel…
Browse files Browse the repository at this point in the history
…emetry#31528)

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
Add `source:{datadogexporter|datadogconnector}` tag to trace agent
telemetry metrics.

**Link to tracking Issue:** <Issue number if applicable>

**Testing:** <Describe what testing was performed and which tests were
added.>

**Documentation:** <Describe the documentation added.>

---------

Co-authored-by: Pablo Baeyens <[email protected]>
  • Loading branch information
liustanley and mx-psi committed Mar 4, 2024
1 parent fe6ff84 commit dac29b1
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 17 deletions.
27 changes: 27 additions & 0 deletions .chloggen/stanley.liu_source-metrics-client-connector.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: 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: []
27 changes: 27 additions & 0 deletions .chloggen/stanley.liu_source-metrics-client.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: 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: []
2 changes: 1 addition & 1 deletion connector/datadogconnector/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions exporter/datadogexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)

Expand Down
23 changes: 17 additions & 6 deletions internal/datadog/metrics_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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))
}
Expand All @@ -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{
Expand All @@ -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
}
Expand Down
16 changes: 8 additions & 8 deletions internal/datadog/metrics_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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))},
},
},
}
Expand All @@ -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))},
},
},
}
Expand All @@ -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))},
},
},
}
Expand All @@ -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))},
},
},
}
Expand All @@ -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))},
},
},
}
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit dac29b1

Please sign in to comment.