Skip to content

fix(otel): honor parent span sampling decisions#47

Open
bramwelt wants to merge 1 commit into
mainfrom
fix/LFXV2-1734-parent-based-sampling
Open

fix(otel): honor parent span sampling decisions#47
bramwelt wants to merge 1 commit into
mainfrom
fix/LFXV2-1734-parent-based-sampling

Conversation

@bramwelt
Copy link
Copy Markdown
Contributor

Summary

Fixes LFXV2-1734 — all Go service spans have parentid: 0 in Datadog because TraceIDRatioBased makes independent sampling decisions, ignoring incoming traceparent headers.

Changes

pkg/utils/otel.go — Replace bare trace.TraceIDRatioBased(ratio) with a new newSampler(cfg) function that:

  • Reads OTEL_TRACES_SAMPLER / OTEL_TRACES_SAMPLER_ARG (standard env vars)
  • Defaults to parentbased_traceidratio when unset — fixes trace continuity immediately with no config change required
  • Supports all 6 OTEL sampler types
  • Falls back to cfg.TracesSampleRatio when OTEL_TRACES_SAMPLER_ARG is unset, preserving backward compatibility

pkg/utils/otel_test.go — Added TestNewSampler and TestNewSampler_InvalidArg

charts/lfx-v2-auth-service/templates/deployment.yaml — Renders OTEL_TRACES_SAMPLER and OTEL_TRACES_SAMPLER_ARG env vars when app.otel.tracesSampler is set

charts/lfx-v2-auth-service/values.yaml — Added tracesSampler: "" to app.otel

Why

trace.TraceIDRatioBased re-decides sampling per span regardless of the parent's sampling flag. Wrapping with parentbased_traceidratio ensures child spans always follow the parent's decision, restoring end-to-end trace continuity.

Issue: LFXV2-1734

Add newSampler() that reads OTEL_TRACES_SAMPLER and
OTEL_TRACES_SAMPLER_ARG. When unset, defaults to
parentbased_traceidratio so child spans always honor
the parent sampling flag, fixing broken trace continuity.

Add tracesSampler field to chart values.yaml and render
OTEL_TRACES_SAMPLER / OTEL_TRACES_SAMPLER_ARG env vars
in the deployment template.

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

Issue: LFXV2-1734
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 May 13, 2026 16:09
@bramwelt bramwelt requested a review from a team as a code owner May 13, 2026 16:09
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 13, 2026

Warning

Rate limit exceeded

@bramwelt has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 11 minutes and 23 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7d0fab6b-f122-4c9d-a887-a3f6fe46960c

📥 Commits

Reviewing files that changed from the base of the PR and between 001ff13 and 3e114bb.

📒 Files selected for processing (4)
  • charts/lfx-v2-auth-service/templates/deployment.yaml
  • charts/lfx-v2-auth-service/values.yaml
  • pkg/utils/otel.go
  • pkg/utils/otel_test.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/LFXV2-1734-parent-based-sampling

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 updates the service’s OpenTelemetry tracing setup to honor upstream/parent span sampling decisions (fixing broken trace continuity where spans appear with parentid: 0 in Datadog) by defaulting to parent-based sampling, while still supporting explicit sampler configuration via standard OTEL env vars.

Changes:

  • Added newSampler(cfg) to build a trace.Sampler from OTEL_TRACES_SAMPLER / OTEL_TRACES_SAMPLER_ARG, defaulting to parentbased_traceidratio with the configured ratio.
  • Updated trace provider construction to use newSampler(cfg) instead of a bare TraceIDRatioBased.
  • Extended Helm chart values/template to optionally render OTEL_TRACES_SAMPLER (+ arg), and added unit tests for the new sampler selection logic.

Reviewed changes

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

File Description
pkg/utils/otel.go Introduces env-driven sampler selection and switches tracer provider sampling to parent-based by default.
pkg/utils/otel_test.go Adds tests covering newSampler supported values and invalid-arg handling.
charts/lfx-v2-auth-service/templates/deployment.yaml Optionally injects OTEL sampler env vars into the Deployment when configured via values.
charts/lfx-v2-auth-service/values.yaml Adds app.otel.tracesSampler chart value and documents supported sampler strings.
Comments suppressed due to low confidence (1)

pkg/utils/otel_test.go:566

  • TestNewSampler_InvalidArg only checks for non-nil, so it doesn’t actually verify the fallback behavior described in the comment (using cfg.TracesSampleRatio). Consider asserting the sampler’s effective ratio by exercising ShouldSample with a root span (no parent) and checking the decision matches what cfg.TracesSampleRatio would produce.
// TestNewSampler_InvalidArg verifies that an invalid OTEL_TRACES_SAMPLER_ARG
// falls back to cfg.TracesSampleRatio without panicking.
func TestNewSampler_InvalidArg(t *testing.T) {
	cfg := OTelConfig{TracesSampleRatio: 0.5}
	t.Setenv("OTEL_TRACES_SAMPLER", "parentbased_traceidratio")
	t.Setenv("OTEL_TRACES_SAMPLER_ARG", "invalid")

	s := newSampler(cfg)
	if s == nil {
		t.Error("newSampler returned nil for invalid OTEL_TRACES_SAMPLER_ARG")
	}

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

Comment thread pkg/utils/otel.go
Comment on lines +306 to +340
// newSampler creates a trace.Sampler from OTEL_TRACES_SAMPLER and
// OTEL_TRACES_SAMPLER_ARG environment variables, falling back to
// parentbased_traceidratio with cfg.TracesSampleRatio when unset.
// This ensures parent span sampling decisions are always honored.
func newSampler(cfg OTelConfig) trace.Sampler {
sampler := os.Getenv("OTEL_TRACES_SAMPLER")
arg := os.Getenv("OTEL_TRACES_SAMPLER_ARG")

parseRatio := func() float64 {
if arg != "" {
r, err := strconv.ParseFloat(arg, 64)
if err == nil && r >= 0.0 && r <= 1.0 {
return r
}
slog.Warn("invalid OTEL_TRACES_SAMPLER_ARG, using TracesSampleRatio", "value", arg)
}
return cfg.TracesSampleRatio
}

switch sampler {
case "always_on":
return trace.AlwaysSample()
case "always_off":
return trace.NeverSample()
case "traceidratio":
return trace.TraceIDRatioBased(parseRatio())
case "parentbased_always_on":
return trace.ParentBased(trace.AlwaysSample())
case "parentbased_always_off":
return trace.ParentBased(trace.NeverSample())
case "parentbased_traceidratio":
return trace.ParentBased(trace.TraceIDRatioBased(parseRatio()))
default: // empty/unknown → parent-based with configured ratio
return trace.ParentBased(trace.TraceIDRatioBased(cfg.TracesSampleRatio))
}
Comment thread pkg/utils/otel.go
Comment on lines +317 to +320
if err == nil && r >= 0.0 && r <= 1.0 {
return r
}
slog.Warn("invalid OTEL_TRACES_SAMPLER_ARG, using TracesSampleRatio", "value", arg)
Comment thread pkg/utils/otel_test.go
Comment on lines +543 to +551
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("OTEL_TRACES_SAMPLER", tt.sampler)
t.Setenv("OTEL_TRACES_SAMPLER_ARG", tt.arg)

s := newSampler(cfg)
if s == nil {
t.Errorf("newSampler(%q) returned nil", tt.sampler)
}
Comment on lines +88 to +90
- name: OTEL_TRACES_SAMPLER
value: {{ $otelTracesSampler | quote }}
{{- if ne $otelTracesSampleRatio "" }}
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