summaryrefslogtreecommitdiff
path: root/vendor/go.opentelemetry.io/contrib/instrumentation/runtime/internal/x/x.go
blob: 95a05d5993ec9b8f687e1a4656b92a284c5fe5f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Package x contains support for OTel runtime instrumentation experimental features.
//
// This package should only be used for features defined in the specification.
// It should not be used for experiments or new project ideas.
package x // import "go.opentelemetry.io/contrib/instrumentation/runtime/internal/x"

import (
	"os"
	"strconv"
)

// DeprecatedRuntimeMetrics is an experimental feature flag that defines if the deprecated
// runtime metrics should be produced. During development of the new
// conventions, it is enabled by default.
//
// To enable this feature set the OTEL_GO_X_DEPRECATED_RUNTIME_METRICS environment variable
// to the case-insensitive string value of "true" (i.e. "True" and "TRUE"
// will also enable this).
var DeprecatedRuntimeMetrics = newFeature("DEPRECATED_RUNTIME_METRICS", false)

// BoolFeature is an experimental feature control flag. It provides a uniform way
// to interact with these feature flags and parse their values.
type BoolFeature struct {
	key        string
	defaultVal bool
}

func newFeature(suffix string, defaultVal bool) BoolFeature {
	const envKeyRoot = "OTEL_GO_X_"
	return BoolFeature{
		key:        envKeyRoot + suffix,
		defaultVal: defaultVal,
	}
}

// Key returns the environment variable key that needs to be set to enable the
// feature.
func (f BoolFeature) Key() string { return f.key }

// Enabled returns if the feature is enabled.
func (f BoolFeature) Enabled() bool {
	v := os.Getenv(f.key)

	val, err := strconv.ParseBool(v)
	if err != nil {
		return f.defaultVal
	}

	return val
}