| 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 | // Carrier is a TextMapCarrier that uses the environment variables as a |
| 15 | // storage medium for propagated key-value pairs. The keys are normalized |
| 16 | // before being used to access the environment variables. |
| 17 | // This is useful for propagating values that are set in the environment |
| 18 | // and need to be accessed by different processes or services. |
| 19 | // The keys are uppercased to avoid case sensitivity issues across different |
| 20 | // operating systems and environments. |
| 21 | // |
| 22 | // If you do not set SetEnvFunc, [Carrier.Set] will do nothing. |
| 23 | // Using [os.Setenv] here is discouraged as the environment should |
| 24 | // be immutable: |
| 25 | // https://opentelemetry.io/docs/specs/otel/context/env-carriers/#environment-variable-immutability |
| 26 | type Carrier struct { |
| 27 | // SetEnvFunc is the function that sets the environment variable. |
| 28 | // Usually, you want to set the environment variables for processes |
| 29 | // that are spawned by the current process. |
| 30 | SetEnvFunc func(key, value string) |
| 31 | values map[string]string |
| 32 | once sync.Once |
| 33 | } |
| 34 | |
| 35 | // Compile time check that Carrier implements the TextMapCarrier. |
| 36 | var _ propagation.TextMapCarrier = (*Carrier)(nil) |
| 37 | |
| 38 | // fetch runs once on first access, and stores the environment in the |
| 39 | // carrier. |
| 40 | func (c *Carrier) fetch() { |
| 41 | c.once.Do(func() { |
| 42 | environ := os.Environ() |
| 43 | c.values = make(map[string]string, len(environ)) |
| 44 | for _, kv := range environ { |
| 45 | kvPair := strings.SplitN(kv, "=", 2) |
| 46 | key := normalize(kvPair[0]) |
| 47 | c.values[key] = kvPair[1] |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | // Get returns the value associated with the normalized passed key. |
| 53 | // The first call to [Carrier.Get] or [Carrier.Keys] for a |
| 54 | // given Carrier will read and store the values from the |
| 55 | // environment and all future reads will be from that store. |
| 56 | func (c *Carrier) Get(key string) string { |
| 57 | c.fetch() |
| 58 | return c.values[normalize(key)] |
| 59 | } |
| 60 | |
| 61 | // Set stores the key-value pair in the environment variable. |
| 62 | // The key is normalized before being used to set the |
| 63 | // environment variable. |
| 64 | // If SetEnvFunc is not set, this method does nothing. |
| 65 | func (c *Carrier) Set(key, value string) { |
| 66 | if c.SetEnvFunc == nil { |
| 67 | return |
| 68 | } |
| 69 | k := normalize(key) |
| 70 | c.SetEnvFunc(k, value) |
| 71 | } |
| 72 | |
| 73 | // Keys lists the keys stored in this carrier. |
| 74 | // This returns all the keys in the environment variables. |
| 75 | // The first call to [Carrier.Get] or [Carrier.Keys] for a |
| 76 | // given Carrier will read and store the values from the |
| 77 | // environment and all future reads will be from that store. |
| 78 | // The keys are returned in their normalized form. |
| 79 | func (c *Carrier) Keys() []string { |
| 80 | c.fetch() |
| 81 | keys := make([]string, 0, len(c.values)) |
| 82 | for key := range c.values { |
| 83 | keys = append(keys, key) |
| 84 | } |
| 85 | return keys |
| 86 | } |