env.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package env // import "go.opentelemetry.io/otel/sdk/internal/env"
  15. import (
  16. "os"
  17. "strconv"
  18. "go.opentelemetry.io/otel/internal/global"
  19. )
  20. // Environment variable names.
  21. const (
  22. // BatchSpanProcessorScheduleDelayKey is the delay interval between two
  23. // consecutive exports (i.e. 5000).
  24. BatchSpanProcessorScheduleDelayKey = "OTEL_BSP_SCHEDULE_DELAY"
  25. // BatchSpanProcessorExportTimeoutKey is the maximum allowed time to
  26. // export data (i.e. 3000).
  27. BatchSpanProcessorExportTimeoutKey = "OTEL_BSP_EXPORT_TIMEOUT"
  28. // BatchSpanProcessorMaxQueueSizeKey is the maximum queue size (i.e. 2048).
  29. BatchSpanProcessorMaxQueueSizeKey = "OTEL_BSP_MAX_QUEUE_SIZE"
  30. // BatchSpanProcessorMaxExportBatchSizeKey is the maximum batch size (i.e.
  31. // 512). Note: it must be less than or equal to
  32. // EnvBatchSpanProcessorMaxQueueSize.
  33. BatchSpanProcessorMaxExportBatchSizeKey = "OTEL_BSP_MAX_EXPORT_BATCH_SIZE"
  34. // AttributeValueLengthKey is the maximum allowed attribute value size.
  35. AttributeValueLengthKey = "OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT"
  36. // AttributeCountKey is the maximum allowed span attribute count.
  37. AttributeCountKey = "OTEL_ATTRIBUTE_COUNT_LIMIT"
  38. // SpanAttributeValueLengthKey is the maximum allowed attribute value size
  39. // for a span.
  40. SpanAttributeValueLengthKey = "OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT"
  41. // SpanAttributeCountKey is the maximum allowed span attribute count for a
  42. // span.
  43. SpanAttributeCountKey = "OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT"
  44. // SpanEventCountKey is the maximum allowed span event count.
  45. SpanEventCountKey = "OTEL_SPAN_EVENT_COUNT_LIMIT"
  46. // SpanEventAttributeCountKey is the maximum allowed attribute per span
  47. // event count.
  48. SpanEventAttributeCountKey = "OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT"
  49. // SpanLinkCountKey is the maximum allowed span link count.
  50. SpanLinkCountKey = "OTEL_SPAN_LINK_COUNT_LIMIT"
  51. // SpanLinkAttributeCountKey is the maximum allowed attribute per span
  52. // link count.
  53. SpanLinkAttributeCountKey = "OTEL_LINK_ATTRIBUTE_COUNT_LIMIT"
  54. )
  55. // firstInt returns the value of the first matching environment variable from
  56. // keys. If the value is not an integer or no match is found, defaultValue is
  57. // returned.
  58. func firstInt(defaultValue int, keys ...string) int {
  59. for _, key := range keys {
  60. value, ok := os.LookupEnv(key)
  61. if !ok {
  62. continue
  63. }
  64. intValue, err := strconv.Atoi(value)
  65. if err != nil {
  66. global.Info("Got invalid value, number value expected.", key, value)
  67. return defaultValue
  68. }
  69. return intValue
  70. }
  71. return defaultValue
  72. }
  73. // IntEnvOr returns the int value of the environment variable with name key if
  74. // it exists and the value is an int. Otherwise, defaultValue is returned.
  75. func IntEnvOr(key string, defaultValue int) int {
  76. value, ok := os.LookupEnv(key)
  77. if !ok {
  78. return defaultValue
  79. }
  80. intValue, err := strconv.Atoi(value)
  81. if err != nil {
  82. global.Info("Got invalid value, number value expected.", key, value)
  83. return defaultValue
  84. }
  85. return intValue
  86. }
  87. // BatchSpanProcessorScheduleDelay returns the environment variable value for
  88. // the OTEL_BSP_SCHEDULE_DELAY key if it exists, otherwise defaultValue is
  89. // returned.
  90. func BatchSpanProcessorScheduleDelay(defaultValue int) int {
  91. return IntEnvOr(BatchSpanProcessorScheduleDelayKey, defaultValue)
  92. }
  93. // BatchSpanProcessorExportTimeout returns the environment variable value for
  94. // the OTEL_BSP_EXPORT_TIMEOUT key if it exists, otherwise defaultValue is
  95. // returned.
  96. func BatchSpanProcessorExportTimeout(defaultValue int) int {
  97. return IntEnvOr(BatchSpanProcessorExportTimeoutKey, defaultValue)
  98. }
  99. // BatchSpanProcessorMaxQueueSize returns the environment variable value for
  100. // the OTEL_BSP_MAX_QUEUE_SIZE key if it exists, otherwise defaultValue is
  101. // returned.
  102. func BatchSpanProcessorMaxQueueSize(defaultValue int) int {
  103. return IntEnvOr(BatchSpanProcessorMaxQueueSizeKey, defaultValue)
  104. }
  105. // BatchSpanProcessorMaxExportBatchSize returns the environment variable value for
  106. // the OTEL_BSP_MAX_EXPORT_BATCH_SIZE key if it exists, otherwise defaultValue
  107. // is returned.
  108. func BatchSpanProcessorMaxExportBatchSize(defaultValue int) int {
  109. return IntEnvOr(BatchSpanProcessorMaxExportBatchSizeKey, defaultValue)
  110. }
  111. // SpanAttributeValueLength returns the environment variable value for the
  112. // OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT key if it exists. Otherwise, the
  113. // environment variable value for OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT is
  114. // returned or defaultValue if that is not set.
  115. func SpanAttributeValueLength(defaultValue int) int {
  116. return firstInt(defaultValue, SpanAttributeValueLengthKey, AttributeValueLengthKey)
  117. }
  118. // SpanAttributeCount returns the environment variable value for the
  119. // OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT key if it exists. Otherwise, the
  120. // environment variable value for OTEL_ATTRIBUTE_COUNT_LIMIT is returned or
  121. // defaultValue if that is not set.
  122. func SpanAttributeCount(defaultValue int) int {
  123. return firstInt(defaultValue, SpanAttributeCountKey, AttributeCountKey)
  124. }
  125. // SpanEventCount returns the environment variable value for the
  126. // OTEL_SPAN_EVENT_COUNT_LIMIT key if it exists, otherwise defaultValue is
  127. // returned.
  128. func SpanEventCount(defaultValue int) int {
  129. return IntEnvOr(SpanEventCountKey, defaultValue)
  130. }
  131. // SpanEventAttributeCount returns the environment variable value for the
  132. // OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT key if it exists, otherwise defaultValue
  133. // is returned.
  134. func SpanEventAttributeCount(defaultValue int) int {
  135. return IntEnvOr(SpanEventAttributeCountKey, defaultValue)
  136. }
  137. // SpanLinkCount returns the environment variable value for the
  138. // OTEL_SPAN_LINK_COUNT_LIMIT key if it exists, otherwise defaultValue is
  139. // returned.
  140. func SpanLinkCount(defaultValue int) int {
  141. return IntEnvOr(SpanLinkCountKey, defaultValue)
  142. }
  143. // SpanLinkAttributeCount returns the environment variable value for the
  144. // OTEL_LINK_ATTRIBUTE_COUNT_LIMIT key if it exists, otherwise defaultValue is
  145. // returned.
  146. func SpanLinkAttributeCount(defaultValue int) int {
  147. return IntEnvOr(SpanLinkAttributeCountKey, defaultValue)
  148. }