blob: e3309231d42391451e38c37e9ba82550f77e7004 [file] [log] [blame]
Abhay Kumara2ae5992025-11-10 14:02:24 +00001// Copyright The OpenTelemetry Authors
2// SPDX-License-Identifier: Apache-2.0
3
4// Package env provides types and functionality for environment variable support
5// in the OpenTelemetry SDK.
6package env // import "go.opentelemetry.io/otel/sdk/internal/env"
7
8import (
9 "os"
10 "strconv"
11
12 "go.opentelemetry.io/otel/internal/global"
13)
14
15// Environment variable names.
16const (
17 // BatchSpanProcessorScheduleDelayKey is the delay interval between two
18 // consecutive exports (i.e. 5000).
19 BatchSpanProcessorScheduleDelayKey = "OTEL_BSP_SCHEDULE_DELAY"
20 // BatchSpanProcessorExportTimeoutKey is the maximum allowed time to
21 // export data (i.e. 3000).
22 BatchSpanProcessorExportTimeoutKey = "OTEL_BSP_EXPORT_TIMEOUT"
23 // BatchSpanProcessorMaxQueueSizeKey is the maximum queue size (i.e. 2048).
24 BatchSpanProcessorMaxQueueSizeKey = "OTEL_BSP_MAX_QUEUE_SIZE"
25 // BatchSpanProcessorMaxExportBatchSizeKey is the maximum batch size (i.e.
26 // 512). Note: it must be less than or equal to
27 // BatchSpanProcessorMaxQueueSize.
28 BatchSpanProcessorMaxExportBatchSizeKey = "OTEL_BSP_MAX_EXPORT_BATCH_SIZE"
29
30 // AttributeValueLengthKey is the maximum allowed attribute value size.
31 AttributeValueLengthKey = "OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT"
32
33 // AttributeCountKey is the maximum allowed span attribute count.
34 AttributeCountKey = "OTEL_ATTRIBUTE_COUNT_LIMIT"
35
36 // SpanAttributeValueLengthKey is the maximum allowed attribute value size
37 // for a span.
38 SpanAttributeValueLengthKey = "OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT"
39
40 // SpanAttributeCountKey is the maximum allowed span attribute count for a
41 // span.
42 SpanAttributeCountKey = "OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT"
43
44 // SpanEventCountKey is the maximum allowed span event count.
45 SpanEventCountKey = "OTEL_SPAN_EVENT_COUNT_LIMIT"
46
47 // SpanEventAttributeCountKey is the maximum allowed attribute per span
48 // event count.
49 SpanEventAttributeCountKey = "OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT"
50
51 // SpanLinkCountKey is the maximum allowed span link count.
52 SpanLinkCountKey = "OTEL_SPAN_LINK_COUNT_LIMIT"
53
54 // SpanLinkAttributeCountKey is the maximum allowed attribute per span
55 // link count.
56 SpanLinkAttributeCountKey = "OTEL_LINK_ATTRIBUTE_COUNT_LIMIT"
57)
58
59// firstInt returns the value of the first matching environment variable from
60// keys. If the value is not an integer or no match is found, defaultValue is
61// returned.
62func firstInt(defaultValue int, keys ...string) int {
63 for _, key := range keys {
64 value := os.Getenv(key)
65 if value == "" {
66 continue
67 }
68
69 intValue, err := strconv.Atoi(value)
70 if err != nil {
71 global.Info("Got invalid value, number value expected.", key, value)
72 return defaultValue
73 }
74
75 return intValue
76 }
77
78 return defaultValue
79}
80
81// IntEnvOr returns the int value of the environment variable with name key if
82// it exists, it is not empty, and the value is an int. Otherwise, defaultValue is returned.
83func IntEnvOr(key string, defaultValue int) int {
84 value := os.Getenv(key)
85 if value == "" {
86 return defaultValue
87 }
88
89 intValue, err := strconv.Atoi(value)
90 if err != nil {
91 global.Info("Got invalid value, number value expected.", key, value)
92 return defaultValue
93 }
94
95 return intValue
96}
97
98// BatchSpanProcessorScheduleDelay returns the environment variable value for
99// the OTEL_BSP_SCHEDULE_DELAY key if it exists, otherwise defaultValue is
100// returned.
101func BatchSpanProcessorScheduleDelay(defaultValue int) int {
102 return IntEnvOr(BatchSpanProcessorScheduleDelayKey, defaultValue)
103}
104
105// BatchSpanProcessorExportTimeout returns the environment variable value for
106// the OTEL_BSP_EXPORT_TIMEOUT key if it exists, otherwise defaultValue is
107// returned.
108func BatchSpanProcessorExportTimeout(defaultValue int) int {
109 return IntEnvOr(BatchSpanProcessorExportTimeoutKey, defaultValue)
110}
111
112// BatchSpanProcessorMaxQueueSize returns the environment variable value for
113// the OTEL_BSP_MAX_QUEUE_SIZE key if it exists, otherwise defaultValue is
114// returned.
115func BatchSpanProcessorMaxQueueSize(defaultValue int) int {
116 return IntEnvOr(BatchSpanProcessorMaxQueueSizeKey, defaultValue)
117}
118
119// BatchSpanProcessorMaxExportBatchSize returns the environment variable value for
120// the OTEL_BSP_MAX_EXPORT_BATCH_SIZE key if it exists, otherwise defaultValue
121// is returned.
122func BatchSpanProcessorMaxExportBatchSize(defaultValue int) int {
123 return IntEnvOr(BatchSpanProcessorMaxExportBatchSizeKey, defaultValue)
124}
125
126// SpanAttributeValueLength returns the environment variable value for the
127// OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT key if it exists. Otherwise, the
128// environment variable value for OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT is
129// returned or defaultValue if that is not set.
130func SpanAttributeValueLength(defaultValue int) int {
131 return firstInt(defaultValue, SpanAttributeValueLengthKey, AttributeValueLengthKey)
132}
133
134// SpanAttributeCount returns the environment variable value for the
135// OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT key if it exists. Otherwise, the
136// environment variable value for OTEL_ATTRIBUTE_COUNT_LIMIT is returned or
137// defaultValue if that is not set.
138func SpanAttributeCount(defaultValue int) int {
139 return firstInt(defaultValue, SpanAttributeCountKey, AttributeCountKey)
140}
141
142// SpanEventCount returns the environment variable value for the
143// OTEL_SPAN_EVENT_COUNT_LIMIT key if it exists, otherwise defaultValue is
144// returned.
145func SpanEventCount(defaultValue int) int {
146 return IntEnvOr(SpanEventCountKey, defaultValue)
147}
148
149// SpanEventAttributeCount returns the environment variable value for the
150// OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT key if it exists, otherwise defaultValue
151// is returned.
152func SpanEventAttributeCount(defaultValue int) int {
153 return IntEnvOr(SpanEventAttributeCountKey, defaultValue)
154}
155
156// SpanLinkCount returns the environment variable value for the
157// OTEL_SPAN_LINK_COUNT_LIMIT key if it exists, otherwise defaultValue is
158// returned.
159func SpanLinkCount(defaultValue int) int {
160 return IntEnvOr(SpanLinkCountKey, defaultValue)
161}
162
163// SpanLinkAttributeCount returns the environment variable value for the
164// OTEL_LINK_ATTRIBUTE_COUNT_LIMIT key if it exists, otherwise defaultValue is
165// returned.
166func SpanLinkAttributeCount(defaultValue int) int {
167 return IntEnvOr(SpanLinkAttributeCountKey, defaultValue)
168}