|
| 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/exporters/otlp/otlpmetric/otlpmetricgrpc" |
| 22 | + "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" |
| 23 | + "go.opentelemetry.io/otel/sdk/metric" |
| 24 | +) |
| 25 | + |
| 26 | +// MetricOption applies an autoexport configuration option. |
| 27 | +type MetricOption = option[metric.Reader] |
| 28 | + |
| 29 | +// WithFallbackMetricReader sets the fallback exporter to use when no exporter |
| 30 | +// is configured through the OTEL_METRICS_EXPORTER environment variable. |
| 31 | +func WithFallbackMetricReader(exporter metric.Reader) MetricOption { |
| 32 | + return withFallback[metric.Reader](exporter) |
| 33 | +} |
| 34 | + |
| 35 | +// NewMetricReader returns a configured [go.opentelemetry.io/otel/sdk/metric.Reader] |
| 36 | +// defined using the environment variables described below. |
| 37 | +// |
| 38 | +// OTEL_METRICS_EXPORTER defines the metrics exporter; supported values: |
| 39 | +// - "none" - "no operation" exporter |
| 40 | +// - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlpmetric] |
| 41 | +// |
| 42 | +// OTEL_EXPORTER_OTLP_PROTOCOL defines OTLP exporter's transport protocol; |
| 43 | +// supported values: |
| 44 | +// - "grpc" - protobuf-encoded data using gRPC wire format over HTTP/2 connection; |
| 45 | +// see: [go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc] |
| 46 | +// - "http/protobuf" (default) - protobuf-encoded data over HTTP connection; |
| 47 | +// see: [go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp] |
| 48 | +// |
| 49 | +// An error is returned if an environment value is set to an unhandled value. |
| 50 | +// |
| 51 | +// Use [RegisterMetricReader] to handle more values of OTEL_METRICS_EXPORTER. |
| 52 | +// |
| 53 | +// Use [WithFallbackMetricReader] option to change the returned exporter |
| 54 | +// when OTEL_TRACES_EXPORTER is unset or empty. |
| 55 | +// |
| 56 | +// Use [IsNoneMetricReader] to check if the retured exporter is a "no operation" exporter. |
| 57 | +func NewMetricReader(ctx context.Context, opts ...MetricOption) (metric.Reader, error) { |
| 58 | + return metricsSignal.create(ctx, opts...) |
| 59 | +} |
| 60 | + |
| 61 | +// RegisterMetricReader sets the MetricReader factory to be used when the |
| 62 | +// OTEL_METRICS_EXPORTERS environment variable contains the exporter name. This |
| 63 | +// will panic if name has already been registered. |
| 64 | +func RegisterMetricReader(name string, factory func(context.Context) (metric.Reader, error)) { |
| 65 | + must(metricsSignal.registry.store(name, factory)) |
| 66 | +} |
| 67 | + |
| 68 | +var metricsSignal = newSignal[metric.Reader]("OTEL_METRICS_EXPORTER") |
| 69 | + |
| 70 | +func init() { |
| 71 | + RegisterMetricReader("otlp", func(ctx context.Context) (metric.Reader, error) { |
| 72 | + proto := os.Getenv(otelExporterOTLPProtoEnvKey) |
| 73 | + if proto == "" { |
| 74 | + proto = "http/protobuf" |
| 75 | + } |
| 76 | + |
| 77 | + switch proto { |
| 78 | + case "grpc": |
| 79 | + r, err := otlpmetricgrpc.New(ctx) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + return metric.NewPeriodicReader(r), nil |
| 84 | + case "http/protobuf": |
| 85 | + r, err := otlpmetrichttp.New(ctx) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + return metric.NewPeriodicReader(r), nil |
| 90 | + default: |
| 91 | + return nil, errInvalidOTLPProtocol |
| 92 | + } |
| 93 | + }) |
| 94 | + RegisterMetricReader("none", func(ctx context.Context) (metric.Reader, error) { |
| 95 | + return newNoopMetricReader(), nil |
| 96 | + }) |
| 97 | +} |
0 commit comments