Debian Codegraph @

sdk/metric/env.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package metric // import "go.opentelemetry.io/otel/sdk/metric"
5
6 import (
7 "os"
8 "strconv"
9 "time"
10
11 "go.opentelemetry.io/otel/internal/global"
12 )
13
14 // Environment variable names.
15 const (
16 // The time interval (in milliseconds) between the start of two export attempts.
17 envInterval = "OTEL_METRIC_EXPORT_INTERVAL"
18 // Maximum allowed time (in milliseconds) to export data.
19 envTimeout = "OTEL_METRIC_EXPORT_TIMEOUT"
20 )
21
22 // envDuration returns an environment variable's value as duration in milliseconds if it is exists,
23 // or the defaultValue if the environment variable is not defined or the value is not valid.
24 func envDuration(key string, defaultValue time.Duration) time.Duration {
25 v := os.Getenv(key)
26 if v == "" {
27 return defaultValue
28 }
29 d, err := strconv.Atoi(v)
30 if err != nil {
31 global.Error(err, "parse duration", "environment variable", key, "value", v)
32 return defaultValue
33 }
34 if d <= 0 {
35 global.Error(errNonPositiveDuration, "non-positive duration", "environment variable", key, "value", v)
36 return defaultValue
37 }
38 return time.Duration(d) * time.Millisecond
39 }