From edd2fa7f3770e1f55948838fff44393ce5c4c4eb Mon Sep 17 00:00:00 2001 From: Yang Song Date: Tue, 12 Dec 2023 13:30:32 -0500 Subject: [PATCH] Fix all breakage --- connector/datadogconnector/connector.go | 8 ++++---- connector/datadogconnector/factory.go | 4 ++-- .../internal/metrics/consumer_test.go | 5 ++++- exporter/datadogexporter/logs_exporter_test.go | 17 +++++++++-------- exporter/datadogexporter/metrics_exporter.go | 7 ++++--- .../datadogexporter/metrics_exporter_test.go | 6 ++++-- processor/datadogprocessor/factory.go | 2 +- processor/datadogprocessor/processor.go | 8 ++++---- processor/datadogprocessor/processor_test.go | 14 +++++++------- 9 files changed, 39 insertions(+), 32 deletions(-) diff --git a/connector/datadogconnector/connector.go b/connector/datadogconnector/connector.go index 7e8efd7ffacf..69c7a0f1f99e 100644 --- a/connector/datadogconnector/connector.go +++ b/connector/datadogconnector/connector.go @@ -41,18 +41,18 @@ type connectorImp struct { var _ component.Component = (*connectorImp)(nil) // testing that the connectorImp properly implements the type Component interface // function to create a new connector -func newConnector(logger *zap.Logger, _ component.Config, metricsConsumer consumer.Metrics, tracesConsumer consumer.Traces) (*connectorImp, error) { - logger.Info("Building datadog connector") +func newConnector(set component.TelemetrySettings, _ component.Config, metricsConsumer consumer.Metrics, tracesConsumer consumer.Traces) (*connectorImp, error) { + set.Logger.Info("Building datadog connector") in := make(chan *pb.StatsPayload, 100) - trans, err := metrics.NewTranslator(logger) + trans, err := metrics.NewTranslator(set) ctx := context.Background() if err != nil { return nil, err } return &connectorImp{ - logger: logger, + logger: set.Logger, agent: datadog.NewAgent(ctx, in), translator: trans, in: in, diff --git a/connector/datadogconnector/factory.go b/connector/datadogconnector/factory.go index 5d1d6673426c..a209a6af9405 100644 --- a/connector/datadogconnector/factory.go +++ b/connector/datadogconnector/factory.go @@ -36,7 +36,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) { - c, err := newConnector(params.Logger, cfg, nextConsumer, nil) + c, err := newConnector(params.TelemetrySettings, cfg, nextConsumer, nil) if err != nil { return nil, err } @@ -44,7 +44,7 @@ func createTracesToMetricsConnector(_ context.Context, params connector.CreateSe } func createTracesToTracesConnector(_ context.Context, params connector.CreateSettings, cfg component.Config, nextConsumer consumer.Traces) (connector.Traces, error) { - c, err := newConnector(params.Logger, cfg, nil, nextConsumer) + c, err := newConnector(params.TelemetrySettings, cfg, nil, nextConsumer) if err != nil { return nil, err } diff --git a/exporter/datadogexporter/internal/metrics/consumer_test.go b/exporter/datadogexporter/internal/metrics/consumer_test.go index 37bceedf1861..d9b84df03367 100644 --- a/exporter/datadogexporter/internal/metrics/consumer_test.go +++ b/exporter/datadogexporter/internal/metrics/consumer_test.go @@ -14,6 +14,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/pdata/pmetric" conventions "go.opentelemetry.io/collector/semconv/v1.6.1" "go.uber.org/zap" @@ -28,7 +29,9 @@ func (t testProvider) Source(context.Context) (source.Source, error) { } func newTranslator(t *testing.T, logger *zap.Logger) *metrics.Translator { - tr, err := metrics.NewTranslator(logger, + set := componenttest.NewNopTelemetrySettings() + set.Logger = logger + tr, err := metrics.NewTranslator(set, metrics.WithHistogramMode(metrics.HistogramModeDistributions), metrics.WithNumberMode(metrics.NumberModeCumulativeToDelta), metrics.WithFallbackSourceProvider(testProvider("fallbackHostname")), diff --git a/exporter/datadogexporter/logs_exporter_test.go b/exporter/datadogexporter/logs_exporter_test.go index 7f01c3629af9..f2e9fb5d1ab4 100644 --- a/exporter/datadogexporter/logs_exporter_test.go +++ b/exporter/datadogexporter/logs_exporter_test.go @@ -8,7 +8,6 @@ import ( "encoding/binary" "fmt" "testing" - "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -21,6 +20,8 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/traceutil" ) +const timeFormatString = "2006-01-02T15:04:05.000Z07:00" + func TestLogsExporter(t *testing.T) { lr := testdata.GenerateLogsOneLogRecord() ld := lr.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0) @@ -44,7 +45,7 @@ func TestLogsExporter(t *testing.T) { "message": ld.Body().AsString(), "app": "server", "instance_num": "1", - "@timestamp": testdata.TestLogTime.Format(time.RFC3339), + "@timestamp": testdata.TestLogTime.Format(timeFormatString), "status": "Info", "dd.span_id": fmt.Sprintf("%d", spanIDToUint64(ld.SpanID())), "dd.trace_id": fmt.Sprintf("%d", traceIDToUint64(ld.TraceID())), @@ -74,7 +75,7 @@ func TestLogsExporter(t *testing.T) { "message": "hello", "app": "server", "instance_num": "1", - "@timestamp": testdata.TestLogTime.Format(time.RFC3339), + "@timestamp": testdata.TestLogTime.Format(timeFormatString), "status": "Info", "dd.span_id": fmt.Sprintf("%d", spanIDToUint64(ld.SpanID())), "dd.trace_id": fmt.Sprintf("%d", traceIDToUint64(ld.TraceID())), @@ -104,7 +105,7 @@ func TestLogsExporter(t *testing.T) { "message": ld.Body().AsString(), "app": "server", "instance_num": "1", - "@timestamp": testdata.TestLogTime.Format(time.RFC3339), + "@timestamp": testdata.TestLogTime.Format(timeFormatString), "status": "Info", "dd.span_id": fmt.Sprintf("%d", spanIDToUint64(ld.SpanID())), "dd.trace_id": fmt.Sprintf("%d", traceIDToUint64(ld.TraceID())), @@ -136,7 +137,7 @@ func TestLogsExporter(t *testing.T) { "message": ld.Body().AsString(), "app": "server", "instance_num": "1", - "@timestamp": testdata.TestLogTime.Format(time.RFC3339), + "@timestamp": testdata.TestLogTime.Format(timeFormatString), "status": "Info", "dd.span_id": fmt.Sprintf("%d", spanIDToUint64(ld.SpanID())), "dd.trace_id": fmt.Sprintf("%d", traceIDToUint64(ld.TraceID())), @@ -152,7 +153,7 @@ func TestLogsExporter(t *testing.T) { "message": "something happened", "env": "dev", "customer": "acme", - "@timestamp": testdata.TestLogTime.Format(time.RFC3339), + "@timestamp": testdata.TestLogTime.Format(timeFormatString), "status": "Info", "ddtags": "tag1:true,otel_source:datadog_exporter", "otel.severity_text": "Info", @@ -180,7 +181,7 @@ func TestLogsExporter(t *testing.T) { "message": ld.Body().AsString(), "app": "server", "instance_num": "1", - "@timestamp": testdata.TestLogTime.Format(time.RFC3339), + "@timestamp": testdata.TestLogTime.Format(timeFormatString), "status": "Info", "dd.span_id": fmt.Sprintf("%d", spanIDToUint64(ld.SpanID())), "dd.trace_id": fmt.Sprintf("%d", traceIDToUint64(ld.TraceID())), @@ -196,7 +197,7 @@ func TestLogsExporter(t *testing.T) { "message": "something happened", "env": "dev", "customer": "acme", - "@timestamp": testdata.TestLogTime.Format(time.RFC3339), + "@timestamp": testdata.TestLogTime.Format(timeFormatString), "status": "Info", "ddtags": "tag2:true,otel_source:datadog_exporter", "otel.severity_text": "Info", diff --git a/exporter/datadogexporter/metrics_exporter.go b/exporter/datadogexporter/metrics_exporter.go index 01f0342786f1..b0144727ebd0 100644 --- a/exporter/datadogexporter/metrics_exporter.go +++ b/exporter/datadogexporter/metrics_exporter.go @@ -18,6 +18,7 @@ import ( "github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata" "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" @@ -50,7 +51,7 @@ type metricsExporter struct { } // translatorFromConfig creates a new metrics translator from the exporter -func translatorFromConfig(logger *zap.Logger, cfg *Config, sourceProvider source.Provider) (*otlpmetrics.Translator, error) { +func translatorFromConfig(set component.TelemetrySettings, cfg *Config, sourceProvider source.Provider) (*otlpmetrics.Translator, error) { options := []otlpmetrics.TranslatorOption{ otlpmetrics.WithDeltaTTL(cfg.Metrics.DeltaTTL), otlpmetrics.WithFallbackSourceProvider(sourceProvider), @@ -82,7 +83,7 @@ func translatorFromConfig(logger *zap.Logger, cfg *Config, sourceProvider source options = append(options, otlpmetrics.WithInitialCumulMonoValueMode( otlpmetrics.InitialCumulMonoValueMode(cfg.Metrics.SumConfig.InitialCumulativeMonotonicMode))) - return otlpmetrics.NewTranslator(logger, options...) + return otlpmetrics.NewTranslator(set, options...) } func newMetricsExporter( @@ -94,7 +95,7 @@ func newMetricsExporter( apmStatsProcessor api.StatsProcessor, metadataReporter *inframetadata.Reporter, ) (*metricsExporter, error) { - tr, err := translatorFromConfig(params.Logger, cfg, sourceProvider) + tr, err := translatorFromConfig(params.TelemetrySettings, cfg, sourceProvider) if err != nil { return nil, err } diff --git a/exporter/datadogexporter/metrics_exporter_test.go b/exporter/datadogexporter/metrics_exporter_test.go index 8dcf3d792a47..8bb134e86d83 100644 --- a/exporter/datadogexporter/metrics_exporter_test.go +++ b/exporter/datadogexporter/metrics_exporter_test.go @@ -23,6 +23,7 @@ import ( "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/config/confignet" "go.opentelemetry.io/collector/exporter/exportertest" "go.opentelemetry.io/collector/pdata/pcommon" @@ -750,8 +751,9 @@ func createTestMetricsWithStats() pmetric.Metrics { "custom_attribute": "custom_value", }) dest := md.ResourceMetrics() - logger, _ := zap.NewDevelopment() - trans, err := metrics.NewTranslator(logger) + set := componenttest.NewNopTelemetrySettings() + set.Logger, _ = zap.NewDevelopment() + trans, err := metrics.NewTranslator(set) if err != nil { panic(err) } diff --git a/processor/datadogprocessor/factory.go b/processor/datadogprocessor/factory.go index d5bbbcceddb9..7e8f547277b8 100644 --- a/processor/datadogprocessor/factory.go +++ b/processor/datadogprocessor/factory.go @@ -23,5 +23,5 @@ func NewFactory() processor.Factory { } func createTracesProcessor(ctx context.Context, params processor.CreateSettings, cfg component.Config, nextConsumer consumer.Traces) (processor.Traces, error) { - return newProcessor(ctx, params.Logger, cfg, nextConsumer) + return newProcessor(ctx, params.TelemetrySettings, cfg, nextConsumer) } diff --git a/processor/datadogprocessor/processor.go b/processor/datadogprocessor/processor.go index 5ace04936c86..7eb70d5fa3e3 100644 --- a/processor/datadogprocessor/processor.go +++ b/processor/datadogprocessor/processor.go @@ -44,14 +44,14 @@ type datadogProcessor struct { exit chan struct{} } -func newProcessor(ctx context.Context, logger *zap.Logger, config component.Config, nextConsumer consumer.Traces) (*datadogProcessor, error) { +func newProcessor(ctx context.Context, set component.TelemetrySettings, config component.Config, nextConsumer consumer.Traces) (*datadogProcessor, error) { cfg := config.(*Config) in := make(chan *pb.StatsPayload, 100) - trans, err := metrics.NewTranslator(logger) + trans, err := metrics.NewTranslator(set) if err != nil { return nil, err } - logger.Warn( + set.Logger.Warn( "The datadogprocessor has been deprecated in favor of the datadogconnector", zap.String( "documentation", @@ -59,7 +59,7 @@ func newProcessor(ctx context.Context, logger *zap.Logger, config component.Conf ), ) return &datadogProcessor{ - logger: logger, + logger: set.Logger, nextConsumer: nextConsumer, agent: datadog.NewAgent(ctx, in), translator: trans, diff --git a/processor/datadogprocessor/processor_test.go b/processor/datadogprocessor/processor_test.go index 5e3c198c925f..789a0122f640 100644 --- a/processor/datadogprocessor/processor_test.go +++ b/processor/datadogprocessor/processor_test.go @@ -14,12 +14,12 @@ import ( "github.com/DataDog/sketches-go/ddsketch" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/exporter" "go.opentelemetry.io/collector/extension" "go.opentelemetry.io/collector/pdata/pmetric" "go.opentelemetry.io/collector/pdata/ptrace" - "go.uber.org/zap" "google.golang.org/protobuf/proto" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/datadog" @@ -29,7 +29,7 @@ func TestProcessorStart(t *testing.T) { ctx := context.Background() t.Run("fail", func(t *testing.T) { - p, err := newProcessor(ctx, zap.NewNop(), createDefaultConfig(), &mockTracesConsumer{}) + p, err := newProcessor(ctx, componenttest.NewNopTelemetrySettings(), createDefaultConfig(), &mockTracesConsumer{}) require.NoError(t, err) defer p.Shutdown(ctx) //nolint:errcheck require.True(t, p.Capabilities().MutatesData) @@ -42,7 +42,7 @@ func TestProcessorStart(t *testing.T) { }) t.Run("fail/2", func(t *testing.T) { - p, err := newProcessor(ctx, zap.NewNop(), createDefaultConfig(), &mockTracesConsumer{}) + p, err := newProcessor(ctx, componenttest.NewNopTelemetrySettings(), createDefaultConfig(), &mockTracesConsumer{}) require.NoError(t, err) defer p.Shutdown(ctx) //nolint:errcheck err = p.Start(ctx, &mockHost{ @@ -56,7 +56,7 @@ func TestProcessorStart(t *testing.T) { }) t.Run("succeed/0", func(t *testing.T) { - p, err := newProcessor(ctx, zap.NewNop(), createDefaultConfig(), &mockTracesConsumer{}) + p, err := newProcessor(ctx, componenttest.NewNopTelemetrySettings(), createDefaultConfig(), &mockTracesConsumer{}) require.NoError(t, err) defer p.Shutdown(ctx) //nolint:errcheck err = p.Start(ctx, &mockHost{ @@ -69,7 +69,7 @@ func TestProcessorStart(t *testing.T) { }) t.Run("succeed/1", func(t *testing.T) { - p, err := newProcessor(ctx, zap.NewNop(), createDefaultConfig(), &mockTracesConsumer{}) + p, err := newProcessor(ctx, componenttest.NewNopTelemetrySettings(), createDefaultConfig(), &mockTracesConsumer{}) require.NoError(t, err) defer p.Shutdown(ctx) //nolint:errcheck err = p.Start(ctx, &mockHost{ @@ -82,7 +82,7 @@ func TestProcessorStart(t *testing.T) { }) t.Run("succeed/2", func(t *testing.T) { - lp, err := newProcessor(ctx, zap.NewNop(), &Config{MetricsExporter: component.NewIDWithName("datadog", "2")}, &mockTracesConsumer{}) + lp, err := newProcessor(ctx, componenttest.NewNopTelemetrySettings(), &Config{MetricsExporter: component.NewIDWithName("datadog", "2")}, &mockTracesConsumer{}) require.NoError(t, err) defer lp.Shutdown(ctx) //nolint:errcheck err = lp.Start(ctx, &mockHost{ @@ -99,7 +99,7 @@ func TestProcessorStart(t *testing.T) { func TestProcessorIngest(t *testing.T) { var mockConsumer mockTracesConsumer ctx := context.Background() - p, err := newProcessor(ctx, zap.NewNop(), createDefaultConfig(), &mockConsumer) + p, err := newProcessor(ctx, componenttest.NewNopTelemetrySettings(), createDefaultConfig(), &mockConsumer) require.NoError(t, err) out := make(chan *pb.StatsPayload, 1) ing := &mockIngester{Out: out}