Skip to content

Commit

Permalink
Expose config object
Browse files Browse the repository at this point in the history
Signed-off-by: gouthamve <gouthamve@gmail.com>
  • Loading branch information
gouthamve committed Apr 3, 2024
1 parent 6b0d3d1 commit 1ee0485
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 32 deletions.
14 changes: 7 additions & 7 deletions exporters/autoexport/metrics.go
Expand Up @@ -28,13 +28,13 @@ type MetricOption = option[metric.Reader]

// WithFallbackMetricReader sets the fallback exporter to use when no exporter
// is configured through the OTEL_METRICS_EXPORTER environment variable.
func WithFallbackMetricReader(metricReaderFactory func(ctx context.Context, cfg config[metric.Reader]) (metric.Reader, error)) MetricOption {
func WithFallbackMetricReader(metricReaderFactory func(ctx context.Context, cfg Config[metric.Reader]) (metric.Reader, error)) MetricOption {

Check warning on line 31 in exporters/autoexport/metrics.go

View check run for this annotation

Codecov / codecov/patch

exporters/autoexport/metrics.go#L31

Added line #L31 was not covered by tests
return withFallbackFactory[metric.Reader](metricReaderFactory)
}

// WithProducer registers producers as an external Producer of metric data for this Reader.
func WithProducer(producer metric.Producer) MetricOption {
return optionFunc[metric.Reader](func(cfg *config[metric.Reader]) {
return optionFunc[metric.Reader](func(cfg *Config[metric.Reader]) {
cfg.metricReaderOptions = append(cfg.metricReaderOptions, metric.WithProducer(producer))
})

Check warning on line 39 in exporters/autoexport/metrics.go

View check run for this annotation

Codecov / codecov/patch

exporters/autoexport/metrics.go#L36-L39

Added lines #L36 - L39 were not covered by tests
}
Expand Down Expand Up @@ -74,14 +74,14 @@ func NewMetricReader(ctx context.Context, opts ...MetricOption) (metric.Reader,
// RegisterMetricReader sets the MetricReader factory to be used when the
// OTEL_METRICS_EXPORTERS environment variable contains the exporter name. This
// will panic if name has already been registered.
func RegisterMetricReader(name string, factory func(context.Context, config[metric.Reader]) (metric.Reader, error)) {
func RegisterMetricReader(name string, factory func(context.Context, Config[metric.Reader]) (metric.Reader, error)) {
must(metricsSignal.registry.store(name, factory))
}

var metricsSignal = newSignal[metric.Reader]("OTEL_METRICS_EXPORTER")

func init() {
RegisterMetricReader("otlp", func(ctx context.Context, cfg config[metric.Reader]) (metric.Reader, error) {
RegisterMetricReader("otlp", func(ctx context.Context, cfg Config[metric.Reader]) (metric.Reader, error) {
proto := os.Getenv(otelExporterOTLPProtoEnvKey)
if proto == "" {
proto = "http/protobuf"
Expand All @@ -104,17 +104,17 @@ func init() {
return nil, errInvalidOTLPProtocol
}
})
RegisterMetricReader("console", func(ctx context.Context, cfg config[metric.Reader]) (metric.Reader, error) {
RegisterMetricReader("console", func(ctx context.Context, cfg Config[metric.Reader]) (metric.Reader, error) {
r, err := stdoutmetric.New()
if err != nil {
return nil, err
}
return metric.NewPeriodicReader(r, cfg.metricReaderOptions...), nil
})
RegisterMetricReader("none", func(context.Context, config[metric.Reader]) (metric.Reader, error) {
RegisterMetricReader("none", func(context.Context, Config[metric.Reader]) (metric.Reader, error) {
return newNoopMetricReader(), nil
})
RegisterMetricReader("prometheus", func(ctx context.Context, _ config[metric.Reader]) (metric.Reader, error) {
RegisterMetricReader("prometheus", func(ctx context.Context, _ Config[metric.Reader]) (metric.Reader, error) {
// create an isolated registry instead of using the global registry --
// the user might not want to mix OTel with non-OTel metrics
reg := prometheus.NewRegistry()
Expand Down
6 changes: 3 additions & 3 deletions exporters/autoexport/registry.go
Expand Up @@ -17,7 +17,7 @@ const otelExporterOTLPProtoEnvKey = "OTEL_EXPORTER_OTLP_PROTOCOL"
// goroutines without additional locking or coordination.
type registry[T any] struct {
mu sync.Mutex
names map[string]func(context.Context, config[T]) (T, error)
names map[string]func(context.Context, Config[T]) (T, error)
}

var (
Expand All @@ -37,7 +37,7 @@ var (
// then execute the factory, returning the created SpanExporter.
// errUnknownExporter is returned if the registration is missing and the error from
// executing the factory if not nil.
func (r *registry[T]) load(ctx context.Context, key string, cfg config[T]) (T, error) {
func (r *registry[T]) load(ctx context.Context, key string, cfg Config[T]) (T, error) {
r.mu.Lock()
defer r.mu.Unlock()
factory, ok := r.names[key]
Expand All @@ -50,7 +50,7 @@ func (r *registry[T]) load(ctx context.Context, key string, cfg config[T]) (T, e

// store sets the factory for a key if is not already in the registry. errDuplicateRegistration
// is returned if the registry already contains key.
func (r *registry[T]) store(key string, factory func(_ context.Context, cfg config[T]) (T, error)) error {
func (r *registry[T]) store(key string, factory func(_ context.Context, cfg Config[T]) (T, error)) error {
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.names[key]; ok {
Expand Down
14 changes: 7 additions & 7 deletions exporters/autoexport/registry_test.go
Expand Up @@ -16,13 +16,13 @@ import (

type testType struct{ string }

func factory(val string) func(ctx context.Context, _ config[*testType]) (*testType, error) {
return func(ctx context.Context, _ config[*testType]) (*testType, error) { return &testType{val}, nil }
func factory(val string) func(ctx context.Context, _ Config[*testType]) (*testType, error) {
return func(ctx context.Context, _ Config[*testType]) (*testType, error) { return &testType{val}, nil }
}

func newTestRegistry() registry[*testType] {
return registry[*testType]{
names: make(map[string]func(context.Context, config[*testType]) (*testType, error)),
names: make(map[string]func(context.Context, Config[*testType]) (*testType, error)),
}
}

Expand All @@ -33,7 +33,7 @@ func TestCanStoreExporterFactory(t *testing.T) {

func TestLoadOfUnknownExporterReturnsError(t *testing.T) {
r := newTestRegistry()
exp, err := r.load(context.Background(), "non-existent", config[*testType]{})
exp, err := r.load(context.Background(), "non-existent", Config[*testType]{})
assert.Equal(t, err, errUnknownExporter, "empty registry should hold nothing")
assert.Nil(t, exp, "non-nil exporter returned")
}
Expand All @@ -55,7 +55,7 @@ func TestRegistryIsConcurrentSafe(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
_, err := r.load(context.Background(), exporterName, config[*testType]{})
_, err := r.load(context.Background(), exporterName, Config[*testType]{})
assert.NoError(t, err, "missing exporter in registry")
}()

Expand All @@ -68,10 +68,10 @@ func TestSubsequentCallsToGetExporterReturnsNewInstances(t *testing.T) {
const key = "key"
assert.NoError(t, r.store(key, factory(key)))

exp1, err := r.load(context.Background(), key, config[*testType]{})
exp1, err := r.load(context.Background(), key, Config[*testType]{})
assert.NoError(t, err)

exp2, err := r.load(context.Background(), key, config[*testType]{})
exp2, err := r.load(context.Background(), key, Config[*testType]{})
assert.NoError(t, err)

assert.NotSame(t, exp1, exp2)
Expand Down
19 changes: 10 additions & 9 deletions exporters/autoexport/signal.go
Expand Up @@ -19,13 +19,13 @@ func newSignal[T any](envKey string) signal[T] {
return signal[T]{
envKey: envKey,
registry: &registry[T]{
names: make(map[string]func(context.Context, config[T]) (T, error)),
names: make(map[string]func(context.Context, Config[T]) (T, error)),
},
}
}

func (s signal[T]) create(ctx context.Context, opts ...option[T]) (T, error) {
var cfg config[T]
var cfg Config[T]
for _, opt := range opts {
opt.apply(&cfg)
}
Expand All @@ -41,25 +41,26 @@ func (s signal[T]) create(ctx context.Context, opts ...option[T]) (T, error) {
return s.registry.load(ctx, expType, cfg)
}

type config[T any] struct {
fallbackFactory func(ctx context.Context, _ config[T]) (T, error)
// Config for the signal factory options.
type Config[T any] struct {
fallbackFactory func(ctx context.Context, _ Config[T]) (T, error)

metricReaderOptions []metric.PeriodicReaderOption
}

type option[T any] interface {
apply(cfg *config[T])
apply(cfg *Config[T])
}

type optionFunc[T any] func(cfg *config[T])
type optionFunc[T any] func(cfg *Config[T])

//lint:ignore U1000 https://github.com/dominikh/go-tools/issues/1440
func (fn optionFunc[T]) apply(cfg *config[T]) {
func (fn optionFunc[T]) apply(cfg *Config[T]) {
fn(cfg)
}

func withFallbackFactory[T any](fallbackFactory func(ctx context.Context, cfg config[T]) (T, error)) option[T] {
return optionFunc[T](func(cfg *config[T]) {
func withFallbackFactory[T any](fallbackFactory func(ctx context.Context, cfg Config[T]) (T, error)) option[T] {
return optionFunc[T](func(cfg *Config[T]) {
cfg.fallbackFactory = fallbackFactory
})
}
2 changes: 1 addition & 1 deletion exporters/autoexport/signal_test.go
Expand Up @@ -30,7 +30,7 @@ func TestFallbackExporterFactoryErrorReturnedWhenNoEnvExporterConfiguredAndFallb
ts := newSignal[*testType]("TEST_TYPE_KEY")

expectedErr := errors.New("error expected to return")
errFactory := func(ctx context.Context, _ config[*testType]) (*testType, error) {
errFactory := func(ctx context.Context, _ Config[*testType]) (*testType, error) {
return nil, expectedErr
}
exp, err := ts.create(context.Background(), withFallbackFactory(errFactory))
Expand Down
10 changes: 5 additions & 5 deletions exporters/autoexport/spans.go
Expand Up @@ -23,7 +23,7 @@ type Option = SpanOption

// WithFallbackSpanExporter sets the fallback exporter to use when no exporter
// is configured through the OTEL_TRACES_EXPORTER environment variable.
func WithFallbackSpanExporter(spanExporterFactory func(ctx context.Context, _ config[trace.SpanExporter]) (trace.SpanExporter, error)) SpanOption {
func WithFallbackSpanExporter(spanExporterFactory func(ctx context.Context, _ Config[trace.SpanExporter]) (trace.SpanExporter, error)) SpanOption {

Check warning on line 26 in exporters/autoexport/spans.go

View check run for this annotation

Codecov / codecov/patch

exporters/autoexport/spans.go#L26

Added line #L26 was not covered by tests
return withFallbackFactory[trace.SpanExporter](spanExporterFactory)
}

Expand Down Expand Up @@ -57,14 +57,14 @@ func NewSpanExporter(ctx context.Context, opts ...SpanOption) (trace.SpanExporte
// RegisterSpanExporter sets the SpanExporter factory to be used when the
// OTEL_TRACES_EXPORTERS environment variable contains the exporter name. This
// will panic if name has already been registered.
func RegisterSpanExporter(name string, factory func(context.Context, config[trace.SpanExporter]) (trace.SpanExporter, error)) {
func RegisterSpanExporter(name string, factory func(context.Context, Config[trace.SpanExporter]) (trace.SpanExporter, error)) {
must(tracesSignal.registry.store(name, factory))
}

var tracesSignal = newSignal[trace.SpanExporter]("OTEL_TRACES_EXPORTER")

func init() {
RegisterSpanExporter("otlp", func(ctx context.Context, _ config[trace.SpanExporter]) (trace.SpanExporter, error) {
RegisterSpanExporter("otlp", func(ctx context.Context, _ Config[trace.SpanExporter]) (trace.SpanExporter, error) {
proto := os.Getenv(otelExporterOTLPProtoEnvKey)
if proto == "" {
proto = "http/protobuf"
Expand All @@ -79,10 +79,10 @@ func init() {
return nil, errInvalidOTLPProtocol
}
})
RegisterSpanExporter("console", func(ctx context.Context, _ config[trace.SpanExporter]) (trace.SpanExporter, error) {
RegisterSpanExporter("console", func(ctx context.Context, _ Config[trace.SpanExporter]) (trace.SpanExporter, error) {
return stdouttrace.New()
})
RegisterSpanExporter("none", func(ctx context.Context, _ config[trace.SpanExporter]) (trace.SpanExporter, error) {
RegisterSpanExporter("none", func(ctx context.Context, _ Config[trace.SpanExporter]) (trace.SpanExporter, error) {
return noopSpanExporter{}, nil
})
}

0 comments on commit 1ee0485

Please sign in to comment.