Skip to content

Commit 1ee0485

Browse files
committed
Expose config object
Signed-off-by: gouthamve <[email protected]>
1 parent 6b0d3d1 commit 1ee0485

File tree

6 files changed

+33
-32
lines changed

6 files changed

+33
-32
lines changed

exporters/autoexport/metrics.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ type MetricOption = option[metric.Reader]
2828

2929
// WithFallbackMetricReader sets the fallback exporter to use when no exporter
3030
// is configured through the OTEL_METRICS_EXPORTER environment variable.
31-
func WithFallbackMetricReader(metricReaderFactory func(ctx context.Context, cfg config[metric.Reader]) (metric.Reader, error)) MetricOption {
31+
func WithFallbackMetricReader(metricReaderFactory func(ctx context.Context, cfg Config[metric.Reader]) (metric.Reader, error)) MetricOption {
3232
return withFallbackFactory[metric.Reader](metricReaderFactory)
3333
}
3434

3535
// WithProducer registers producers as an external Producer of metric data for this Reader.
3636
func WithProducer(producer metric.Producer) MetricOption {
37-
return optionFunc[metric.Reader](func(cfg *config[metric.Reader]) {
37+
return optionFunc[metric.Reader](func(cfg *Config[metric.Reader]) {
3838
cfg.metricReaderOptions = append(cfg.metricReaderOptions, metric.WithProducer(producer))
3939
})
4040
}
@@ -74,14 +74,14 @@ func NewMetricReader(ctx context.Context, opts ...MetricOption) (metric.Reader,
7474
// RegisterMetricReader sets the MetricReader factory to be used when the
7575
// OTEL_METRICS_EXPORTERS environment variable contains the exporter name. This
7676
// will panic if name has already been registered.
77-
func RegisterMetricReader(name string, factory func(context.Context, config[metric.Reader]) (metric.Reader, error)) {
77+
func RegisterMetricReader(name string, factory func(context.Context, Config[metric.Reader]) (metric.Reader, error)) {
7878
must(metricsSignal.registry.store(name, factory))
7979
}
8080

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

8383
func init() {
84-
RegisterMetricReader("otlp", func(ctx context.Context, cfg config[metric.Reader]) (metric.Reader, error) {
84+
RegisterMetricReader("otlp", func(ctx context.Context, cfg Config[metric.Reader]) (metric.Reader, error) {
8585
proto := os.Getenv(otelExporterOTLPProtoEnvKey)
8686
if proto == "" {
8787
proto = "http/protobuf"
@@ -104,17 +104,17 @@ func init() {
104104
return nil, errInvalidOTLPProtocol
105105
}
106106
})
107-
RegisterMetricReader("console", func(ctx context.Context, cfg config[metric.Reader]) (metric.Reader, error) {
107+
RegisterMetricReader("console", func(ctx context.Context, cfg Config[metric.Reader]) (metric.Reader, error) {
108108
r, err := stdoutmetric.New()
109109
if err != nil {
110110
return nil, err
111111
}
112112
return metric.NewPeriodicReader(r, cfg.metricReaderOptions...), nil
113113
})
114-
RegisterMetricReader("none", func(context.Context, config[metric.Reader]) (metric.Reader, error) {
114+
RegisterMetricReader("none", func(context.Context, Config[metric.Reader]) (metric.Reader, error) {
115115
return newNoopMetricReader(), nil
116116
})
117-
RegisterMetricReader("prometheus", func(ctx context.Context, _ config[metric.Reader]) (metric.Reader, error) {
117+
RegisterMetricReader("prometheus", func(ctx context.Context, _ Config[metric.Reader]) (metric.Reader, error) {
118118
// create an isolated registry instead of using the global registry --
119119
// the user might not want to mix OTel with non-OTel metrics
120120
reg := prometheus.NewRegistry()

exporters/autoexport/registry.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const otelExporterOTLPProtoEnvKey = "OTEL_EXPORTER_OTLP_PROTOCOL"
1717
// goroutines without additional locking or coordination.
1818
type registry[T any] struct {
1919
mu sync.Mutex
20-
names map[string]func(context.Context, config[T]) (T, error)
20+
names map[string]func(context.Context, Config[T]) (T, error)
2121
}
2222

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

5151
// store sets the factory for a key if is not already in the registry. errDuplicateRegistration
5252
// is returned if the registry already contains key.
53-
func (r *registry[T]) store(key string, factory func(_ context.Context, cfg config[T]) (T, error)) error {
53+
func (r *registry[T]) store(key string, factory func(_ context.Context, cfg Config[T]) (T, error)) error {
5454
r.mu.Lock()
5555
defer r.mu.Unlock()
5656
if _, ok := r.names[key]; ok {

exporters/autoexport/registry_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import (
1616

1717
type testType struct{ string }
1818

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

2323
func newTestRegistry() registry[*testType] {
2424
return registry[*testType]{
25-
names: make(map[string]func(context.Context, config[*testType]) (*testType, error)),
25+
names: make(map[string]func(context.Context, Config[*testType]) (*testType, error)),
2626
}
2727
}
2828

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

3434
func TestLoadOfUnknownExporterReturnsError(t *testing.T) {
3535
r := newTestRegistry()
36-
exp, err := r.load(context.Background(), "non-existent", config[*testType]{})
36+
exp, err := r.load(context.Background(), "non-existent", Config[*testType]{})
3737
assert.Equal(t, err, errUnknownExporter, "empty registry should hold nothing")
3838
assert.Nil(t, exp, "non-nil exporter returned")
3939
}
@@ -55,7 +55,7 @@ func TestRegistryIsConcurrentSafe(t *testing.T) {
5555
wg.Add(1)
5656
go func() {
5757
defer wg.Done()
58-
_, err := r.load(context.Background(), exporterName, config[*testType]{})
58+
_, err := r.load(context.Background(), exporterName, Config[*testType]{})
5959
assert.NoError(t, err, "missing exporter in registry")
6060
}()
6161

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

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

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

7777
assert.NotSame(t, exp1, exp2)

exporters/autoexport/signal.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ func newSignal[T any](envKey string) signal[T] {
1919
return signal[T]{
2020
envKey: envKey,
2121
registry: &registry[T]{
22-
names: make(map[string]func(context.Context, config[T]) (T, error)),
22+
names: make(map[string]func(context.Context, Config[T]) (T, error)),
2323
},
2424
}
2525
}
2626

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

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

4748
metricReaderOptions []metric.PeriodicReaderOption
4849
}
4950

5051
type option[T any] interface {
51-
apply(cfg *config[T])
52+
apply(cfg *Config[T])
5253
}
5354

54-
type optionFunc[T any] func(cfg *config[T])
55+
type optionFunc[T any] func(cfg *Config[T])
5556

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

61-
func withFallbackFactory[T any](fallbackFactory func(ctx context.Context, cfg config[T]) (T, error)) option[T] {
62-
return optionFunc[T](func(cfg *config[T]) {
62+
func withFallbackFactory[T any](fallbackFactory func(ctx context.Context, cfg Config[T]) (T, error)) option[T] {
63+
return optionFunc[T](func(cfg *Config[T]) {
6364
cfg.fallbackFactory = fallbackFactory
6465
})
6566
}

exporters/autoexport/signal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestFallbackExporterFactoryErrorReturnedWhenNoEnvExporterConfiguredAndFallb
3030
ts := newSignal[*testType]("TEST_TYPE_KEY")
3131

3232
expectedErr := errors.New("error expected to return")
33-
errFactory := func(ctx context.Context, _ config[*testType]) (*testType, error) {
33+
errFactory := func(ctx context.Context, _ Config[*testType]) (*testType, error) {
3434
return nil, expectedErr
3535
}
3636
exp, err := ts.create(context.Background(), withFallbackFactory(errFactory))

exporters/autoexport/spans.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Option = SpanOption
2323

2424
// WithFallbackSpanExporter sets the fallback exporter to use when no exporter
2525
// is configured through the OTEL_TRACES_EXPORTER environment variable.
26-
func WithFallbackSpanExporter(spanExporterFactory func(ctx context.Context, _ config[trace.SpanExporter]) (trace.SpanExporter, error)) SpanOption {
26+
func WithFallbackSpanExporter(spanExporterFactory func(ctx context.Context, _ Config[trace.SpanExporter]) (trace.SpanExporter, error)) SpanOption {
2727
return withFallbackFactory[trace.SpanExporter](spanExporterFactory)
2828
}
2929

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

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

6666
func init() {
67-
RegisterSpanExporter("otlp", func(ctx context.Context, _ config[trace.SpanExporter]) (trace.SpanExporter, error) {
67+
RegisterSpanExporter("otlp", func(ctx context.Context, _ Config[trace.SpanExporter]) (trace.SpanExporter, error) {
6868
proto := os.Getenv(otelExporterOTLPProtoEnvKey)
6969
if proto == "" {
7070
proto = "http/protobuf"
@@ -79,10 +79,10 @@ func init() {
7979
return nil, errInvalidOTLPProtocol
8080
}
8181
})
82-
RegisterSpanExporter("console", func(ctx context.Context, _ config[trace.SpanExporter]) (trace.SpanExporter, error) {
82+
RegisterSpanExporter("console", func(ctx context.Context, _ Config[trace.SpanExporter]) (trace.SpanExporter, error) {
8383
return stdouttrace.New()
8484
})
85-
RegisterSpanExporter("none", func(ctx context.Context, _ config[trace.SpanExporter]) (trace.SpanExporter, error) {
85+
RegisterSpanExporter("none", func(ctx context.Context, _ Config[trace.SpanExporter]) (trace.SpanExporter, error) {
8686
return noopSpanExporter{}, nil
8787
})
8888
}

0 commit comments

Comments
 (0)