envoy icon indicating copy to clipboard operation
envoy copied to clipboard

tracing: expose trace info in dynamic metadata

Open gdvalle opened this issue 1 year ago • 3 comments
trafficstars

Commit Message: tracing: expose trace info in dynamic metadata

Additional Description: Expose trace information in dynamic metadata so filters in other languages can create child spans of the same trace, or so the logging system can record per-request trace info. The issue only calls out trace ID, but in practice I found we must also expose the span ID to create proper parent/child span relationship. Also, report whether it was sampled, so we can set trace flags appropriately. Trace and sampled info was readily available, but span ID was not, so plumb that through all the tracers.

I put this under a new envoy.tracing metadata domain as it felt warranted, but I'm not sure whether this is the "right" place. I wanted to avoid touching docs until I can get some feedback on the naming/placement.

This is only implemented for the HTTP flow.

Risk Level: Low Testing: A unit test is modified to verify population of the dynamic metadata. An HTTP Golang filter was used to verify the workings end-to-end using the OpenTelemetry tracer. Docs Changes: The new dynamic metadata fields should be documented (TODO). Release Notes: Platform Specific Features: [Optional Runtime guard:] [Optional Fixes #Issue] https://github.com/envoyproxy/envoy/issues/32894 [Optional Fixes commit #PR or SHA] [Optional Deprecated:] [Optional API Considerations:]

To use this from Go, for example:

sampled := <dynamic metadata fetch/cast>
traceID := <dynamic metadata fetch/cast, convert to bytes>
spanID := <dynamic metadata fetch/cast, convert to bytes>
var traceFlags oteltrace.TraceFlags
if sampled {
	traceFlags = oteltrace.FlagsSampled
}
// Establish the parent span context manually. If this is in DecodeHeaders, it will be the ingress span.
ctx = oteltrace.ContextWithSpanContext(
	ctx,
	oteltrace.NewSpanContext(oteltrace.SpanContextConfig{
		TraceID:    oteltrace.TraceID(traceID),
		SpanID:     oteltrace.SpanID(spanID),
		TraceFlags: traceFlags,
	}),
)
trace.Start(ctx, ...)

image

gdvalle avatar Mar 22 '24 14:03 gdvalle

Hi @gdvalle, welcome and thank you for your contribution.

We will try to review your Pull Request as quickly as possible.

In the meantime, please take a look at the contribution guidelines if you have not done so already.

:cat:

Caused by: https://github.com/envoyproxy/envoy/pull/33065 was opened by gdvalle.

see: more, trace.

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

github-actions[bot] avatar Apr 27 '24 04:04 github-actions[bot]

I haven't had time to rework this yet, but still plan to.

gdvalle avatar May 03 '24 20:05 gdvalle

@wbpcode PTAL.

adisuissa avatar Jun 04 '24 12:06 adisuissa

Thanks for the contribution. There are two main changes here. One adds getSpanId method to the tracer. One change to the contrib go.

Could you split them to two independent PRs? The first one is LGTM but it would be better to do it in an independent PR. Thanks.

wbpcode avatar Jun 05 '24 03:06 wbpcode

Could you split them to two independent PRs? The first one is LGTM but it would be better to do it in an independent PR. Thanks.

Done. I've turned this PR into the span ID API changes. Given the Go changes depend on this, I'll open that after merge. I don't see a way to chain the branches together in a PR against this repo.

gdvalle avatar Jun 05 '24 14:06 gdvalle

LGTM. Thanks.

wbpcode avatar Jun 05 '24 15:06 wbpcode

Funny, 2 hours ago https://github.com/envoyproxy/envoy/pull/33909 was merged, which also implemented getSpanId.

@wbpcode would you be okay with me altering this PR yet again to target the Go filter work, or just open a new one?

gdvalle avatar Jun 05 '24 21:06 gdvalle

Funny, 2 hours ago #33909 was merged, which also implemented getSpanId.

@wbpcode would you be okay with me altering this PR yet again to target the Go filter work, or just open a new one?

It's complete up to you. Both are OK for me. :)

wbpcode avatar Jun 06 '24 01:06 wbpcode

/wait on main merge

alyssawilk avatar Jun 11 '24 12:06 alyssawilk

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

github-actions[bot] avatar Jul 11 '24 16:07 github-actions[bot]

This pull request has been automatically closed because it has not had activity in the last 37 days. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

github-actions[bot] avatar Jul 18 '24 16:07 github-actions[bot]