-
Notifications
You must be signed in to change notification settings - Fork 92
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
store root span info directly in context instead of request attribute #84
Closed
pdelewski
wants to merge
3
commits into
open-telemetry:main
from
pdelewski:store-root-span-info-in-context
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
use OpenTelemetry\API\Common\Instrumentation\CachedInstrumentation; | ||
use OpenTelemetry\API\Common\Instrumentation\Globals; | ||
use OpenTelemetry\API\Trace\Span; | ||
use OpenTelemetry\API\Trace\SpanInterface; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\API\Trace\StatusCode; | ||
use OpenTelemetry\Context\Context; | ||
|
@@ -24,10 +23,11 @@ | |
*/ | ||
class Psr15Instrumentation | ||
{ | ||
public static $rootSpan; | ||
|
||
public static function register(): void | ||
{ | ||
$instrumentation = new CachedInstrumentation('io.opentelemetry.contrib.php.psr15'); | ||
|
||
/** | ||
* Create a span for each psr-15 middleware that is executed. | ||
*/ | ||
|
@@ -68,9 +68,7 @@ public static function register(): void | |
'handle', | ||
pre: static function (RequestHandlerInterface $handler, array $params, string $class, string $function, ?string $filename, ?int $lineno) use ($instrumentation) { | ||
$request = ($params[0] instanceof ServerRequestInterface) ? $params[0] : null; | ||
$root = $request | ||
? $request->getAttribute(SpanInterface::class) | ||
: Span::getCurrent(); | ||
$root = Context::getCurrent()->get(Psr15Instrumentation::$rootSpan); | ||
$builder = $instrumentation->tracer()->spanBuilder( | ||
$root | ||
? sprintf('%s::%s', $class, $function) | ||
|
@@ -92,7 +90,7 @@ public static function register(): void | |
->setAttribute(TraceAttributes::HTTP_REQUEST_CONTENT_LENGTH, $request->getHeaderLine('Content-Length')) | ||
->setAttribute(TraceAttributes::HTTP_SCHEME, $request->getUri()->getScheme()) | ||
->startSpan(); | ||
$request = $request->withAttribute(SpanInterface::class, $span); | ||
$parent = $parent->with(Psr15Instrumentation::$rootSpan, $span); | ||
} else { | ||
$span = $builder->startSpan(); | ||
} | ||
|
@@ -125,3 +123,5 @@ public static function register(): void | |
); | ||
} | ||
} | ||
|
||
Psr15Instrumentation::$rootSpan ??= Context::createKey('rootSpan'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that this won't work with preloading, should be moved into |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO should also be private and only accessible via getter to prevent modification - or use an
enum
for this context key.