- Notifications
You must be signed in to change notification settings - Fork905
feat(provisioner): propagate trace info#17166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletionsprovisioner/terraform/otelenv.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package terraform | ||
import ( | ||
"context" | ||
"fmt" | ||
"slices" | ||
"strings" | ||
"unicode" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/propagation" | ||
) | ||
// TODO: replace this with the upstream OTEL env propagation when it is | ||
// released. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. 👍 | ||
// envCarrier is a propagation.TextMapCarrier that is used to extract or | ||
// inject tracing environment variables. This is used with a | ||
// propagation.TextMapPropagator | ||
type envCarrier struct { | ||
Env []string | ||
} | ||
var _ propagation.TextMapCarrier = (*envCarrier)(nil) | ||
func toKey(key string) string { | ||
key = strings.ToUpper(key) | ||
key = strings.ReplaceAll(key, "-", "_") | ||
return strings.Map(func(r rune) rune { | ||
if unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_' { | ||
return r | ||
} | ||
return -1 | ||
}, key) | ||
} | ||
func (c *envCarrier) Set(key, value string) { | ||
if c == nil { | ||
return | ||
} | ||
key = toKey(key) | ||
for i, e := range c.Env { | ||
if strings.HasPrefix(e, key+"=") { | ||
// don't directly update the slice so we don't modify the slice | ||
// passed in | ||
c.Env = slices.Clone(c.Env) | ||
c.Env[i] = fmt.Sprintf("%s=%s", key, value) | ||
return | ||
} | ||
} | ||
c.Env = append(c.Env, fmt.Sprintf("%s=%s", key, value)) | ||
} | ||
func (c *envCarrier) Get(key string) string { | ||
if c == nil { | ||
return "" | ||
} | ||
key = toKey(key) | ||
for _, e := range c.Env { | ||
if strings.HasPrefix(e, key+"=") { | ||
return strings.TrimPrefix(e, key+"=") | ||
} | ||
} | ||
return "" | ||
} | ||
func (c *envCarrier) Keys() []string { | ||
if c == nil { | ||
return nil | ||
} | ||
keys := make([]string, len(c.Env)) | ||
for i, e := range c.Env { | ||
k, _, _ := strings.Cut(e, "=") | ||
keys[i] = k | ||
} | ||
return keys | ||
} | ||
// otelEnvInject will add add any necessary environment variables for the span | ||
// found in the Context. If environment variables are already present | ||
// in `environ` then they will be updated. If no variables are found the | ||
// new ones will be appended. The new environment will be returned, `environ` | ||
// will never be modified. | ||
func otelEnvInject(ctx context.Context, environ []string) []string { | ||
c := &envCarrier{Env: environ} | ||
otel.GetTextMapPropagator().Inject(ctx, c) | ||
spikecurtis marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return c.Env | ||
} |
85 changes: 85 additions & 0 deletionsprovisioner/terraform/otelenv_internal_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package terraform | ||
import ( | ||
"context" | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/propagation" | ||
sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
type testIDGenerator struct{} | ||
var _ sdktrace.IDGenerator = (*testIDGenerator)(nil) | ||
func (testIDGenerator) NewIDs(_ context.Context) (trace.TraceID, trace.SpanID) { | ||
traceID, _ := trace.TraceIDFromHex("60d19e9e9abf2197c1d6d8f93e28ee2a") | ||
spanID, _ := trace.SpanIDFromHex("a028bd951229a46f") | ||
return traceID, spanID | ||
} | ||
func (testIDGenerator) NewSpanID(_ context.Context, _ trace.TraceID) trace.SpanID { | ||
spanID, _ := trace.SpanIDFromHex("a028bd951229a46f") | ||
return spanID | ||
} | ||
func TestOtelEnvInject(t *testing.T) { | ||
t.Parallel() | ||
testTraceProvider := sdktrace.NewTracerProvider( | ||
sdktrace.WithSampler(sdktrace.AlwaysSample()), | ||
sdktrace.WithIDGenerator(testIDGenerator{}), | ||
) | ||
tracer := testTraceProvider.Tracer("example") | ||
ctx, span := tracer.Start(context.Background(), "testing") | ||
defer span.End() | ||
input := []string{"PATH=/usr/bin:/bin"} | ||
otel.SetTextMapPropagator(propagation.TraceContext{}) | ||
got := otelEnvInject(ctx, input) | ||
require.Equal(t, []string{ | ||
"PATH=/usr/bin:/bin", | ||
"TRACEPARENT=00-60d19e9e9abf2197c1d6d8f93e28ee2a-a028bd951229a46f-01", | ||
}, got) | ||
// verify we update rather than append | ||
input = []string{ | ||
"PATH=/usr/bin:/bin", | ||
"TRACEPARENT=origTraceParent", | ||
"TERM=xterm", | ||
} | ||
otel.SetTextMapPropagator(propagation.TraceContext{}) | ||
got = otelEnvInject(ctx, input) | ||
require.Equal(t, []string{ | ||
"PATH=/usr/bin:/bin", | ||
"TRACEPARENT=00-60d19e9e9abf2197c1d6d8f93e28ee2a-a028bd951229a46f-01", | ||
"TERM=xterm", | ||
}, got) | ||
} | ||
func TestEnvCarrierSet(t *testing.T) { | ||
t.Parallel() | ||
c := &envCarrier{ | ||
Env: []string{"PATH=/usr/bin:/bin", "TERM=xterm"}, | ||
} | ||
c.Set("PATH", "/usr/local/bin") | ||
c.Set("NEWVAR", "newval") | ||
require.Equal(t, []string{ | ||
"PATH=/usr/local/bin", | ||
"TERM=xterm", | ||
"NEWVAR=newval", | ||
}, c.Env) | ||
} | ||
func TestEnvCarrierKeys(t *testing.T) { | ||
t.Parallel() | ||
c := &envCarrier{ | ||
Env: []string{"PATH=/usr/bin:/bin", "TERM=xterm"}, | ||
} | ||
require.Equal(t, []string{"PATH", "TERM"}, c.Keys()) | ||
} |
2 changes: 2 additions & 0 deletionsprovisioner/terraform/provision.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.