Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 8, 2025
Merged

Conversation

coryb
Copy link
Contributor

@coryb coryb commented Mar 29, 2025

If tracing is enabled, propagate the trace information to the terraform provisioner via environment variables. This sets the TRACEPARENT environment variable using the default W3C trace propagators. Users can choose to continue the trace by adding new spans in the provisioner by reading from the environment like:

ctx := env.ContextWithRemoteSpanContext(context.Background(), os.Environ())

@cdr-bot cdr-bot bot added the community Pull Requests and issues created by the community. label Mar 29, 2025
@coryb coryb changed the title feat(coderd): propagate trace info to provisioner feat(provisioner): propagate trace info Mar 29, 2025
@coryb coryb marked this pull request as ready for review March 29, 2025 21:34
@spikecurtis
Copy link
Contributor

Thanks for the PR!

Can you say a little more about the use case for this change? It appears that terraform itself doesn't yet process TRACEPARENT. Are there provisioners you use that do process it?

@coryb
Copy link
Contributor Author

coryb commented Apr 3, 2025

Can you say a little more about the use case for this change? It appears that terraform itself hashicorp/terraform#35444. Are there provisioners you use that do process it?

Correct, terraform itself won't use it, but it will maintain it in the environment for the provider binaries it calls. We (Netflix) have a custom provider to implement our deployment strategies and would like the ability to continue traces that were started in Coder or further upstream. For example, our provider Create and Update methods will just call:

ctx := env.ContextWithRemoteSpanContext(context.Background(), os.Environ())

Then any new spans created will use the inherited span context.

The primary use case is to enhance our integration testing. In our tests, we start a trace and can see the spans between our test into Coder where it invokes terraform. Currently, in our terraform provider we have to start a new trace, where we collect spans through to workspace startup. With this patch, we will be able to connect our traces from the test entirely through to workspace startup, which should help with visibility/debugging.

@spikecurtis
Copy link
Contributor

That makes sense!

I have to say, from a supply-chain security standpoint, we are very reluctant to add a dependency on github.com/coryb/otelbundle if we can avoid it. If you're OK with it, we'd be willing to accept the propagation/env implementation as a contribution directly to this repo.

The other thing I'd ask for is a unit test of the propagation.

@coryb
Copy link
Contributor Author

coryb commented Apr 4, 2025

I have to say, from a supply-chain security standpoint, we are very reluctant to add a dependency on github.com/coryb/otelbundle if we can avoid it. If you're OK with it, we'd be willing to accept the propagation/env implementation as a contribution directly to this repo.

Thanks, works for me, I will update. You should be able to swap out the implementation with an official OTEL support once they get that sorted.

@coryb coryb force-pushed the trace-provider branch 2 times, most recently from 1b211bd to 4459db5 Compare April 4, 2025 17:21
@coryb
Copy link
Contributor Author

coryb commented Apr 4, 2025

@spikecurtis copied the required code to inject the trace env, and made all the functions unexported. Also added a test to ensure the TRACEPARENT env var is set/updated.

@coryb
Copy link
Contributor Author

coryb commented Apr 4, 2025

updated for lint rules...

If tracing is enabled, propagate the trace information to the terraform
provisioner via environment variables.  This sets the `TRACEPARENT`
environment variable using the default W3C trace propagators.  Users
can choose to continue the trace by adding new spans in the provisioner
by reading from the environment like:

	ctx := env.ContextWithRemoteSpanContext(context.Background(), os.Environ())
Copy link
Contributor

@spikecurtis spikecurtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few nits inline, but I think this is very close.

// don't directly update the slice so we don't modify the slice
// passed in
newEnv := slices.Clone(c.Env)
newEnv = append(newEnv[:i], append([]string{fmt.Sprintf("%s=%s", key, value)}, newEnv[i+1:]...)...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you've cloned the initial slice, seems like it would be simpler to just modify in place, rather than re-slice and append.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah, I think that code went through too many refactors and came out nonsense. I have simplified it.

)

// TODO: replace this with the upstream OTEL env propagation when it is
// released.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -0,0 +1,63 @@
package terraform // nolint:testpackage
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file should be named otelenv_internal_test.go then you don't need this nolint

coryb and others added 3 commits April 7, 2025 07:49
Co-authored-by: Spike Curtis <spike@spikecurtis.com>
Co-authored-by: Spike Curtis <spike@spikecurtis.com>
Simplified the slice copy/update logic.  Removed the panics for non
required interface functions, the impl was trivial, added simple tests
to ensure they work as expected.
@spikecurtis spikecurtis merged commit 12e5718 into coder:main Apr 8, 2025
29 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Apr 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
community Pull Requests and issues created by the community.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants