blob: 68d296cbed3618747047571840c731bcdf2ae4dc [file] [log] [blame]
Abhay Kumar40252eb2025-10-13 13:25:53 +00001// Copyright The OpenTelemetry Authors
2// SPDX-License-Identifier: Apache-2.0
3
4// Package x contains support for OTel SDK experimental features.
5//
6// This package should only be used for features defined in the specification.
7// It should not be used for experiments or new project ideas.
8package x // import "go.opentelemetry.io/otel/sdk/internal/x"
9
10import (
11 "os"
12 "strings"
13)
14
15// Resource is an experimental feature flag that defines if resource detectors
16// should be included experimental semantic conventions.
17//
18// To enable this feature set the OTEL_GO_X_RESOURCE environment variable
19// to the case-insensitive string value of "true" (i.e. "True" and "TRUE"
20// will also enable this).
21var Resource = newFeature("RESOURCE", func(v string) (string, bool) {
22 if strings.ToLower(v) == "true" {
23 return v, true
24 }
25 return "", false
26})
27
28// Feature is an experimental feature control flag. It provides a uniform way
29// to interact with these feature flags and parse their values.
30type Feature[T any] struct {
31 key string
32 parse func(v string) (T, bool)
33}
34
35func newFeature[T any](suffix string, parse func(string) (T, bool)) Feature[T] {
36 const envKeyRoot = "OTEL_GO_X_"
37 return Feature[T]{
38 key: envKeyRoot + suffix,
39 parse: parse,
40 }
41}
42
43// Key returns the environment variable key that needs to be set to enable the
44// feature.
45func (f Feature[T]) Key() string { return f.key }
46
47// Lookup returns the user configured value for the feature and true if the
48// user has enabled the feature. Otherwise, if the feature is not enabled, a
49// zero-value and false are returned.
50func (f Feature[T]) Lookup() (v T, ok bool) {
51 // https://github.com/open-telemetry/opentelemetry-specification/blob/62effed618589a0bec416a87e559c0a9d96289bb/specification/configuration/sdk-environment-variables.md#parsing-empty-value
52 //
53 // > The SDK MUST interpret an empty value of an environment variable the
54 // > same way as when the variable is unset.
55 vRaw := os.Getenv(f.key)
56 if vRaw == "" {
57 return v, ok
58 }
59 return f.parse(vRaw)
60}
61
62// Enabled returns if the feature is enabled.
63func (f Feature[T]) Enabled() bool {
64 _, ok := f.Lookup()
65 return ok
66}