|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport" |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "os" |
| 20 | + |
| 21 | + "go.opentelemetry.io/otel/sdk/trace" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + otelTracesExportersEnvKey = "OTEL_TRACES_EXPORTER" |
| 26 | +) |
| 27 | + |
| 28 | +type config struct { |
| 29 | + fallbackExporter trace.SpanExporter |
| 30 | +} |
| 31 | + |
| 32 | +func newConfig(ctx context.Context, opts ...Option) (config, error) { |
| 33 | + cfg := config{} |
| 34 | + for _, opt := range opts { |
| 35 | + cfg = opt.apply(cfg) |
| 36 | + } |
| 37 | + |
| 38 | + // if no fallback exporter is configured, use otlp exporter |
| 39 | + if cfg.fallbackExporter == nil { |
| 40 | + exp, err := spanExporter(context.Background(), "otlp") |
| 41 | + if err != nil { |
| 42 | + return cfg, err |
| 43 | + } |
| 44 | + cfg.fallbackExporter = exp |
| 45 | + } |
| 46 | + return cfg, nil |
| 47 | +} |
| 48 | + |
| 49 | +// Option applies an autoexport configuration option. |
| 50 | +type Option interface { |
| 51 | + apply(config) config |
| 52 | +} |
| 53 | + |
| 54 | +type optionFunc func(config) config |
| 55 | + |
| 56 | +func (fn optionFunc) apply(cfg config) config { |
| 57 | + return fn(cfg) |
| 58 | +} |
| 59 | + |
| 60 | +// WithFallbackSpanExporter sets the fallback exporter to use when no exporter |
| 61 | +// is configured through the OTEL_TRACES_EXPORTER environment variable. |
| 62 | +func WithFallbackSpanExporter(exporter trace.SpanExporter) Option { |
| 63 | + return optionFunc(func(cfg config) config { |
| 64 | + cfg.fallbackExporter = exporter |
| 65 | + return cfg |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +// NewSpanExporter returns a configured SpanExporter defined using the environment |
| 70 | +// variable OTEL_TRACES_EXPORTER, the configured fallback exporter via options or |
| 71 | +// a default OTLP exporter (in this order). |
| 72 | +func NewSpanExporter(ctx context.Context, opts ...Option) (trace.SpanExporter, error) { |
| 73 | + // prefer exporter configured via environment variables over exporter |
| 74 | + // passed in via exporter parameter |
| 75 | + envExporter, err := makeExporterFromEnv(ctx) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + if envExporter != nil { |
| 80 | + return envExporter, nil |
| 81 | + } |
| 82 | + config, err := newConfig(ctx, opts...) |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + return config.fallbackExporter, nil |
| 87 | +} |
| 88 | + |
| 89 | +// makeExporterFromEnv returns a configured SpanExporter defined by the OTEL_TRACES_EXPORTER |
| 90 | +// environment variable. |
| 91 | +// nil is returned if no exporter is defined for the environment variable. |
| 92 | +func makeExporterFromEnv(ctx context.Context) (trace.SpanExporter, error) { |
| 93 | + expType, defined := os.LookupEnv(otelTracesExportersEnvKey) |
| 94 | + if !defined { |
| 95 | + return nil, nil |
| 96 | + } |
| 97 | + return spanExporter(ctx, expType) |
| 98 | +} |
0 commit comments