diff --git a/exporters/autoexport/metrics.go b/exporters/autoexport/metrics.go index e8682d9b896..324d400b1a2 100644 --- a/exporters/autoexport/metrics.go +++ b/exporters/autoexport/metrics.go @@ -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 { 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)) }) } @@ -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" @@ -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() diff --git a/exporters/autoexport/registry.go b/exporters/autoexport/registry.go index a01a1de64ca..8b206ce2e4e 100644 --- a/exporters/autoexport/registry.go +++ b/exporters/autoexport/registry.go @@ -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 ( @@ -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] @@ -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 { diff --git a/exporters/autoexport/registry_test.go b/exporters/autoexport/registry_test.go index 97871ee443f..34a376b15a8 100644 --- a/exporters/autoexport/registry_test.go +++ b/exporters/autoexport/registry_test.go @@ -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)), } } @@ -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") } @@ -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") }() @@ -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) diff --git a/exporters/autoexport/signal.go b/exporters/autoexport/signal.go index ec06c98bbdf..173022f9cb6 100644 --- a/exporters/autoexport/signal.go +++ b/exporters/autoexport/signal.go @@ -19,13 +19,13 @@ func newSignal[T any](envKey string) signal[T] { return signal[T]{ envKey: envKey, registry: ®istry[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) } @@ -41,25 +41,25 @@ 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) +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 }) } diff --git a/exporters/autoexport/signal_test.go b/exporters/autoexport/signal_test.go index 8fc4e6eb0ea..a1a1d982654 100644 --- a/exporters/autoexport/signal_test.go +++ b/exporters/autoexport/signal_test.go @@ -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)) diff --git a/exporters/autoexport/spans.go b/exporters/autoexport/spans.go index 1b5de6e5e3f..37bd6470066 100644 --- a/exporters/autoexport/spans.go +++ b/exporters/autoexport/spans.go @@ -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 { return withFallbackFactory[trace.SpanExporter](spanExporterFactory) } @@ -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" @@ -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 }) }