Skip to content

Commit

Permalink
otelcolconvert: automatically sync DebugMetrics with Flow defaults (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rfratto authored Feb 21, 2024
1 parent b92f106 commit 5588140
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 1 deletion.
14 changes: 14 additions & 0 deletions converter/internal/common/river_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
26 changes: 26 additions & 0 deletions converter/internal/common/river_utils_test.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions converter/internal/otelcolconvert/converter_jaegerreceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down
2 changes: 2 additions & 0 deletions converter/internal/otelcolconvert/converter_kafkareceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
Expand Down
2 changes: 2 additions & 0 deletions converter/internal/otelcolconvert/converter_otlpreceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions converter/internal/otelcolconvert/converter_zipkinreceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down

0 comments on commit 5588140

Please sign in to comment.