Debian Codegraph @

instrumentation/runtime/internal/x/x.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Package x contains support for OTel runtime instrumentation 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.
8 package x // import "go.opentelemetry.io/contrib/instrumentation/runtime/internal/x"
9
10 import (
11 "os"
12 "strconv"
13 )
14
15 // DeprecatedRuntimeMetrics is an experimental feature flag that defines if the deprecated
16 // runtime metrics should be produced. During development of the new
17 // conventions, it is enabled by default.
18 //
19 // To enable this feature set the OTEL_GO_X_DEPRECATED_RUNTIME_METRICS environment variable
20 // to the case-insensitive string value of "true" (i.e. "True" and "TRUE"
21 // will also enable this).
22 var DeprecatedRuntimeMetrics = newFeature("DEPRECATED_RUNTIME_METRICS", false)
23
24 // BoolFeature is an experimental feature control flag. It provides a uniform way
25 // to interact with these feature flags and parse their values.
26 type BoolFeature struct {
27 key string
28 defaultVal bool
29 }
30
31 func newFeature(suffix string, defaultVal bool) BoolFeature {
32 const envKeyRoot = "OTEL_GO_X_"
33 return BoolFeature{
34 key: envKeyRoot + suffix,
35 defaultVal: defaultVal,
36 }
37 }
38
39 // Key returns the environment variable key that needs to be set to enable the
40 // feature.
41 func (f BoolFeature) Key() string { return f.key }
42
43 // Enabled returns if the feature is enabled.
44 func (f BoolFeature) Enabled() bool {
45 v := os.Getenv(f.key)
46
47 val, err := strconv.ParseBool(v)
48 if err != nil {
49 return f.defaultVal
50 }
51
52 return val
53 }