Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions propagation/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package propagation // import "go.opentelemetry.io/otel/propagation"

import (
"os"
"strings"
)

// EnvCarrier is a TextMapCarrier that uses the environment variables as a
// storage medium for propagated key-value pairs. The keys are uppercased
// before being used to access the environment variables.
// This is useful for propagating values that are set in the environment
// and need to be accessed by different processes or services.
// The keys are uppercased to avoid case sensitivity issues across different
// operating systems and environments.
type EnvCarrier struct{}

var _ TextMapCarrier = EnvCarrier{}

// Get returns the value associated with the passed key.
// The key is uppercased before being used to access the environment variable.
func (EnvCarrier) Get(key string) string {
k := strings.ToUpper(key)
return os.Getenv(k)
}

// Set stores the key-value pair in the environment variable.
// The key is uppercased before being used to set the environment variable.
func (EnvCarrier) Set(key, value string) {
k := strings.ToUpper(key)
_ = os.Setenv(k, value)
Comment thread
pellared marked this conversation as resolved.
Outdated
}

// Keys lists the keys stored in this carrier.
// This method is not implemented for EnvCarrier as it is not possible to
// list all environment variables in a portable way.
func (EnvCarrier) Keys() []string {
// I don't know why TextMapCarrier even has a Keys method.
// It looks like it was some mistake in the original design.
return nil
}
Comment thread
pellared marked this conversation as resolved.
64 changes: 64 additions & 0 deletions propagation/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package propagation_test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)

func TestExtractValidTraceContextFromEnv(t *testing.T) {
stateStr := "key1=value1,key2=value2"
state, err := trace.ParseTraceState(stateStr)
require.NoError(t, err)

tests := []struct {
name string
envs map[string]string
want trace.SpanContext
}{
{
name: "sampled",
envs: map[string]string{
"TRACEPARENT": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
},
want: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceFlags: trace.FlagsSampled,
Remote: true,
}),
},
{
name: "valid tracestate",
envs: map[string]string{
"TRACEPARENT": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
"TRACESTATE": stateStr,
},
want: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
TraceState: state,
Remote: true,
}),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
for k, v := range tc.envs {
t.Setenv(k, v)
}
ctx = prop.Extract(ctx, propagation.EnvCarrier{})
assert.Equal(t, tc.want, trace.SpanContextFromContext(ctx))
})
}
}
Loading