123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package datadogexporter
- import (
- "bytes"
- "compress/gzip"
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "sync"
- "testing"
- "time"
- "github.com/DataDog/agent-payload/v5/gogen"
- pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace"
- "github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
- "github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata"
- "github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata/payload"
- "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/source"
- "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/config/confignet"
- "go.opentelemetry.io/collector/exporter/exportertest"
- "go.opentelemetry.io/collector/pdata/pcommon"
- "go.opentelemetry.io/collector/pdata/pmetric"
- conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
- "go.uber.org/zap"
- "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/testutil"
- )
- func TestNewExporter(t *testing.T) {
- if !isMetricExportV2Enabled() {
- require.NoError(t, enableNativeMetricExport())
- defer require.NoError(t, enableZorkianMetricExport())
- }
- server := testutil.DatadogServerMock()
- defer server.Close()
- cfg := &Config{
- API: APIConfig{
- Key: "ddog_32_characters_long_api_key1",
- },
- Metrics: MetricsConfig{
- TCPAddr: confignet.TCPAddr{
- Endpoint: server.URL,
- },
- DeltaTTL: 3600,
- HistConfig: HistogramConfig{
- Mode: HistogramModeDistributions,
- SendAggregations: false,
- },
- SumConfig: SumConfig{
- CumulativeMonotonicMode: CumulativeMonotonicSumModeToDelta,
- },
- },
- }
- params := exportertest.NewNopCreateSettings()
- f := NewFactory()
- // The client should have been created correctly
- exp, err := f.CreateMetricsExporter(context.Background(), params, cfg)
- require.NoError(t, err)
- assert.NotNil(t, exp)
- testMetrics := pmetric.NewMetrics()
- testutil.TestMetrics.CopyTo(testMetrics)
- err = exp.ConsumeMetrics(context.Background(), testMetrics)
- require.NoError(t, err)
- assert.Equal(t, len(server.MetadataChan), 0)
- cfg.HostMetadata.Enabled = true
- cfg.HostMetadata.HostnameSource = HostnameSourceFirstResource
- testMetrics = pmetric.NewMetrics()
- testutil.TestMetrics.CopyTo(testMetrics)
- err = exp.ConsumeMetrics(context.Background(), testMetrics)
- require.NoError(t, err)
- body := <-server.MetadataChan
- var recvMetadata payload.HostMetadata
- err = json.Unmarshal(body, &recvMetadata)
- require.NoError(t, err)
- assert.Equal(t, recvMetadata.InternalHostname, "custom-hostname")
- }
- func Test_metricsExporter_PushMetricsData(t *testing.T) {
- if !isMetricExportV2Enabled() {
- require.NoError(t, enableNativeMetricExport())
- t.Cleanup(func() { require.NoError(t, enableZorkianMetricExport()) })
- }
- attrs := map[string]string{
- conventions.AttributeDeploymentEnvironment: "dev",
- "custom_attribute": "custom_value",
- }
- tests := []struct {
- metrics pmetric.Metrics
- source source.Source
- hostTags []string
- histogramMode HistogramMode
- expectedSeries map[string]any
- expectedSketchPayload *gogen.SketchPayload
- expectedErr error
- expectedStats []*pb.ClientStatsPayload
- }{
- {
- metrics: createTestMetrics(attrs),
- source: source.Source{
- Kind: source.HostnameKind,
- Identifier: "test-host",
- },
- histogramMode: HistogramModeNoBuckets,
- hostTags: []string{"key1:value1", "key2:value2"},
- expectedErr: errors.New("no buckets mode and no send count sum are incompatible"),
- },
- {
- metrics: createTestMetrics(attrs),
- source: source.Source{
- Kind: source.HostnameKind,
- Identifier: "test-host",
- },
- histogramMode: HistogramModeCounters,
- hostTags: []string{"key1:value1", "key2:value2"},
- expectedSeries: map[string]any{
- "series": []any{
- map[string]any{
- "metric": "int.gauge",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(222)}},
- "type": float64(datadogV2.METRICINTAKETYPE_GAUGE),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "otel.system.filesystem.utilization",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(333)}},
- "type": float64(datadogV2.METRICINTAKETYPE_GAUGE),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "double.histogram.bucket",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(2)}},
- "type": float64(datadogV2.METRICINTAKETYPE_COUNT),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"lower_bound:-inf", "upper_bound:0", "env:dev"},
- },
- map[string]any{
- "metric": "double.histogram.bucket",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(18)}},
- "type": float64(datadogV2.METRICINTAKETYPE_COUNT),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"lower_bound:0", "upper_bound:inf", "env:dev"},
- },
- map[string]any{
- "metric": "system.disk.in_use",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(333)}},
- "type": float64(datadogV2.METRICINTAKETYPE_GAUGE),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "otel.datadog_exporter.metrics.running",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(1)}},
- "type": float64(datadogV2.METRICINTAKETYPE_GAUGE),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"version:latest", "command:otelcol"},
- },
- },
- },
- },
- {
- metrics: createTestMetrics(attrs),
- source: source.Source{
- Kind: source.HostnameKind,
- Identifier: "test-host",
- },
- histogramMode: HistogramModeDistributions,
- hostTags: []string{"key1:value1", "key2:value2"},
- expectedSeries: map[string]any{
- "series": []any{
- map[string]any{
- "metric": "int.gauge",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(222)}},
- "type": float64(datadogV2.METRICINTAKETYPE_GAUGE),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "otel.system.filesystem.utilization",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(333)}},
- "type": float64(datadogV2.METRICINTAKETYPE_GAUGE),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "system.disk.in_use",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(333)}},
- "type": float64(datadogV2.METRICINTAKETYPE_GAUGE),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "otel.datadog_exporter.metrics.running",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(1)}},
- "type": float64(datadogV2.METRICINTAKETYPE_GAUGE),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"version:latest", "command:otelcol"},
- },
- },
- },
- expectedSketchPayload: &gogen.SketchPayload{
- Sketches: []gogen.SketchPayload_Sketch{
- {
- Metric: "double.histogram",
- Host: "test-host",
- Tags: []string{"env:dev"},
- Dogsketches: []gogen.SketchPayload_Sketch_Dogsketch{
- {
- Cnt: 20,
- Avg: 0.3,
- Sum: 6,
- K: []int32{0},
- N: []uint32{20},
- },
- },
- },
- },
- },
- },
- {
- metrics: createTestMetrics(attrs),
- source: source.Source{
- Kind: source.AWSECSFargateKind,
- Identifier: "task_arn",
- },
- histogramMode: HistogramModeCounters,
- hostTags: []string{"key1:value1", "key2:value2"},
- expectedSeries: map[string]any{
- "series": []any{
- map[string]any{
- "metric": "int.gauge",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(222)}},
- "type": float64(datadogV2.METRICINTAKETYPE_GAUGE),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"env:dev", "key1:value1", "key2:value2"},
- },
- map[string]any{
- "metric": "otel.system.filesystem.utilization",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(333)}},
- "type": float64(datadogV2.METRICINTAKETYPE_GAUGE),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"env:dev", "key1:value1", "key2:value2"},
- },
- map[string]any{
- "metric": "double.histogram.bucket",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(2)}},
- "type": float64(datadogV2.METRICINTAKETYPE_COUNT),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"lower_bound:-inf", "upper_bound:0", "env:dev", "key1:value1", "key2:value2"},
- },
- map[string]any{
- "metric": "double.histogram.bucket",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(18)}},
- "type": float64(datadogV2.METRICINTAKETYPE_COUNT),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"lower_bound:0", "upper_bound:inf", "env:dev", "key1:value1", "key2:value2"},
- },
- map[string]any{
- "metric": "system.disk.in_use",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(333)}},
- "type": float64(datadogV2.METRICINTAKETYPE_GAUGE),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"env:dev", "key1:value1", "key2:value2"},
- },
- map[string]any{
- "metric": "otel.datadog_exporter.metrics.running",
- "points": []any{map[string]any{"timestamp": float64(0), "value": float64(1)}},
- "type": float64(datadogV2.METRICINTAKETYPE_GAUGE),
- "resources": []any{map[string]any{"name": "test-host", "type": "host"}},
- "tags": []any{"version:latest", "command:otelcol", "key1:value1", "key2:value2"},
- },
- },
- },
- },
- }
- for _, tt := range tests {
- t.Run(fmt.Sprintf("kind=%s,histgramMode=%s", tt.source.Kind, tt.histogramMode), func(t *testing.T) {
- seriesRecorder := &testutil.HTTPRequestRecorder{Pattern: testutil.MetricV2Endpoint}
- sketchRecorder := &testutil.HTTPRequestRecorder{Pattern: testutil.SketchesMetricEndpoint}
- server := testutil.DatadogServerMock(
- seriesRecorder.HandlerFunc,
- sketchRecorder.HandlerFunc,
- )
- defer server.Close()
- var (
- once sync.Once
- statsRecorder testutil.MockStatsProcessor
- )
- pusher := newTestPusher(t)
- reporter, err := inframetadata.NewReporter(zap.NewNop(), pusher, 1*time.Second)
- require.NoError(t, err)
- exp, err := newMetricsExporter(
- context.Background(),
- exportertest.NewNopCreateSettings(),
- newTestConfig(t, server.URL, tt.hostTags, tt.histogramMode),
- &once,
- &testutil.MockSourceProvider{Src: tt.source},
- &statsRecorder,
- reporter,
- )
- if tt.expectedErr == nil {
- assert.NoError(t, err, "unexpected error")
- } else {
- assert.Equal(t, tt.expectedErr, err, "expected error doesn't match")
- return
- }
- exp.getPushTime = func() uint64 { return 0 }
- err = exp.PushMetricsData(context.Background(), tt.metrics)
- if tt.expectedErr == nil {
- assert.NoError(t, err, "unexpected error")
- } else {
- assert.Equal(t, tt.expectedErr, err, "expected error doesn't match")
- return
- }
- if len(tt.expectedSeries) == 0 {
- assert.Nil(t, seriesRecorder.ByteBody)
- } else {
- assert.Equal(t, "gzip", seriesRecorder.Header.Get("Accept-Encoding"))
- assert.Equal(t, "application/json", seriesRecorder.Header.Get("Content-Type"))
- assert.Equal(t, "gzip", seriesRecorder.Header.Get("Content-Encoding"))
- assert.Equal(t, "otelcol/latest", seriesRecorder.Header.Get("User-Agent"))
- assert.NoError(t, err)
- buf := bytes.NewBuffer(seriesRecorder.ByteBody)
- reader, err := gzip.NewReader(buf)
- assert.NoError(t, err)
- dec := json.NewDecoder(reader)
- var actual map[string]any
- assert.NoError(t, dec.Decode(&actual))
- assert.EqualValues(t, tt.expectedSeries, actual)
- }
- if tt.expectedSketchPayload == nil {
- assert.Nil(t, sketchRecorder.ByteBody)
- } else {
- assert.Equal(t, "gzip", sketchRecorder.Header.Get("Accept-Encoding"))
- assert.Equal(t, "application/x-protobuf", sketchRecorder.Header.Get("Content-Type"))
- assert.Equal(t, "otelcol/latest", sketchRecorder.Header.Get("User-Agent"))
- expected, err := tt.expectedSketchPayload.Marshal()
- assert.NoError(t, err)
- assert.Equal(t, expected, sketchRecorder.ByteBody)
- }
- if tt.expectedStats == nil {
- assert.Len(t, statsRecorder.In, 0)
- } else {
- assert.ElementsMatch(t, statsRecorder.In, tt.expectedStats)
- }
- })
- }
- }
- func TestNewExporter_Zorkian(t *testing.T) {
- if isMetricExportV2Enabled() {
- require.NoError(t, enableZorkianMetricExport())
- defer require.NoError(t, enableNativeMetricExport())
- }
- server := testutil.DatadogServerMock()
- defer server.Close()
- cfg := &Config{
- API: APIConfig{
- Key: "ddog_32_characters_long_api_key1",
- },
- Metrics: MetricsConfig{
- TCPAddr: confignet.TCPAddr{
- Endpoint: server.URL,
- },
- DeltaTTL: 3600,
- HistConfig: HistogramConfig{
- Mode: HistogramModeDistributions,
- SendAggregations: false,
- },
- SumConfig: SumConfig{
- CumulativeMonotonicMode: CumulativeMonotonicSumModeToDelta,
- },
- },
- }
- params := exportertest.NewNopCreateSettings()
- f := NewFactory()
- // The client should have been created correctly
- exp, err := f.CreateMetricsExporter(context.Background(), params, cfg)
- require.NoError(t, err)
- assert.NotNil(t, exp)
- testMetrics := pmetric.NewMetrics()
- testutil.TestMetrics.CopyTo(testMetrics)
- err = exp.ConsumeMetrics(context.Background(), testMetrics)
- require.NoError(t, err)
- assert.Equal(t, len(server.MetadataChan), 0)
- cfg.HostMetadata.Enabled = true
- cfg.HostMetadata.HostnameSource = HostnameSourceFirstResource
- testMetrics = pmetric.NewMetrics()
- testutil.TestMetrics.CopyTo(testMetrics)
- err = exp.ConsumeMetrics(context.Background(), testMetrics)
- require.NoError(t, err)
- body := <-server.MetadataChan
- var recvMetadata payload.HostMetadata
- err = json.Unmarshal(body, &recvMetadata)
- require.NoError(t, err)
- assert.Equal(t, recvMetadata.InternalHostname, "custom-hostname")
- }
- func Test_metricsExporter_PushMetricsData_Zorkian(t *testing.T) {
- if isMetricExportV2Enabled() {
- require.NoError(t, enableZorkianMetricExport())
- t.Cleanup(func() { require.NoError(t, enableNativeMetricExport()) })
- }
- attrs := map[string]string{
- conventions.AttributeDeploymentEnvironment: "dev",
- "custom_attribute": "custom_value",
- }
- tests := []struct {
- metrics pmetric.Metrics
- source source.Source
- hostTags []string
- histogramMode HistogramMode
- expectedSeries map[string]any
- expectedSketchPayload *gogen.SketchPayload
- expectedErr error
- expectedStats []*pb.ClientStatsPayload
- }{
- {
- metrics: createTestMetrics(attrs),
- source: source.Source{
- Kind: source.HostnameKind,
- Identifier: "test-host",
- },
- histogramMode: HistogramModeNoBuckets,
- hostTags: []string{"key1:value1", "key2:value2"},
- expectedErr: errors.New("no buckets mode and no send count sum are incompatible"),
- },
- {
- metrics: createTestMetrics(attrs),
- source: source.Source{
- Kind: source.HostnameKind,
- Identifier: "test-host",
- },
- histogramMode: HistogramModeCounters,
- hostTags: []string{"key1:value1", "key2:value2"},
- expectedSeries: map[string]any{
- "series": []any{
- map[string]any{
- "metric": "int.gauge",
- "points": []any{[]any{float64(0), float64(222)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "otel.system.filesystem.utilization",
- "points": []any{[]any{float64(0), float64(333)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "double.histogram.bucket",
- "points": []any{[]any{float64(0), float64(2)}},
- "type": "count",
- "host": "test-host",
- "tags": []any{"lower_bound:-inf", "upper_bound:0", "env:dev"},
- },
- map[string]any{
- "metric": "double.histogram.bucket",
- "points": []any{[]any{float64(0), float64(18)}},
- "type": "count",
- "host": "test-host",
- "tags": []any{"lower_bound:0", "upper_bound:inf", "env:dev"},
- },
- map[string]any{
- "metric": "system.disk.in_use",
- "points": []any{[]any{float64(0), float64(333)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "otel.datadog_exporter.metrics.running",
- "points": []any{[]any{float64(0), float64(1)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"version:latest", "command:otelcol"},
- },
- },
- },
- },
- {
- metrics: createTestMetrics(attrs),
- source: source.Source{
- Kind: source.HostnameKind,
- Identifier: "test-host",
- },
- histogramMode: HistogramModeDistributions,
- hostTags: []string{"key1:value1", "key2:value2"},
- expectedSeries: map[string]any{
- "series": []any{
- map[string]any{
- "metric": "int.gauge",
- "points": []any{[]any{float64(0), float64(222)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "otel.system.filesystem.utilization",
- "points": []any{[]any{float64(0), float64(333)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "system.disk.in_use",
- "points": []any{[]any{float64(0), float64(333)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "otel.datadog_exporter.metrics.running",
- "points": []any{[]any{float64(0), float64(1)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"version:latest", "command:otelcol"},
- },
- },
- },
- expectedSketchPayload: &gogen.SketchPayload{
- Sketches: []gogen.SketchPayload_Sketch{
- {
- Metric: "double.histogram",
- Host: "test-host",
- Tags: []string{"env:dev"},
- Dogsketches: []gogen.SketchPayload_Sketch_Dogsketch{
- {
- Cnt: 20,
- Avg: 0.3,
- Sum: 6,
- K: []int32{0},
- N: []uint32{20},
- },
- },
- },
- },
- },
- },
- {
- metrics: createTestMetrics(attrs),
- source: source.Source{
- Kind: source.AWSECSFargateKind,
- Identifier: "task_arn",
- },
- histogramMode: HistogramModeCounters,
- hostTags: []string{"key1:value1", "key2:value2"},
- expectedSeries: map[string]any{
- "series": []any{
- map[string]any{
- "metric": "int.gauge",
- "points": []any{[]any{float64(0), float64(222)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"env:dev", "key1:value1", "key2:value2"},
- },
- map[string]any{
- "metric": "otel.system.filesystem.utilization",
- "points": []any{[]any{float64(0), float64(333)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"env:dev", "key1:value1", "key2:value2"},
- },
- map[string]any{
- "metric": "double.histogram.bucket",
- "points": []any{[]any{float64(0), float64(2)}},
- "type": "count",
- "host": "test-host",
- "tags": []any{"lower_bound:-inf", "upper_bound:0", "env:dev", "key1:value1", "key2:value2"},
- },
- map[string]any{
- "metric": "double.histogram.bucket",
- "points": []any{[]any{float64(0), float64(18)}},
- "type": "count",
- "host": "test-host",
- "tags": []any{"lower_bound:0", "upper_bound:inf", "env:dev", "key1:value1", "key2:value2"},
- },
- map[string]any{
- "metric": "system.disk.in_use",
- "points": []any{[]any{float64(0), float64(333)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"env:dev", "key1:value1", "key2:value2"},
- },
- map[string]any{
- "metric": "otel.datadog_exporter.metrics.running",
- "points": []any{[]any{float64(0), float64(1)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"version:latest", "command:otelcol", "key1:value1", "key2:value2"},
- },
- },
- },
- expectedSketchPayload: nil,
- expectedErr: nil,
- },
- {
- metrics: createTestMetricsWithStats(),
- source: source.Source{
- Kind: source.HostnameKind,
- Identifier: "test-host",
- },
- histogramMode: HistogramModeDistributions,
- hostTags: []string{"key1:value1", "key2:value2"},
- expectedSeries: map[string]any{
- "series": []any{
- map[string]any{
- "metric": "int.gauge",
- "points": []any{[]any{float64(0), float64(222)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "otel.system.filesystem.utilization",
- "points": []any{[]any{float64(0), float64(333)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "system.disk.in_use",
- "points": []any{[]any{float64(0), float64(333)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"env:dev"},
- },
- map[string]any{
- "metric": "otel.datadog_exporter.metrics.running",
- "points": []any{[]any{float64(0), float64(1)}},
- "type": "gauge",
- "host": "test-host",
- "tags": []any{"version:latest", "command:otelcol"},
- },
- },
- },
- expectedSketchPayload: &gogen.SketchPayload{
- Sketches: []gogen.SketchPayload_Sketch{
- {
- Metric: "double.histogram",
- Host: "test-host",
- Tags: []string{"env:dev"},
- Dogsketches: []gogen.SketchPayload_Sketch_Dogsketch{
- {
- Cnt: 20,
- Avg: 0.3,
- Sum: 6,
- K: []int32{0},
- N: []uint32{20},
- },
- },
- },
- },
- },
- expectedStats: testutil.StatsPayloads,
- },
- }
- for _, tt := range tests {
- t.Run(fmt.Sprintf("kind=%s,histgramMode=%s", tt.source.Kind, tt.histogramMode), func(t *testing.T) {
- seriesRecorder := &testutil.HTTPRequestRecorder{Pattern: testutil.MetricV1Endpoint}
- sketchRecorder := &testutil.HTTPRequestRecorder{Pattern: testutil.SketchesMetricEndpoint}
- server := testutil.DatadogServerMock(
- seriesRecorder.HandlerFunc,
- sketchRecorder.HandlerFunc,
- )
- defer server.Close()
- var (
- once sync.Once
- statsRecorder testutil.MockStatsProcessor
- )
- pusher := newTestPusher(t)
- reporter, err := inframetadata.NewReporter(zap.NewNop(), pusher, 1*time.Second)
- require.NoError(t, err)
- exp, err := newMetricsExporter(
- context.Background(),
- exportertest.NewNopCreateSettings(),
- newTestConfig(t, server.URL, tt.hostTags, tt.histogramMode),
- &once,
- &testutil.MockSourceProvider{Src: tt.source},
- &statsRecorder,
- reporter,
- )
- if tt.expectedErr == nil {
- assert.NoError(t, err, "unexpected error")
- } else {
- assert.Equal(t, tt.expectedErr, err, "expected error doesn't match")
- return
- }
- exp.getPushTime = func() uint64 { return 0 }
- err = exp.PushMetricsData(context.Background(), tt.metrics)
- if tt.expectedErr == nil {
- assert.NoError(t, err, "unexpected error")
- } else {
- assert.Equal(t, tt.expectedErr, err, "expected error doesn't match")
- return
- }
- if len(tt.expectedSeries) == 0 {
- assert.Nil(t, seriesRecorder.ByteBody)
- } else {
- assert.Equal(t, "gzip", seriesRecorder.Header.Get("Accept-Encoding"))
- assert.Equal(t, "application/json", seriesRecorder.Header.Get("Content-Type"))
- assert.Equal(t, "otelcol/latest", seriesRecorder.Header.Get("User-Agent"))
- assert.NoError(t, err)
- var actual map[string]any
- assert.NoError(t, json.Unmarshal(seriesRecorder.ByteBody, &actual))
- assert.EqualValues(t, tt.expectedSeries, actual)
- }
- if tt.expectedSketchPayload == nil {
- assert.Nil(t, sketchRecorder.ByteBody)
- } else {
- assert.Equal(t, "gzip", sketchRecorder.Header.Get("Accept-Encoding"))
- assert.Equal(t, "application/x-protobuf", sketchRecorder.Header.Get("Content-Type"))
- assert.Equal(t, "otelcol/latest", sketchRecorder.Header.Get("User-Agent"))
- expected, err := tt.expectedSketchPayload.Marshal()
- assert.NoError(t, err)
- assert.Equal(t, expected, sketchRecorder.ByteBody)
- }
- if tt.expectedStats == nil {
- assert.Len(t, statsRecorder.In, 0)
- } else {
- assert.ElementsMatch(t, statsRecorder.In, tt.expectedStats)
- }
- })
- }
- }
- func createTestMetricsWithStats() pmetric.Metrics {
- md := createTestMetrics(map[string]string{
- conventions.AttributeDeploymentEnvironment: "dev",
- "custom_attribute": "custom_value",
- })
- dest := md.ResourceMetrics()
- logger, _ := zap.NewDevelopment()
- trans, err := metrics.NewTranslator(logger)
- if err != nil {
- panic(err)
- }
- src := trans.
- StatsPayloadToMetrics(&pb.StatsPayload{Stats: testutil.StatsPayloads}).
- ResourceMetrics()
- src.MoveAndAppendTo(dest)
- return md
- }
- func createTestMetrics(additionalAttributes map[string]string) pmetric.Metrics {
- const (
- host = "test-host"
- name = "test-metrics"
- version = "v0.0.1"
- )
- md := pmetric.NewMetrics()
- rms := md.ResourceMetrics()
- rm := rms.AppendEmpty()
- attrs := rm.Resource().Attributes()
- attrs.PutStr("datadog.host.name", host)
- for attr, val := range additionalAttributes {
- attrs.PutStr(attr, val)
- }
- ilms := rm.ScopeMetrics()
- ilm := ilms.AppendEmpty()
- ilm.Scope().SetName(name)
- ilm.Scope().SetVersion(version)
- metricsArray := ilm.Metrics()
- metricsArray.AppendEmpty() // first one is TypeNone to test that it's ignored
- // IntGauge
- met := metricsArray.AppendEmpty()
- met.SetName("int.gauge")
- dpsInt := met.SetEmptyGauge().DataPoints()
- dpInt := dpsInt.AppendEmpty()
- dpInt.SetTimestamp(seconds(0))
- dpInt.SetIntValue(222)
- // host metric
- met = metricsArray.AppendEmpty()
- met.SetName("system.filesystem.utilization")
- dpsInt = met.SetEmptyGauge().DataPoints()
- dpInt = dpsInt.AppendEmpty()
- dpInt.SetTimestamp(seconds(0))
- dpInt.SetIntValue(333)
- // Histogram (delta)
- met = metricsArray.AppendEmpty()
- met.SetName("double.histogram")
- met.SetEmptyHistogram().SetAggregationTemporality(pmetric.AggregationTemporalityDelta)
- dpsDoubleHist := met.Histogram().DataPoints()
- dpDoubleHist := dpsDoubleHist.AppendEmpty()
- dpDoubleHist.SetCount(20)
- dpDoubleHist.SetSum(6)
- dpDoubleHist.BucketCounts().FromRaw([]uint64{2, 18})
- dpDoubleHist.ExplicitBounds().FromRaw([]float64{0})
- dpDoubleHist.SetTimestamp(seconds(0))
- return md
- }
- func seconds(i int) pcommon.Timestamp {
- return pcommon.NewTimestampFromTime(time.Unix(int64(i), 0))
- }
- func newTestConfig(t *testing.T, endpoint string, hostTags []string, histogramMode HistogramMode) *Config {
- t.Helper()
- return &Config{
- HostMetadata: HostMetadataConfig{
- Tags: hostTags,
- },
- Metrics: MetricsConfig{
- TCPAddr: confignet.TCPAddr{
- Endpoint: endpoint,
- },
- HistConfig: HistogramConfig{
- Mode: histogramMode,
- },
- // Set values to avoid errors. No particular intention in value selection.
- DeltaTTL: 3600,
- SumConfig: SumConfig{
- CumulativeMonotonicMode: CumulativeMonotonicSumModeRawValue,
- },
- },
- }
- }
|