opentelemetry-php icon indicating copy to clipboard operation
opentelemetry-php copied to clipboard

Problem context

Open hwz9612 opened this issue 3 years ago • 2 comments

Describe your environment Describe any aspect of your environment relevant to the problem, including your php version (php -v will tell you your current version), version numbers of installed dependencies, information about your cloud hosting provider, etc. If you're reporting a problem with a specific version of a library in this repo, please check whether the problem has been fixed on master. php7.4 open-telemetry/api (0.0.17) open-telemetry/context Steps to reproduce Describe exactly how to reproduce the error. Include a code sample if applicable.

$span = $tracer->spanBuilder($this->request->url())->startSpan();
$spanScope = $span->activate();

$key = Context::createKey('mimictag');
$contextWithRequestId = Context::getCurrent();
$scope = $contextWithRequestId->activate();
$context = $contextWithRequestId->with($key, '123456');
$anotherSpan = $tracer->spanBuilder("another-spqn")->startSpan();
$contextWithRequestId1 = Context::getCurrent();
$mimictagFromContext = $contextWithRequestId1->get($key);
$anotherSpan->setAttribute("request.id", $mimictagFromContext);

I want to trace request id in different span. But I can not get mimictagFromContext by get($key). Why? What is the expected behavior? What did you expect to see? I want to get mimictagFromContext. What is the actual behavior? What did you see instead? I can not get mimictagFromContext by get($key). Additional context Add any other context about the problem here.

hwz9612 avatar May 20 '23 10:05 hwz9612

Because you are getting the active context, you need to set the key before you activate:

$contextWithRequestId = Context::getCurrent()->with($key, '123456');
$scope = $contextWithRequestId->activate();

brettmc avatar Jun 05 '23 06:06 brettmc

@hwz9612 closing this, feel free to re-open if you are still having issues.

brettmc avatar Jun 14 '23 11:06 brettmc