1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-27 22:49:15 +02:00
Files
opentelemetry-go/exporters/prometheus/internal/x/x.go

59 lines
1.8 KiB
Go
Raw Normal View History

// Code generated by gotmpl. DO NOT MODIFY.
// source: internal/shared/x/x.go.tmpl
feat(prometheus): Add observability for prometheus exporter (#7345) fix #7013 References: - [Follow guidelines](https://github.com/open-telemetry/opentelemetry-go/blob/a5dcd68ebb2f3669f7685ac7b0f3f1624251a381/CONTRIBUTING.md#encapsulation). - PR: #7307 Implement following self-observability metrics from https://github.com/open-telemetry/semantic-conventions/blob/v1.36.0/docs/otel/sdk-metrics.md for https://pkg.go.dev/go.opentelemetry.io/otel/exporters/prometheus: - otel.sdk.exporter.metric_data_point.inflight - otel.sdk.exporter.metric_data_point.exported - otel.sdk.exporter.operation.duration - otel.sdk.metric_reader.collection.duration ### Benchmarks ```console ➜ benchstat /tmp/bench_disabled.txt /tmp/bench_enabled.txt goos: darwin goarch: arm64 pkg: go.opentelemetry.io/otel/exporters/prometheus/internal/observ cpu: Apple M1 Max │ /tmp/bench_disabled.txt │ /tmp/bench_enabled.txt │ │ sec/op │ sec/op vs base │ InstrumentationExportMetrics-10 177.5n ± 0% 177.8n ± 0% +0.14% (p=0.039 n=20) InstrumentationRecordOperationDuration-10 246.6n ± 0% 246.7n ± 0% ~ (p=0.606 n=20) InstrumentationRecordCollectionDuration-10 246.8n ± 1% 247.2n ± 0% ~ (p=0.456 n=20) geomean 221.1n 221.3n +0.09% │ /tmp/bench_disabled.txt │ /tmp/bench_enabled.txt │ │ B/op │ B/op vs base │ InstrumentationExportMetrics-10 256.0 ± 0% 256.0 ± 0% ~ (p=1.000 n=20) ¹ InstrumentationRecordOperationDuration-10 272.0 ± 0% 272.0 ± 0% ~ (p=1.000 n=20) ¹ InstrumentationRecordCollectionDuration-10 272.0 ± 0% 272.0 ± 0% ~ (p=1.000 n=20) ¹ geomean 266.6 266.6 +0.00% ¹ all samples are equal │ /tmp/bench_disabled.txt │ /tmp/bench_enabled.txt │ │ allocs/op │ allocs/op vs base │ InstrumentationExportMetrics-10 3.000 ± 0% 3.000 ± 0% ~ (p=1.000 n=20) ¹ InstrumentationRecordOperationDuration-10 3.000 ± 0% 3.000 ± 0% ~ (p=1.000 n=20) ¹ InstrumentationRecordCollectionDuration-10 3.000 ± 0% 3.000 ± 0% ~ (p=1.000 n=20) ¹ geomean 3.000 3.000 +0.00% ¹ all samples are equal ``` --------- Co-authored-by: Flc゛ <four_leaf_clover@foxmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
2025-09-18 10:52:54 -04:00
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Package x documents experimental features for [go.opentelemetry.io/otel/exporters/prometheus].
package x // import "go.opentelemetry.io/otel/exporters/prometheus/internal/x"
import (
"os"
)
// Feature is an experimental feature control flag. It provides a uniform way
// to interact with these feature flags and parse their values.
type Feature[T any] struct {
keys []string
parse func(v string) (T, bool)
}
func newFeature[T any](suffix []string, parse func(string) (T, bool)) Feature[T] {
const envKeyRoot = "OTEL_GO_X_"
keys := make([]string, 0, len(suffix))
for _, s := range suffix {
keys = append(keys, envKeyRoot+s)
}
return Feature[T]{
keys: keys,
parse: parse,
}
}
// Keys returns the environment variable keys that can be set to enable the
// feature.
func (f Feature[T]) Keys() []string { return f.keys }
// Lookup returns the user configured value for the feature and true if the
// user has enabled the feature. Otherwise, if the feature is not enabled, a
// zero-value and false are returned.
func (f Feature[T]) Lookup() (v T, ok bool) {
// https://github.com/open-telemetry/opentelemetry-specification/blob/62effed618589a0bec416a87e559c0a9d96289bb/specification/configuration/sdk-environment-variables.md#parsing-empty-value
//
// > The SDK MUST interpret an empty value of an environment variable the
// > same way as when the variable is unset.
for _, key := range f.keys {
vRaw := os.Getenv(key)
if vRaw != "" {
return f.parse(vRaw)
}
}
return v, ok
}
// Enabled reports whether the feature is enabled.
func (f Feature[T]) Enabled() bool {
_, ok := f.Lookup()
return ok
}