-
Notifications
You must be signed in to change notification settings - Fork 60
chore: decouple client/server tracing from PSR implementation and consolidate nextSpanResolver. #166
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
chore: decouple client/server tracing from PSR implementation and consolidate nextSpanResolver. #166
Changes from all commits
9986886
dd5de99
02ccf75
18b1e71
eb76e08
4fc98ca
eb5bee4
bd75590
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,10 @@ | |
| namespace Zipkin\Instrumentation\Http\Client; | ||
|
|
||
| use Zipkin\Tracing; | ||
| use Zipkin\Tracer; | ||
| use Zipkin\Span; | ||
| use Zipkin\Propagation\SamplingFlags; | ||
| use Zipkin\Instrumentation\Http\Client\Parser; | ||
|
|
||
| /** | ||
| * ClientTracing includes all the elements needed to instrument a | ||
|
|
@@ -23,46 +27,81 @@ class ClientTracing | |
| private $parser; | ||
|
|
||
| /** | ||
| * function that decides to sample or not an unsampled | ||
| * request. The signature is: | ||
| * | ||
| * <pre> | ||
| * function (RequestInterface $request): ?bool {} | ||
| * </pre> | ||
| * | ||
| * @var callable|null | ||
| * @var callable | ||
| */ | ||
| private $requestSampler; | ||
| private $nextSpanResolver; | ||
|
|
||
| /** | ||
| * @param Tracing $tracing | ||
| * @param Parser $parser HTTP client parser to obtain meaningful information from | ||
| * request and response and tag the span accordingly. | ||
| * @param callable(mixed):?bool $requestSampler function that decides to sample or not an unsampled | ||
| * request. | ||
| * @param callable(mixed):?SamplingFlags $traceContextObtainer function that obtains the context from the | ||
| * request. It is mostly used in event loop scenaries where the global scope can't be used. | ||
| */ | ||
| public function __construct( | ||
| Tracing $tracing, | ||
| Parser $parser = null, | ||
| callable $requestSampler = null | ||
| callable $requestSampler = null, | ||
| callable $traceContextObtainer = null | ||
| ) { | ||
| $this->tracing = $tracing; | ||
| $this->parser = $parser ?? new DefaultParser; | ||
| $this->requestSampler = $requestSampler; | ||
| $this->parser = $parser ?? new NoopParser; | ||
| $this->nextSpanResolver = self::buildNextSpanResolver( | ||
| $tracing->getTracer(), | ||
| $requestSampler, | ||
| $traceContextObtainer | ||
| ); | ||
| } | ||
|
|
||
| public function getTracing(): Tracing | ||
| { | ||
| return $this->tracing; | ||
| } | ||
|
|
||
| public function getParser(): Parser | ||
| { | ||
| return $this->parser; | ||
| } | ||
|
|
||
| /** | ||
| * @return callable with the signature: | ||
| * | ||
| * <pre> | ||
| * function (RequestInterface $request): ?bool | ||
| * </pre> | ||
| * @return callable(mixed):Span the next span handler which creates an appropriate span based on the current scope, | ||
| * and the incoming request. | ||
| */ | ||
| public function getRequestSampler(): ?callable | ||
| public function getNextSpanResolver(): callable | ||
| { | ||
| return $this->requestSampler; | ||
| return $this->nextSpanResolver; | ||
| } | ||
|
|
||
| public function getParser(): Parser | ||
| { | ||
| return $this->parser; | ||
| private static function buildNextSpanResolver( | ||
|
Contributor
Author
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. This is important change. |
||
| Tracer $tracer, | ||
| ?callable $requestSampler, | ||
| ?callable $traceContextObtainer | ||
| ): callable { | ||
| return static function ($request) use ($tracer, $requestSampler, $traceContextObtainer): Span { | ||
| if ($traceContextObtainer !== null) { | ||
| // in this case, the trace context is meant to be obtained from the request | ||
| $traceContext = ($traceContextObtainer)($request); | ||
|
|
||
| if ($requestSampler !== null) { | ||
| return $tracer->nextSpanWithSampler( | ||
| $requestSampler, | ||
| [$request], | ||
| $traceContext | ||
| ); | ||
| } | ||
| return $tracer->nextSpan($traceContext); | ||
| } | ||
|
|
||
| if ($requestSampler !== null) { | ||
| return $tracer->nextSpanWithSampler( | ||
| $requestSampler, | ||
| [$request] | ||
| ); | ||
| } | ||
|
|
||
| return $tracer->nextSpan(); | ||
| }; | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Zipkin\Instrumentation\Http\Client; | ||
|
|
||
| use Zipkin\SpanCustomizer; | ||
| use Zipkin\Propagation\TraceContext; | ||
|
|
||
| /** | ||
| * NoopParser is a noop implementation of a parser that parses nothing as it does not know anything about the request. | ||
|
Contributor
Author
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. not sure I should provide this but at least it is convenient for tests.
Member
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. if not sure keep it in tests directory :P |
||
| * It is responsibility of the library instrumentation to provide a default parser as this should never be used in | ||
| * production. | ||
| */ | ||
| final class NoopParser implements Parser | ||
| { | ||
| public function spanName($request): string | ||
| { | ||
| return ''; | ||
|
Member
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. is this because null isn't a thing? |
||
| } | ||
|
|
||
| public function request($request, TraceContext $context, SpanCustomizer $span): void | ||
| { | ||
| } | ||
|
|
||
| public function response($response, TraceContext $context, SpanCustomizer $span): void | ||
| { | ||
| } | ||
| } | ||
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.
Ignore this changes.