Skip to content

[Bug]: start_as_current_span() missing OpenTelemetry 'context' parameter #847

@1mike1451

Description

@1mike1451

Problem Description

InstanaTracer.start_as_current_span() and start_span() use a non-standard span_context parameter instead of the OpenTelemetry-required context parameter, causing TypeError when called by OpenTelemetry-compliant libraries.

This regression was introduced in commit c4d4251, which changed from context to span_context for architectural simplification. Discovered with FastMCP but affects any OpenTelemetry-compliant library.

Current Implementation (tracer.py:140-151):

def start_as_current_span(
    self,
    name: str,
    span_context: Optional[SpanContext] = None,  # Non-standard parameter
    ...
)

Expected per OpenTelemetry Spec:

def start_as_current_span(
    self,
    name: str,
    context: Optional[Context] = None,  # Required by OpenTelemetry spec
    ...
)

Root Cause:

Instana's SpanContext has 10 custom fields beyond OpenTelemetry's base. The _create_span_context() method needs direct access to these fields.

Proposed Solution:

def start_as_current_span(
    self,
    name: str,
    context: Optional[Context] = None,  # Add for OpenTelemetry compliance
    span_context: Optional[SpanContext] = None,  # Keep for backward compatibility
    kind: SpanKind = SpanKind.INTERNAL,
    ...
) -> Iterator[InstanaSpan]:
    if context is not None and span_context is None:
        span_context = get_current_span(context).get_span_context()
    
    span = self.start_span(name=name, span_context=span_context, ...)

Benefits: Restores OpenTelemetry compliance, preserves Instana's custom fields, follows kind parameter pattern, maintains backward compatibility.

Minimal, Complete, Verifiable, Example

from instana.tracer import InstanaTracer

tracer = InstanaTracer()
with tracer.start_as_current_span("test-span", context=None):
    pass

# Error: TypeError: InstanaTracer.start_as_current_span() got an unexpected keyword argument 'context'

Python Version

3.9

Python Modules

instana (main branch)
opentelemetry-api>=1.27.0
fastmcp>=0.1.0 (where issue was discovered)

Python Environment

No special environment variables required. Issue occurs in any environment where OpenTelemetry-compliant code uses the Instana tracer with the standard `context` parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions