| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/x/x.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
// Package x documents experimental features for [go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc]. |
| 8 |
package x // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/x" |
| 9 |
|
| 10 |
import ( |
| 11 |
"os" |
| 12 |
) |
| 13 |
|
| 14 |
// Feature is an experimental feature control flag. It provides a uniform way |
| 15 |
// to interact with these feature flags and parse their values. |
| 16 |
type Feature[T any] struct { |
| 17 |
keys []string |
| 18 |
parse func(v string) (T, bool) |
| 19 |
} |
| 20 |
|
| 21 |
func newFeature[T any](suffix []string, parse func(string) (T, bool)) Feature[T] { |
| 22 |
const envKeyRoot = "OTEL_GO_X_" |
| 23 |
keys := make([]string, 0, len(suffix)) |
| 24 |
for _, s := range suffix { |
| 25 |
keys = append(keys, envKeyRoot+s) |
| 26 |
} |
| 27 |
return Feature[T]{ |
| 28 |
keys: keys, |
| 29 |
parse: parse, |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
// Keys returns the environment variable keys that can be set to enable the |
| 34 |
// feature. |
| 35 |
func (f Feature[T]) Keys() []string { return f.keys } |
| 36 |
|
| 37 |
// Lookup returns the user configured value for the feature and true if the |
| 38 |
// user has enabled the feature. Otherwise, if the feature is not enabled, a |
| 39 |
// zero-value and false are returned. |
| 40 |
func (f Feature[T]) Lookup() (v T, ok bool) { |
| 41 |
// https://github.com/open-telemetry/opentelemetry-specification/blob/62effed618589a0bec416a87e559c0a9d96289bb/specification/configuration/sdk-environment-variables.md#parsing-empty-value |
| 42 |
// |
| 43 |
// > The SDK MUST interpret an empty value of an environment variable the |
| 44 |
// > same way as when the variable is unset. |
| 45 |
for _, key := range f.keys { |
| 46 |
vRaw := os.Getenv(key) |
| 47 |
if vRaw != "" { |
| 48 |
return f.parse(vRaw) |
| 49 |
} |
| 50 |
} |
| 51 |
return v, ok |
| 52 |
} |
| 53 |
|
| 54 |
// Enabled reports whether the feature is enabled. |
| 55 |
func (f Feature[T]) Enabled() bool { |
| 56 |
_, ok := f.Lookup() |
| 57 |
return ok |
| 58 |
} |