1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00
Files
opentelemetry-go/sdk/internal/x/x.go
T

59 lines
1.7 KiB
Go
Raw Normal View History

// Code generated by gotmpl. DO NOT MODIFY.
// source: internal/shared/x/x.go.tmpl
2024-06-05 09:00:57 -07:00
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
// Package x documents experimental features for [go.opentelemetry.io/otel/sdk].
2024-06-05 09:00:57 -07:00
package x // import "go.opentelemetry.io/otel/sdk/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
2024-06-05 09:00:57 -07:00
parse func(v string) (T, bool)
}
func newFeature[T any](suffix []string, parse func(string) (T, bool)) Feature[T] {
2024-06-05 09:00:57 -07:00
const envKeyRoot = "OTEL_GO_X_"
keys := make([]string, 0, len(suffix))
for _, s := range suffix {
keys = append(keys, envKeyRoot+s)
}
2024-06-05 09:00:57 -07:00
return Feature[T]{
keys: keys,
2024-06-05 09:00:57 -07:00
parse: parse,
}
}
// Keys returns the environment variable keys that can be set to enable the
2024-06-05 09:00:57 -07:00
// feature.
func (f Feature[T]) Keys() []string { return f.keys }
2024-06-05 09:00:57 -07:00
// 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)
}
2024-06-05 09:00:57 -07:00
}
return v, ok
2024-06-05 09:00:57 -07:00
}
// Enabled reports whether the feature is enabled.
2024-06-05 09:00:57 -07:00
func (f Feature[T]) Enabled() bool {
_, ok := f.Lookup()
return ok
}