Skip to content

Conversation

@jipperinbham
Copy link

This enables distributed tracing support by passing span identifiers from the caller to CNI plugins through environment variables. When a valid span context exists, it is propagated using the TRACEPARENT, TRACESTATE, and BAGGAGE environment variables following the OpenTelemetry specification OTEP 0258.

This allows CNI plugins to participate in distributed traces, helping with performance analysis and debugging across the plugin execution lifecycle. I does not create any spans and instead leaves that responsibility to the caller and CNI plugins. Below is an example for extracting the trace context via environment variables:

func extractTraceparent(ctx context.Context) context.Context {
	traceParent, ok := os.LookupEnv("TRACEPARENT")
	if !ok {
		return ctx
	}

	traceParts := strings.Split(traceParent, "-")
	if len(traceParts) != 4 {
		return ctx
	}

	traceID, err := trace.TraceIDFromHex(traceParts[1])
	if err != nil {
		return ctx
	}

	spanID, err := trace.SpanIDFromHex(traceParts[2])
	if err != nil {
		return ctx
	}

	flagBytes, err := hex.DecodeString(traceParts[3])
	if err != nil {
		return ctx
	}

	var ts trace.TraceState
	if traceState, ok := os.LookupEnv("TRACESTATE"); ok {
		if ts, err = trace.ParseTraceState(traceState); err != nil {
			// no-op
		}
	}

	// traceBaggage, err := baggage.Parse(os.Getenv("BAGGAGE"))
	// if err != nil {
	// 	slog.With("error", err.Error()).Error(fmt.Sprintf("Failed to parse baggage %s", err.Error()))
	// }

	scc := trace.SpanContextConfig{
		TraceID:    traceID,
		SpanID:     spanID,
		TraceFlags: trace.TraceFlags(flagBytes[0]),
		TraceState: ts,
		Remote:     true,
	}

	return trace.ContextWithSpanContext(ctx, trace.NewSpanContext(scc))
}

Fixes #561

This enables distributed tracing support by passing span identifiers
from the caller to CNI plugins through environment variables. When a
valid span context exists, it is propagated using the TRACEPARENT,
TRACESTATE, and BAGGAGE environment variables following the
OpenTelemetry specification (OTEP 0258).

This allows CNI plugins to participate in distributed traces, helping
with performance analysis and debugging across the plugin execution
lifecycle.

Fixes containernetworking#561

Signed-off-by: JP Phillips <[email protected]>
@jipperinbham jipperinbham changed the title pkg/invoke: add OpenTelemetry trace context propagation libcni: add OpenTelemetry trace context propagation Nov 10, 2025
jipperinbham added a commit to jipperinbham/go-cni that referenced this pull request Nov 10, 2025
Creates a span for each interface function where context.Context is
available and additional creates child spans for the critical attach
functions for each network being created.

Goes along with containernetworking/cni#1173
to provide end-to-end traces for setting up CNI.
jipperinbham added a commit to jipperinbham/go-cni that referenced this pull request Nov 10, 2025
Creates a span for each interface function where context.Context is
available and additional creates child spans for the critical attach
functions for each network being created.

Goes along with containernetworking/cni#1173
to provide end-to-end traces for setting up CNI.
jipperinbham added a commit to jipperinbham/go-cni that referenced this pull request Nov 10, 2025
Creates a span for each interface function where context.Context is
available and additional creates child spans for the critical attach
functions for each network being created.

Goes along with containernetworking/cni#1173
to provide end-to-end traces for setting up CNI.

Signed-off-by: JP Phillips <[email protected]>
module github.com/containernetworking/cni

go 1.21
go 1.23.0
Copy link
Author

Choose a reason for hiding this comment

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

I'm guessing this is causing the CI failure. @squeed would you prefer I rollback to 1.21?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libcni should support distributed tracing

2 participants