Repository navigation
InstanaTracer.start_as_current_span() got an unexpected keyword argument context - #7174
Nayana-R-Gowda wants to merge 11 commits into
Conversation
|
Thanks for addressing the Instana compatibility issue. The original change was well targeted: it identified the inbound request-root span as the failure point, preserved request execution for older Instana tracers, and included a focused regression test. What was good
What I fixedThe broad I pushed commit
All other Tests added
The focused tests and the complete With those guardrails in place, the compatibility approach looks sound. |
msureshkumar88
left a comment
There was a problem hiding this comment.
The fix stays in the correct backend layer. The narrow retry filter preserves unrelated exceptions. No schema or Alembic change is necessary.
blocking | mcpgateway/observability.py:992 | Removing context loses the extracted parent and can split active and forwarded traces | Attach the extracted context during the fallback span lifetime and detach it in finally.
With a valid inbound `traceparent`, `has_remote_parent` remains true. The retry removes `context` without attaching the extracted parent. A tracer that uses the current context can therefore start a separate root. The header-publication guard retains the original inbound header, so active spans and raw-header forwarding can use different traces. I reproduced successful request processing with different active and forwarded trace IDs using the real OpenTelemetry API and a context-rejecting tracer adapter. Actual legacy Instana AutoTrace remains untested. Preserve parenting through the standard context API and verify compatibility with the legacy provider and propagator.
functionally-impacting | tests/unit/mcpgateway/test_observability.py:836 | The success test cannot detect lost parenting, and the PR adds no live-gateway regression | Add a parent-preservation test and the required black-box MCP test, or document a live-test waiver.
The success test returns `nullcontext(None)`, supplies no inbound `traceparent`, and forces an invalid remote parent. Add a valid remote parent and a context-rejecting tracer that creates a span. Assert parent identity, active and forwarded trace consistency, response completion, and context restoration after success and app failure. Exercise authenticated MCP `tools/call` against a running gateway with the incompatible provider. Repository instructions require this black-box test for behavior that a live gateway can exercise.
Validation: The focused module has 55 passing tests and four SDK-dependent skips. All added executable production lines ran. git diff --check passes. Production Ruff checks pass; seven unused test imports already exist in the base version.
Please fix the trace-parent defect before merge. No UI or database changes are required.
|
mcpgateway/observability.py
tests/unit/mcpgateway/test_observability.py (+4 tests, ~190 lines)
|
msureshkumar88
left a comment
There was a problem hiding this comment.
The update preserves the inbound parent during successful fallback spans and restores context after application failures. The narrow TypeError filter remains intact.
blocking | mcpgateway/observability.py:1005 | A failure in the fallback tracer call leaves the inbound context attached because cleanup starts at line 1012 | Move span creation and retry inside the cleanup try/finally; add a retry-failure regression test.
I reproduced this with the real OpenTelemetry context API and W3C propagator. A tracer rejects `context`, then raises `RuntimeError` during retry. The exception propagates, but the outer context remains replaced by the inbound trace. Later spans in that task can inherit the failed request's parent.
functionally-impacting | tests/unit/mcpgateway/test_observability.py:933 | The requested live-gateway regression remains absent, and the PR documents no waiver | Add an authenticated MCP tools/call test against a running gateway with the incompatible provider, or document the requested waiver.
The PR changes only the middleware and its unit tests. The parent-preservation unit tests address the earlier unit-test request. They do not exercise authenticated MCP tools/call against a running gateway.
Validation: The focused unit module has 58 passing tests and six SDK-dependent skips. Production Ruff and fix-commit whitespace checks pass. GitHub CI passes, including pytest with the observability extra. An independent retry-failure reproduction confirms the context leak.
Not checked: actual legacy Instana AutoTrace, a live MCP request, or the full local validation gate.
Please resolve both findings before merge.
|
functionally-impacting — missing live-gateway regression — Fixed in 69d4ec4. |
Review follow-up: compatibility consolidated and live-testedThanks for the additional fixes and validation work. After tracing the failure through every caller, the important finding was that this was not limited to ContextForge's request middleware. FastAPI telemetry, MCP SDK 2.x telemetry, and ContextForge all call the same legacy Instana tracer using the standard OpenTelemetry What changedI pushed
This also removes the fallback lifecycle that caused the context-leak finding when retry span creation failed. Test consolidationThe overlapping fallback and synthetic-tracer tests were reduced to focused adapter coverage. The synthetic live test was replaced with a self-contained test using the real The live regression verifies:
CI now runs this test explicitly with Validation
This now covers the actual process-wide failure while keeping a single compatibility mechanism. |
…ent context Signed-off-by: NAYANA.R <nayana.r7813@gmail.com>
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
Signed-off-by: NAYANA.R <nayana.r7813@gmail.com>
Signed-off-by: NAYANA.R <nayana.r7813@gmail.com>
…ated MCP tools/call regression test or document an explicit waiver Signed-off-by: NAYANA.R <nayana.r7813@gmail.com>
Signed-off-by: NAYANA.R <nayana.r7813@gmail.com>
Signed-off-by: NAYANA.R <nayana.r7813@gmail.com>
Signed-off-by: Madhav Kandukuri <madhav165@gmail.com>
Signed-off-by: NAYANA.R <nayana.r7813@gmail.com>
e9be02c to
9ddbd3a
Compare
The request middleware annotates the ASGI scope as MutableMapping, but the typing import omitted the name. Importing mcpgateway.observability raised NameError and broke pytest collection, pylint, ruff, and gateway startup. Signed-off-by: NAYANA.R <nayana.r7813@gmail.com>
Signed-off-by: NAYANA.R <nayana.r7813@gmail.com>
8939397 to
a175997
Compare
Signed-off-by: NAYANA.R nayana.r7813@gmail.com
Issue: MCP tool invocations (e.g. mcp_903e1051_dw-mk-ask-question) fail with InstanaTracer.start_as_current_span() got an unexpected keyword argument 'context' when ContextForge runs under Instana AutoTrace with instana < 3.12.
Root cause: ContextForge's OpenTelemetryRequestMiddleware.call (mcpgateway/observability.py) passes context= to _TRACER.start_as_current_span(...) whenever an inbound request carries a traceparent header. Older Instana tracers (< 3.12) predate OTel's context kwarg and raise TypeError, aborting the request — even though tracing itself is nonessential.
Fix:
Files changed (fix/instana-tracer-context-kwarg):
Closes #7208