From 090532052939e454287cb2ab09ab3927650a0eca Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Wed, 26 May 2021 18:26:04 +0200 Subject: [PATCH] Deduplicate hosts for which we send running metrics --- exporter/datadogexporter/metrics_translator.go | 14 +++++++++----- .../datadogexporter/metrics_translator_test.go | 2 +- exporter/datadogexporter/translate_traces.go | 14 +++++++++----- exporter/datadogexporter/translate_traces_test.go | 2 +- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/exporter/datadogexporter/metrics_translator.go b/exporter/datadogexporter/metrics_translator.go index 3c6580b51391..f277890315e5 100644 --- a/exporter/datadogexporter/metrics_translator.go +++ b/exporter/datadogexporter/metrics_translator.go @@ -255,6 +255,7 @@ func mapHistogramMetrics(name string, slice pdata.HistogramDataPointSlice, bucke func mapMetrics(logger *zap.Logger, cfg config.MetricsConfig, prevPts *ttlmap.TTLMap, fallbackHost string, md pdata.Metrics, buildInfo component.BuildInfo) (series []datadog.Metric, droppedTimeSeries int) { pushTime := uint64(time.Now().UTC().UnixNano()) rms := md.ResourceMetrics() + seenHosts := make(map[string]struct{}) for i := 0; i < rms.Len(); i++ { rm := rms.At(i) @@ -270,11 +271,7 @@ func mapMetrics(logger *zap.Logger, cfg config.MetricsConfig, prevPts *ttlmap.TT if !ok { host = fallbackHost } - - // Report the host as running - runningMetric := metrics.DefaultMetrics("metrics", host, pushTime, buildInfo) - - series = append(series, runningMetric...) + seenHosts[host] = struct{}{} ilms := rm.InstrumentationLibraryMetrics() for j := 0; j < ilms.Len(); j++ { @@ -317,5 +314,12 @@ func mapMetrics(logger *zap.Logger, cfg config.MetricsConfig, prevPts *ttlmap.TT } } } + + for host := range seenHosts { + // Report the host as running + runningMetric := metrics.DefaultMetrics("metrics", host, pushTime, buildInfo) + series = append(series, runningMetric...) + } + return } diff --git a/exporter/datadogexporter/metrics_translator_test.go b/exporter/datadogexporter/metrics_translator_test.go index e2b9952023cf..5946656f5f30 100644 --- a/exporter/datadogexporter/metrics_translator_test.go +++ b/exporter/datadogexporter/metrics_translator_test.go @@ -556,7 +556,7 @@ func TestRunningMetrics(t *testing.T) { assert.ElementsMatch(t, runningHostnames, - []string{"fallbackHostname", "resource-hostname-1", "resource-hostname-1", "resource-hostname-2"}, + []string{"fallbackHostname", "resource-hostname-1", "resource-hostname-2"}, ) } diff --git a/exporter/datadogexporter/translate_traces.go b/exporter/datadogexporter/translate_traces.go index 0030a87bd3ac..3e85e4bb1262 100644 --- a/exporter/datadogexporter/translate_traces.go +++ b/exporter/datadogexporter/translate_traces.go @@ -73,7 +73,8 @@ func convertToDatadogTd(td pdata.Traces, fallbackHost string, cfg *config.Config var traces []*pb.TracePayload - var runningMetrics []datadog.Metric + seenHosts := make(map[string]struct{}) + var series []datadog.Metric pushTime := pdata.TimestampFromTime(time.Now()) for i := 0; i < resourceSpans.Len(); i++ { rs := resourceSpans.At(i) @@ -81,15 +82,18 @@ func convertToDatadogTd(td pdata.Traces, fallbackHost string, cfg *config.Config if !ok { host = fallbackHost } - + seenHosts[host] = struct{}{} payload := resourceSpansToDatadogSpans(rs, host, cfg, blk) traces = append(traces, &payload) + } - ms := metrics.DefaultMetrics("traces", host, uint64(pushTime), buildInfo) - runningMetrics = append(runningMetrics, ms...) + for host := range seenHosts { + // Report the host as running + runningMetric := metrics.DefaultMetrics("traces", host, uint64(pushTime), buildInfo) + series = append(series, runningMetric...) } - return traces, runningMetrics + return traces, series } func aggregateTracePayloadsByEnv(tracePayloads []*pb.TracePayload) []*pb.TracePayload { diff --git a/exporter/datadogexporter/translate_traces_test.go b/exporter/datadogexporter/translate_traces_test.go index 28193b3ccd69..be2108ae479c 100644 --- a/exporter/datadogexporter/translate_traces_test.go +++ b/exporter/datadogexporter/translate_traces_test.go @@ -191,7 +191,7 @@ func TestRunningTraces(t *testing.T) { assert.ElementsMatch(t, runningHostnames, - []string{"resource-hostname-1", "resource-hostname-1", "resource-hostname-2", "fallbackHost"}, + []string{"resource-hostname-1", "resource-hostname-2", "fallbackHost"}, ) }