-
Notifications
You must be signed in to change notification settings - Fork 682
autoexport: Add support for metrics #4229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
80d77f8
autoexport: Add support for metrics
punya 78ef081
Format and fix typo
punya 0b6c412
Add changelog entry
punya 3ea1366
Factor registry into metrics/spans/shared
punya 3ae284a
Start adding metrics-related tests
punya 155c74e
Generalize tests to metrics
punya 2999fd4
Fix deps
punya d498e8e
Tests
punya c33d73f
Add support for Prometheus exporter
punya 8c40f0a
Rename signal -> fixture in tests
punya ffd185b
Rewrite + respond to PR feedback
punya 0f350db
Reduce unnecessarily fancy generics in tests
punya 6d04d4c
Remove unnecessary generics in tests
punya 5cf5027
Remove unused deps
punya 4f0e614
Cleanup
punya 90eeff5
Cleanup and lint fixes
punya 6c1e67c
Fix copypasta and improve names
punya 1e68beb
Tests for unspecified protocol
punya 9cacbfc
Merge remote-tracking branch 'origin/main' into autoexport-metrics
punya b8c8b35
Appease gofumpt
punya 1af21f1
DRY panic logic
punya 2b08d9f
Avoid unnecessary require block
punya 37ac9d5
Fix documentation
punya 8f6c1bb
Use SpanOption rather than deprecated alias
punya b1a5584
Remove Prometheus support - deferred to #4472
punya 3ecc111
Merge branch 'main' into autoexport-metrics
punya a8b2ef7
Move CHANGELOG entry and add deprecation
punya 503e4ff
Update CHANGELOG.md
pellared File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport" | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" | ||
"go.opentelemetry.io/otel/exporters/prometheus" | ||
"go.opentelemetry.io/otel/sdk/metric" | ||
) | ||
|
||
// MetricOption applies an autoexport configuration option. | ||
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(exporter metric.Reader) MetricOption { | ||
return withFallback[metric.Reader](exporter) | ||
} | ||
|
||
// NewMetricReader returns a configured [go.opentelemetry.io/otel/sdk/metric.Reader] | ||
// defined using the environment variables described below. | ||
// | ||
// OTEL_METRICS_EXPORTER defines the metrics exporter; supported values: | ||
// - "none" - "no operation" exporter | ||
// - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlpmetric] | ||
// - "prometheus" - Prometheus exporter; see [go.opentelemetry.io/otel/exporters/prometheus] | ||
// | ||
// OTEL_EXPORTER_OTLP_PROTOCOL defines OTLP exporter's transport protocol; | ||
// supported values: | ||
// - "grpc" - protobuf-encoded data using gRPC wire format over HTTP/2 connection; | ||
// see: [go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc] | ||
// - "http/protobuf" (default) - protobuf-encoded data over HTTP connection; | ||
// see: [go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp] | ||
// | ||
// An error is returned if an environment value is set to an unhandled value. | ||
// | ||
// Use [RegisterMetricReader] to handle more values of OTEL_METRICS_EXPORTER. | ||
// | ||
// Use [WithFallbackMetricReader] option to change the returned exporter | ||
// when OTEL_TRACES_EXPORTER is unset or empty. | ||
// | ||
// Use [IsNoneMetricReader] to check if the retured exporter is a "no operation" exporter. | ||
func NewMetricReader(ctx context.Context, opts ...MetricOption) (metric.Reader, error) { | ||
return metricsSignal.create(ctx, opts...) | ||
} | ||
|
||
// 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) (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) (metric.Reader, error) { | ||
proto := os.Getenv(otelExporterOTLPProtoEnvKey) | ||
if proto == "" { | ||
proto = "http/protobuf" | ||
} | ||
|
||
switch proto { | ||
case "grpc": | ||
r, err := otlpmetricgrpc.New(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return metric.NewPeriodicReader(r), nil | ||
case "http/protobuf": | ||
r, err := otlpmetrichttp.New(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return metric.NewPeriodicReader(r), nil | ||
default: | ||
return nil, errInvalidOTLPProtocol | ||
} | ||
}) | ||
RegisterMetricReader("prometheus", func(ctx context.Context) (metric.Reader, error) { | ||
return prometheus.New() | ||
punya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
RegisterMetricReader("none", func(ctx context.Context) (metric.Reader, error) { | ||
return newNoopMetricReader(), nil | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.