Skip to content

feat(nats): add OTel span instrumentation#16

Open
bramwelt wants to merge 4 commits into
mainfrom
feat/LFXV2-1743-nats-tracing
Open

feat(nats): add OTel span instrumentation#16
bramwelt wants to merge 4 commits into
mainfrom
feat/LFXV2-1743-nats-tracing

Conversation

@bramwelt

Copy link
Copy Markdown
Contributor

Summary

  • Adds natsHeaderCarrier type implementing propagation.TextMapCarrier for NATS header-based trace context propagation
  • Wraps Request() with a SpanKindClient span that injects trace context into outbound NATS message headers
  • Adds SpanKindConsumer span to SubscribeWithTransportMessenger() that extracts trace context from inbound message headers
  • Wraps CDP and Query HTTP clients with otelhttp.NewTransport for outbound HTTP tracing

Issue: LFXV2-1743

🤖 Generated with Claude Code

Add OTel client spans to Request(), consumer spans to
SubscribeWithTransportMessenger(), and otelhttp transport
to CDP and Query Service HTTP clients.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Issue: LFXV2-1743
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Trevor Bramwell <tbramwell@linuxfoundation.org>
Copilot AI review requested due to automatic review settings June 23, 2026 23:41
@bramwelt bramwelt requested a review from a team as a code owner June 23, 2026 23:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces OpenTelemetry (OTel) tracing for outbound/inbound NATS messaging (context propagation + spans) and adds outbound HTTP client tracing for the CDP and Query service clients, improving end-to-end observability across inter-service calls.

Changes:

  • Added an OTel TextMapCarrier adapter for nats.Header and unit tests for it.
  • Instrumented NATS request/reply (SpanKindClient) and subscription message processing (SpanKindConsumer) with context injection/extraction.
  • Added otelhttp transport wrapping for default CDP and Query HTTP clients.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
internal/infrastructure/query/client.go Adds otelhttp.NewTransport to instrument outbound Query Service HTTP requests.
internal/infrastructure/cdp/client.go Adds otelhttp.NewTransport to instrument outbound CDP HTTP requests.
internal/infrastructure/nats/client.go Wraps NATS request/subscribe flows with spans and injects/extracts trace context via headers.
internal/infrastructure/nats/tracing.go Introduces natsHeaderCarrier implementing propagation.TextMapCarrier.
internal/infrastructure/nats/tracing_test.go Adds tests validating natsHeaderCarrier behavior and interface conformance.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/infrastructure/nats/tracing.go
Comment thread internal/infrastructure/query/client.go
Comment thread internal/infrastructure/cdp/client.go
Replace manual OTLP exporter and propagator setup
with autoexport and autoprop. Configuration is now
driven entirely by OTEL_* environment variables.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Issue: LFXV2-1743
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Trevor Bramwell <tbramwell@linuxfoundation.org>
Comment thread go.mod
Comment thread go.mod
Comment thread go.mod
Comment thread go.mod
Comment thread go.mod
Comment thread go.mod
Comment thread go.mod
Comment thread go.mod
- internal/infrastructure/nats/tracing.go: add nil guard to
  natsHeaderCarrier.Set() to prevent panic on nil map assignment
  (per copilot[bot])
- internal/infrastructure/cdp/client.go: wrap non-nil HTTPClient
  transport with otelhttp when a custom client is provided
  (per copilot[bot])
- internal/infrastructure/query/client.go: wrap non-nil HTTPClient
  transport with otelhttp when a custom client is provided
  (per copilot[bot])

Resolves 3 review threads.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Issue: LFXV2-1743
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Trevor Bramwell <tbramwell@linuxfoundation.org>
Copilot AI review requested due to automatic review settings June 24, 2026 16:32
@bramwelt

Copy link
Copy Markdown
Contributor Author

Review Feedback Addressed

Commit: 314bd61

Changes Made

  • internal/infrastructure/query/client.go: When a non-nil HTTPClient is provided, NewClient now clones it and wraps its transport with otelhttp.NewTransport, with a double-wrap guard. (per copilot-pull-request-reviewer)
  • internal/infrastructure/cdp/client.go: Same fix — wrap non-nil provided HTTPClient transport with otelhttp. (per copilot-pull-request-reviewer)
  • internal/infrastructure/nats/tracing.go: Added nil guard to natsHeaderCarrier.Set().

No Change Needed

  • go.mod (×8): BSD-3-Clause + Google patent license — standard transitive deps from OpenTelemetry SDK, does not affect LFX MIT-licensed code (flagged by github-license-compliance)

Threads Resolved

10 of 10 unresolved threads addressed.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 9 changed files in this pull request and generated 2 comments.

Comment thread pkg/utils/otel.go
Comment thread pkg/utils/otel.go
- pkg/utils/otel.go: restore OTEL_TRACES_SAMPLE_RATIO support —
  parse the env var and wire it into TracerProvider as
  ParentBased(TraceIDRatioBased(ratio)); the Helm chart injects
  this var so dropping it silently broke sampling (per copilot[bot])

Resolves 2 review threads.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Issue: LFXV2-1743
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Trevor Bramwell <tbramwell@linuxfoundation.org>
@bramwelt

Copy link
Copy Markdown
Contributor Author

Review Feedback Addressed (Round 2)

Commit: 62256af

Changes Made

  • pkg/utils/otel.go: restored OTEL_TRACES_SAMPLE_RATIO support — parses the env var and wires it into the TracerProvider as ParentBased(TraceIDRatioBased(ratio)). The Helm chart injects this env var, so dropping it silently broke sampling. Includes range/parse validation with slog.Warn fallback. (per copilot-pull-request-reviewer)

Threads Resolved

2 of 2 unresolved threads addressed.

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.

2 participants