Skip to content

Apply the ignore rules to every request that starts a trace - #265

Merged
mrproliu merged 1 commit into
mainfrom
fix-ignore-rules-reused-goroutine
Nov 8, 2026
Merged

mrproliu merged 1 commit into
mainfrom
fix-ignore-rules-reused-goroutine

Conversation

@wu-sheng

@wu-sheng wu-sheng commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Summary

Found while checking the so11y expected data changed in #264. The /ignored.html handler should be ignored, because .html is in the default ignore_suffix, but the scenario expected it to be traced.

Problems on main:

  1. The ignore rules worked only for a goroutine's first request. A goroutine keeps its tracing context after a trace ends: End only resets the active span. createNoop treated any existing context as being inside a trace, so it skipped ignore_suffix, trace_ignore_path, and the init/disconnected-reporter check. On a goroutine that serves several requests, such as an HTTP/1 keep-alive connection, every request after the first was traced even when it should be ignored, and was traced while the reporter was disconnected. Fix wrong tracing context switch when trace ignore plugin activated #237 (in v0.7.0) introduced this when it moved the checks below that return, so that they don't apply inside a trace. In so11y, /ignored.html reuses the connection that served /propagated.
  2. The spans under an ignored request each started their own trace. The ignore and availability paths returned a noop span without storing it, so the DB and HTTP calls of an ignored request started single-span traces, and their exit spans propagated sw8 downstream. The sampler's noop path stores its span, so a sampled-out request doesn't have this problem.
  3. Continuations of one snapshot shared a noop span. ContinueContext gave every continuation the same noop object, so goroutines continuing one captured context, such as the send and receive goroutines of a gRPC stream, shared an unsynchronized nesting count.
  4. gin and goframe panicked on a negative header_length_threshold after creating the entry span and before SetContext, so the span was never ended. With the fix for (2), the stuck noop span of an ignored request would have suppressed every later request on that goroutine.

Changes:

  • createNoop returns early only when the context has an active span. Otherwise it runs the checks, reusing the goroutine's context. A noop span from the checks is stored as the active span, as the sampler path does. Spans inside a trace still skip the checks, which keeps the Fix wrong tracing context switch when trace ignore plugin activated #237 behavior.
  • ContinueContext gives each continuation of a noop snapshot its own noop span.
  • gin, goframe: SetContext runs right after the span is created, so AfterInvoke always ends the span. A header_length_threshold of 0 or less now means no limit, as for redis.max_args_bytes; collect_request_headers already decides whether headers are collected. The doc rows are updated.
  • so11y expected counters: propagated 1, sampler 4, created-ignored 1, finished-tracing 5, finished-ignored 1. They no longer depend on HTTP connection reuse.

Not changed here, because they already exist on main and need their own design:

  • A goroutine that continues an ignored or sampled-out request ignores only its first span. Keeping the copied noop span active would break MQ consumers and gRPC workers started inside an untraced request: they would drop every later message, even ones with a valid sw8 header.
  • When a nested plugin ends a reused entry span, the active span is reset before the outer owner ends.

Tests

  • New in plugins/core/tracer_ignore_test.go: TestIgnoreRulesApplyAfterFinishedTrace, TestDisconnectedReporterAppliesAfterFinishedTrace, TestSpansUnderIgnoredEntryAreIgnored, and TestContinuedNoopSnapshotsAreIndependent fail on main. TestIgnoreRulesDoNotApplyInsideTrace (the Fix wrong tracing context switch when trace ignore plugin activated #237 case) passes on both.
  • New TestCollectHeadersWithoutLimit in gin and goframe (thresholds 0 and -1) fails on main.
  • make test, the race tests, go vet, and golangci-lint v2.15.0 pass. The so11y scenario passed locally in all three cases, and the collector received exactly the new counters.

@wu-sheng
wu-sheng requested a review from mrproliu November 7, 2026 14:38
@wu-sheng

wu-sheng commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

@mrproliu, could you check whether this mechanism change fits the design of the tracing context? It changes the code that #237 touched.

  1. createNoop now treats "no active span" as the start of a new trace, even when the goroutine still holds the context of a finished trace. The ignore rules and the init/disconnected-reporter check run in that case, reusing the leftover context. With an active span, the new span is still a child (or a noop under a noop) and the checks don't apply, as Fix wrong tracing context switch when trace ignore plugin activated #237 intended.
  2. The noop span from those checks is now stored as the active span, as the sampler's noop span already is, so the spans under an ignored request are noop too. NoopSpan.End clears it when the request ends.
  3. ContinueContext gives each continuation of a noop snapshot its own noop span instead of sharing the captured one.

A design question: the root cause is that a finished trace leaves its context in the goroutine. The Java agent removes both the tracing context and the runtime context when a trace finishes (ContextManager#stopSpan calls CONTEXT.remove() and RUNTIME_CONTEXT.remove()). This PR keeps the Go behavior, including the runtime context values, and only changes how createNoop reads a leftover context. Would you rather clear the goroutine's context when its trace finishes? That would also stop runtime context values from carrying over to the next request on a reused goroutine, but it would drop values set outside a trace.

@wu-sheng wu-sheng added this to the 0.8.0 milestone Nov 7, 2026
A goroutine keeps its tracing context after a trace ends, and createNoop
treated any existing context as being inside a trace, so it skipped the
ignore rules (agent.ignore_suffix, agent.trace_ignore_path) and the
disconnected-reporter check. On a goroutine that serves several requests,
such as an HTTP/1 keep-alive connection, only the first request was
checked. The so11y scenario expected /ignored.html to be traced because
of this.

- Return early in createNoop only when the context has an active span,
  and run the checks otherwise. Store the noop span of an ignored request
  as the active span, as for a request the sampler drops, so the spans
  under it are ignored instead of each starting its own trace.
- Give each ContinueContext of a noop snapshot its own noop span, so
  goroutines continuing one captured context don't share its nesting
  count.
- gin, goframe: pass the span to the after-hook before collecting the
  request headers, and treat a header_length_threshold of 0 or less as
  no limit. A negative value panicked after the span was created, so the
  span was never ended.
- Restore the so11y expected counters to what the scenario intends.
@wu-sheng
wu-sheng force-pushed the fix-ignore-rules-reused-goroutine branch from cbc7d32 to f332792 Compare November 7, 2026 15:16

@mrproliu mrproliu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Treating "no active span" as the start of a trace keeps the #237 intent and fixes the reused-goroutine case.

One possible problem: the noop span of the ignore and disconnected-reporter paths is now stored as the active span. If a plugin creates a root span and never ends it (for example, BeforeInvoke fails before SetContext), every later request on that goroutine, such as an HTTP/1 keep-alive connection, is silently ignored. This PR fixes gin and goframe, but other plugins could still hit it.

I'll submit a follow-up PR that handles this in the interceptor templates, so plugins don't need to get it right themselves.

@mrproliu
mrproliu merged commit bdef1b2 into main Nov 8, 2026
49 checks passed
@mrproliu
mrproliu deleted the fix-ignore-rules-reused-goroutine branch November 8, 2026 05:22
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