...

Source file src/go.opentelemetry.io/otel/sdk/internal/env/env.go

Documentation: go.opentelemetry.io/otel/sdk/internal/env

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

View as plain text