| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package log // import "go.opentelemetry.io/otel/sdk/log" |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"os" |
| 9 |
"strconv" |
| 10 |
"time" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel" |
| 13 |
) |
| 14 |
|
| 15 |
// setting is a configuration setting value. |
| 16 |
type setting[T any] struct { |
| 17 |
Value T |
| 18 |
Set bool |
| 19 |
} |
| 20 |
|
| 21 |
// newSetting returns a new [setting] with the value set. |
| 22 |
func newSetting[T any](value T) setting[T] { |
| 23 |
return setting[T]{Value: value, Set: true} |
| 24 |
} |
| 25 |
|
| 26 |
// resolver returns an updated setting after applying an resolution operation. |
| 27 |
type resolver[T any] func(setting[T]) setting[T] |
| 28 |
|
| 29 |
// Resolve returns a resolved version of s. |
| 30 |
// |
| 31 |
// It will apply all the passed fn in the order provided, chaining together the |
| 32 |
// return setting to the next input. The setting s is used as the initial |
| 33 |
// argument to the first fn. |
| 34 |
// |
| 35 |
// Each fn needs to validate if it should apply given the Set state of the |
| 36 |
// setting. This will not perform any checks on the set state when chaining |
| 37 |
// function. |
| 38 |
func (s setting[T]) Resolve(fn ...resolver[T]) setting[T] { |
| 39 |
for _, f := range fn { |
| 40 |
s = f(s) |
| 41 |
} |
| 42 |
return s |
| 43 |
} |
| 44 |
|
| 45 |
// clampMax returns a resolver that will ensure a setting value is no greater |
| 46 |
// than n. If it is, the value is set to n. |
| 47 |
func clampMax[T ~int | ~int64](n T) resolver[T] { |
| 48 |
return func(s setting[T]) setting[T] { |
| 49 |
if s.Value > n { |
| 50 |
s.Value = n |
| 51 |
} |
| 52 |
return s |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// clearLessThanOne returns a resolver that will clear a setting value and |
| 57 |
// change its set state to false if its value is less than 1. |
| 58 |
func clearLessThanOne[T ~int | ~int64]() resolver[T] { |
| 59 |
return func(s setting[T]) setting[T] { |
| 60 |
if s.Value < 1 { |
| 61 |
s.Value = 0 |
| 62 |
s.Set = false |
| 63 |
} |
| 64 |
return s |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// getenv returns a resolver that will apply an integer environment variable |
| 69 |
// value associated with key to a setting value. |
| 70 |
// |
| 71 |
// If the input setting to the resolver is set, the environment variable will |
| 72 |
// not be applied. |
| 73 |
// |
| 74 |
// If the environment variable value associated with key is not an integer, an |
| 75 |
// error will be sent to the OTel error handler and the setting will not be |
| 76 |
// updated. |
| 77 |
// |
| 78 |
// If the setting value is a [time.Duration] type, the environment variable |
| 79 |
// will be interpreted as a duration of milliseconds. |
| 80 |
func getenv[T ~int | ~int64](key string) resolver[T] { |
| 81 |
return func(s setting[T]) setting[T] { |
| 82 |
if s.Set { |
| 83 |
// Passed, valid, options have precedence. |
| 84 |
return s |
| 85 |
} |
| 86 |
|
| 87 |
if v := os.Getenv(key); v != "" { |
| 88 |
n, err := strconv.Atoi(v) |
| 89 |
if err != nil { |
| 90 |
otel.Handle(fmt.Errorf("invalid %s value %s: %w", key, v, err)) |
| 91 |
} else { |
| 92 |
switch any(s.Value).(type) { |
| 93 |
case time.Duration: |
| 94 |
// OTel duration envar are in millisecond. |
| 95 |
s.Value = T(time.Duration(n) * time.Millisecond) |
| 96 |
default: |
| 97 |
s.Value = T(n) |
| 98 |
} |
| 99 |
s.Set = true |
| 100 |
} |
| 101 |
} |
| 102 |
return s |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// fallback returns a resolve that will set a setting value to val if it is not |
| 107 |
// already set. |
| 108 |
// |
| 109 |
// This is usually passed at the end of a resolver chain to ensure a default is |
| 110 |
// applied if the setting has not already been set. |
| 111 |
func fallback[T any](val T) resolver[T] { |
| 112 |
return func(s setting[T]) setting[T] { |
| 113 |
if !s.Set { |
| 114 |
s.Value = val |
| 115 |
s.Set = true |
| 116 |
} |
| 117 |
return s |
| 118 |
} |
| 119 |
} |