|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package envcar // import "go.opentelemetry.io/contrib/propagators/envcar" |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + |
| 11 | + "go.opentelemetry.io/otel/propagation" |
| 12 | +) |
| 13 | + |
| 14 | +// upperWithUnderscores converts a string so that A-Z and 0-9 and _ are kept |
| 15 | +// as-is, a-z is uppercased, and all other characters are replaced with _. |
| 16 | +func upperWithUnderscores(s string) string { |
| 17 | + b := make([]byte, 0, len(s)) |
| 18 | + for _, r := range s { |
| 19 | + switch { |
| 20 | + case r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '_': |
| 21 | + b = append(b, byte(r)) |
| 22 | + case r >= 'a' && r <= 'z': |
| 23 | + b = append(b, byte(r+'A'-'a')) |
| 24 | + default: |
| 25 | + b = append(b, '_') |
| 26 | + } |
| 27 | + } |
| 28 | + return string(b) |
| 29 | +} |
| 30 | + |
| 31 | +// Carrier is a TextMapCarrier that uses the environment variables as a |
| 32 | +// storage medium for propagated key-value pairs. The keys are normalised |
| 33 | +// before being used to access the environment variables. |
| 34 | +// This is useful for propagating values that are set in the environment |
| 35 | +// and need to be accessed by different processes or services. |
| 36 | +// The keys are uppercased to avoid case sensitivity issues across different |
| 37 | +// operating systems and environments. |
| 38 | +// |
| 39 | +// If you do not set SetEnvFunc, [Carrier.Set] will do nothing. |
| 40 | +// Using [os.Setenv] here is discouraged as the environment should |
| 41 | +// be immutable: |
| 42 | +// https://opentelemetry.io/docs/specs/otel/context/env-carriers/#environment-variable-immutability |
| 43 | +type Carrier struct { |
| 44 | + // SetEnvFunc is the function that sets the environment variable. |
| 45 | + // Usually, you want to set the environment variables for processes |
| 46 | + // that are spawned by the current process. |
| 47 | + SetEnvFunc func(key, value string) |
| 48 | + values map[string]string |
| 49 | + once sync.Once |
| 50 | +} |
| 51 | + |
| 52 | +// Compile time check that Carrier implements the TextMapCarrier. |
| 53 | +var _ propagation.TextMapCarrier = (*Carrier)(nil) |
| 54 | + |
| 55 | +// fetch runs once on first access, and stores the environment in the |
| 56 | +// carrier. |
| 57 | +func (c *Carrier) fetch() { |
| 58 | + c.once.Do(func() { |
| 59 | + environ := os.Environ() |
| 60 | + c.values = make(map[string]string, len(environ)) |
| 61 | + for _, kv := range environ { |
| 62 | + kvPair := strings.SplitN(kv, "=", 2) |
| 63 | + c.values[kvPair[0]] = kvPair[1] |
| 64 | + } |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +// Get returns the value associated with the normalized passed key. |
| 69 | +// The first call to [Carrier.Get] or [Carrier.Keys] for a |
| 70 | +// given Carrier will read and store the values from the |
| 71 | +// environment and all future reads will be from that store. |
| 72 | +func (c *Carrier) Get(key string) string { |
| 73 | + c.fetch() |
| 74 | + return c.values[upperWithUnderscores(key)] |
| 75 | +} |
| 76 | + |
| 77 | +// Set stores the key-value pair in the environment variable. |
| 78 | +// The key is normalized before being used to set the |
| 79 | +// environment variable. |
| 80 | +// If SetEnvFunc is not set, this method does nothing. |
| 81 | +func (c *Carrier) Set(key, value string) { |
| 82 | + if c.SetEnvFunc == nil { |
| 83 | + return |
| 84 | + } |
| 85 | + k := upperWithUnderscores(key) |
| 86 | + c.SetEnvFunc(k, value) |
| 87 | +} |
| 88 | + |
| 89 | +// Keys lists the keys stored in this carrier. |
| 90 | +// This returns all the keys in the environment variables. |
| 91 | +// The first call to [Carrier.Get] or [Carrier.Keys] for a |
| 92 | +// given Carrier will read and store the values from the |
| 93 | +// environment and all future reads will be from that store. |
| 94 | +// Keys are returned as is, without any normalization, but |
| 95 | +// this behavior is subject to change. |
| 96 | +func (c *Carrier) Keys() []string { |
| 97 | + c.fetch() |
| 98 | + keys := make([]string, 0, len(c.values)) |
| 99 | + for key := range c.values { |
| 100 | + keys = append(keys, key) |
| 101 | + } |
| 102 | + return keys |
| 103 | +} |
0 commit comments