Skip to content

fix(tracer): support converting OpenTelemetry parent span context - #904

Open
arjun-rajappa wants to merge 2 commits into
mainfrom
fix/support-otel-parent-span-context
Open

arjun-rajappa wants to merge 2 commits into
mainfrom
fix/support-otel-parent-span-context

Conversation

@arjun-rajappa

Copy link
Copy Markdown
Contributor

Allow OpenTelemetry SpanContext instances in start_span by converting valid non-Instana parent contexts into Instana SpanContext objects instead of raising a TypeError. If the context is invalid, fall back to None. Include debug logs for parent context conversion and invalid context resets.

Allow OpenTelemetry SpanContext instances in start_span by converting
valid non-Instana parent contexts into Instana SpanContext objects instead
of raising a TypeError. If the context is invalid, fall back to None.
Include debug logs for parent context conversion and invalid context resets.

Signed-off-by: Arjun Rajappa <arjun.rajappa@ibm.com>
@arjun-rajappa
arjun-rajappa requested a review from a team as a code owner September 17, 2026 05:07
@pvital pvital added bug fix OTel OpenTelemetry compatibility. labels Sep 17, 2026

@pvital pvital left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@arjun-rajappa, can you add specific start_span test cases with context arguments with OTel and Instana SpanContext? You can use the Pytest parametrization for that.

… Context

Signed-off-by: Arjun Rajappa <arjun.rajappa@ibm.com>

@pvital pvital left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks good to me.

@CagriYonca CagriYonca 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.

A few comments

Comment thread src/instana/tracer.py
if parent_context.is_valid:
logger.debug("Converting non-Instana parent context to Instana SpanContext")
parent_context = SpanContext(
trace_id=parent_context.trace_id,

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.

OTel uses 128bit TraceID but Instana uses 64bit.

We can use something like this to convert it:

from instana.util.ids import hex_id, hex_id_limited

hex_tid = hex_id(parent_context.trace_id)  # 128-bit int → hex string

parent_context = SpanContext(
    trace_id=int(hex_id_limited(parent_context.trace_id), 16) if len(hex_tid) > 16 else parent_context.trace_id, # get last 64bits if trace_id == 128bits
    span_id=parent_context.span_id,
    is_remote=parent_context.is_remote,
    trace_flags=parent_context.trace_flags,
    trace_state=parent_context.trace_state,
    trace_parent=None,
    long_trace_id=hex_tid if len(hex_tid) > 16 else None,
)

Comment thread tests/test_tracer.py
from opentelemetry.context.context import Context
from opentelemetry.trace import SpanKind
from opentelemetry.trace.span import _SPAN_ID_MAX_VALUE
from opentelemetry.trace import SpanKind, TraceFlags

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.

TraceFlags is not used.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug fix OTel OpenTelemetry compatibility.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants