From 558814037a832d82a700d03aeef711762f0747f9 Mon Sep 17 00:00:00 2001 From: Robert Fratto Date: Wed, 21 Feb 2024 17:40:41 -0500 Subject: [PATCH] otelcolconvert: automatically sync DebugMetrics with Flow defaults (#6469) --- converter/internal/common/river_utils.go | 14 ++++++++++ converter/internal/common/river_utils_test.go | 26 +++++++++++++++++++ .../converter_jaegerreceiver.go | 2 ++ .../otelcolconvert/converter_kafkareceiver.go | 2 ++ .../converter_opencensusreceiver.go | 2 ++ .../otelcolconvert/converter_otlpexporter.go | 2 +- .../otelcolconvert/converter_otlpreceiver.go | 2 ++ .../converter_zipkinreceiver.go | 2 ++ 8 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 converter/internal/common/river_utils_test.go diff --git a/converter/internal/common/river_utils.go b/converter/internal/common/river_utils.go index d8cb98a93e01..40b24520fe56 100644 --- a/converter/internal/common/river_utils.go +++ b/converter/internal/common/river_utils.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + "github.com/grafana/river" "github.com/grafana/river/parser" "github.com/grafana/river/printer" "github.com/grafana/river/scanner" @@ -124,3 +125,16 @@ func SanitizeIdentifierPanics(in string) string { } return out } + +// DefaultValue returns the default value for a given type. If *T implements +// river.Defaulter, a value will be returned with defaults applied. If *T does +// not implement river.Defaulter, the zero value of T is returned. +// +// T must not be a pointer type. +func DefaultValue[T any]() T { + var val T + if defaulter, ok := any(&val).(river.Defaulter); ok { + defaulter.SetToDefault() + } + return val +} diff --git a/converter/internal/common/river_utils_test.go b/converter/internal/common/river_utils_test.go new file mode 100644 index 000000000000..4b60a167cca2 --- /dev/null +++ b/converter/internal/common/river_utils_test.go @@ -0,0 +1,26 @@ +package common_test + +import ( + "testing" + + "github.com/grafana/agent/converter/internal/common" + "github.com/grafana/river" + "github.com/stretchr/testify/require" +) + +func TestDefaultValue(t *testing.T) { + var explicitDefault defaultingType + explicitDefault.SetToDefault() + + require.Equal(t, explicitDefault, common.DefaultValue[defaultingType]()) +} + +type defaultingType struct { + Number int +} + +var _ river.Defaulter = (*defaultingType)(nil) + +func (dt *defaultingType) SetToDefault() { + dt.Number = 42 +} diff --git a/converter/internal/otelcolconvert/converter_jaegerreceiver.go b/converter/internal/otelcolconvert/converter_jaegerreceiver.go index bd627037f419..d9980d03b766 100644 --- a/converter/internal/otelcolconvert/converter_jaegerreceiver.go +++ b/converter/internal/otelcolconvert/converter_jaegerreceiver.go @@ -54,6 +54,8 @@ func toJaegerReceiver(state *state, id component.InstanceID, cfg *jaegerreceiver ThriftCompact: toJaegerThriftCompactArguments(cfg.ThriftCompact), }, + DebugMetrics: common.DefaultValue[jaeger.Arguments]().DebugMetrics, + Output: &otelcol.ConsumerArguments{ Traces: toTokenizedConsumers(nextTraces), }, diff --git a/converter/internal/otelcolconvert/converter_kafkareceiver.go b/converter/internal/otelcolconvert/converter_kafkareceiver.go index 8c0b1a0026d1..94a1b71ca18b 100644 --- a/converter/internal/otelcolconvert/converter_kafkareceiver.go +++ b/converter/internal/otelcolconvert/converter_kafkareceiver.go @@ -65,6 +65,8 @@ func toKafkaReceiver(state *state, id component.InstanceID, cfg *kafkareceiver.C MessageMarking: toKafkaMessageMarking(cfg.MessageMarking), HeaderExtraction: toKafkaHeaderExtraction(cfg.HeaderExtraction), + DebugMetrics: common.DefaultValue[kafka.Arguments]().DebugMetrics, + Output: &otelcol.ConsumerArguments{ Metrics: toTokenizedConsumers(nextMetrics), Logs: toTokenizedConsumers(nextLogs), diff --git a/converter/internal/otelcolconvert/converter_opencensusreceiver.go b/converter/internal/otelcolconvert/converter_opencensusreceiver.go index 1df72fab8da4..332f512a74e5 100644 --- a/converter/internal/otelcolconvert/converter_opencensusreceiver.go +++ b/converter/internal/otelcolconvert/converter_opencensusreceiver.go @@ -50,6 +50,8 @@ func toOpencensusReceiver(state *state, id component.InstanceID, cfg *opencensus CorsAllowedOrigins: cfg.CorsOrigins, GRPC: *toGRPCServerArguments(&cfg.GRPCServerSettings), + DebugMetrics: common.DefaultValue[opencensus.Arguments]().DebugMetrics, + Output: &otelcol.ConsumerArguments{ Metrics: toTokenizedConsumers(nextMetrics), Traces: toTokenizedConsumers(nextTraces), diff --git a/converter/internal/otelcolconvert/converter_otlpexporter.go b/converter/internal/otelcolconvert/converter_otlpexporter.go index fa57ea698e7f..f4e1e8e10063 100644 --- a/converter/internal/otelcolconvert/converter_otlpexporter.go +++ b/converter/internal/otelcolconvert/converter_otlpexporter.go @@ -52,7 +52,7 @@ func toOtelcolExporterOTLP(cfg *otlpexporter.Config) *otlp.Arguments { Queue: toQueueArguments(cfg.QueueSettings), Retry: toRetryArguments(cfg.RetrySettings), - DebugMetrics: otelcol.DefaultDebugMetricsArguments, + DebugMetrics: common.DefaultValue[otlp.Arguments]().DebugMetrics, Client: otlp.GRPCClientArguments(toGRPCClientArguments(cfg.GRPCClientSettings)), } diff --git a/converter/internal/otelcolconvert/converter_otlpreceiver.go b/converter/internal/otelcolconvert/converter_otlpreceiver.go index 6a0ad8f4cbb2..a46a96c727af 100644 --- a/converter/internal/otelcolconvert/converter_otlpreceiver.go +++ b/converter/internal/otelcolconvert/converter_otlpreceiver.go @@ -54,6 +54,8 @@ func toOtelcolReceiverOTLP(state *state, id component.InstanceID, cfg *otlprecei GRPC: (*otlp.GRPCServerArguments)(toGRPCServerArguments(cfg.GRPC)), HTTP: toHTTPConfigArguments(cfg.HTTP), + DebugMetrics: common.DefaultValue[otlp.Arguments]().DebugMetrics, + Output: &otelcol.ConsumerArguments{ Metrics: toTokenizedConsumers(nextMetrics), Logs: toTokenizedConsumers(nextLogs), diff --git a/converter/internal/otelcolconvert/converter_zipkinreceiver.go b/converter/internal/otelcolconvert/converter_zipkinreceiver.go index 53c9f8de148c..20ec5aecc830 100644 --- a/converter/internal/otelcolconvert/converter_zipkinreceiver.go +++ b/converter/internal/otelcolconvert/converter_zipkinreceiver.go @@ -47,6 +47,8 @@ func toZipkinReceiver(state *state, id component.InstanceID, cfg *zipkinreceiver ParseStringTags: cfg.ParseStringTags, HTTPServer: *toHTTPServerArguments(&cfg.HTTPServerSettings), + DebugMetrics: common.DefaultValue[zipkin.Arguments]().DebugMetrics, + Output: &otelcol.ConsumerArguments{ Traces: toTokenizedConsumers(nextTraces), },