Skip to content

creating a new context with an existing trace context is not working as expected #1572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
HeenaBansal20 opened this issue Apr 25, 2025 · 5 comments

Comments

@HeenaBansal20
Copy link

HeenaBansal20 commented Apr 25, 2025

I am looking for the way to create a new context with an existing context but with updated TraceFlags.

I am writting my own extract() function and trying to understand how I can create new context with the current context.

For example if my propagator FIELDS has 3 headers x-span , x-trace and x-sampled.
If only the x-sampled header is present, the function should extract the context and keep the existing traceid and spanid (if available) but update the sampled flag.

I am trying this way but I am getting currentContext as null:

public function extract($carrier, ?PropagationGetterInterface $getter = null, ?ContextInterface $context = null): ContextInterface
    {
        $getter ??= ArrayAccessGetterSetter::getInstance();
        $context ??= Context::getCurrent();
        
        $spanContext = self::extractImpl($carrier, $getter, $context);
        if (!$spanContext->isValid()) {
            return $context;
        }

        return $context->withContextValue(Span::wrap($spanContext));
    }
  private static function extractImpl($carrier, PropagationGetterInterface $getter, ContextInterface $context): SpanContextInterface
    {
        $traceId = self::readHeader($carrier, $getter, self::INSTANA_TRACE_ID_HEADER);
        $spanId = self::readHeader($carrier, $getter, self::INSTANA_SPAN_ID_HEADER);
        $sampled = self::getSampledValue($carrier, $getter);
$currentspanContext = Span::fromContext($context)->getContext();
if ((!$traceId &&  !$spanId) && $sampled != null) {
            echo "I am in this condition";
           
            $updatedSpanContext = SpanContext::createFromRemoteParent(
                $currentSpanContext->getTraceId(),
                $currentSpanContext->getSpanId(),
                $isSampled ? TraceFlags::SAMPLED : TraceFlags::DEFAULT
            );
            return updatedSpanContext;

Tetscase:


public function test_extract_context_with_no_trace_and_span_headers(): void
     {
         $carrier = [
             'X-INSTANA-L' => '0',
         ];

         $this->assertEquals(
             SpanContext::createFromRemoteParent(SpanContextValidator::ff000000000000000000000000000041,
             SpanContextValidator::ff00000000000041),
             $this->getSpanContext($this->instanaMultiPropagator->extract($carrier))
         );
     }

@HeenaBansal20 HeenaBansal20 changed the title [Extensions propagators] : How I can create a new context with an existing trace context How I can create a new context with an existing trace context Apr 25, 2025
@HeenaBansal20 HeenaBansal20 changed the title How I can create a new context with an existing trace context creating a new context with an existing trace context is not working as expected Apr 29, 2025
@brettmc
Copy link
Collaborator

brettmc commented May 5, 2025

I am trying this way but I am getting currentContext as null

Do you mean this this line null? $context ??= Context::getCurrent();

If I have understood correctly, you want to use an existing context propagation mechanism (for example the otel TraceParent one), and then selectively apply Instana headers on top of that?

@HeenaBansal20
Copy link
Author

@brettmc , this line returns null.
$currentspanContext = Span::fromContext($context)->getContext();

I want to keep the current traceid and spanid from the span context and update the trace flags only as part of instana headers.

For example
if my carrier has only this
$carrier = [
'X-INSTANA-L' => '1',
];

I should be able to create a new remote parent from root traceid and spanid but trace flags to be 1. New span should be
$updatedSpanContext = SpanContext::createFromRemoteParent(
$currentSpanContext->getTraceId(),
$currentSpanContext->getSpanId(),
$isSampled ? TraceFlags::SAMPLED : TraceFlags::DEFAULT
);

@brettmc
Copy link
Collaborator

brettmc commented May 6, 2025

I think what you're hoping for is that the TraceContextPropagator has run before your propagator, then your propagator may make some changes.
Have you tried using MultiTextMapPropagator? You'd have to be careful that they are registered in the correct order, but I think it can do what you want. First TraceContextPropagator will extract context, and that context will be passed in to subsequent propagators, ie yours.

@HeenaBansal20
Copy link
Author

@brettmc , I reconsidered the requirement with respect to only one INSTANA header in carrier.
I need not to go to that route.
please take a look at my PR here .
#1582

@brettmc
Copy link
Collaborator

brettmc commented May 8, 2025

I think this can be closed now, since you've taken a different path in the PR.

@brettmc brettmc closed this as completed May 8, 2025
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

No branches or pull requests

2 participants