Skip to content

Commit c8ffef9

Browse files
authored
Allow custom traces enrichment (#23)
* feat: add custom append methods for override * chore: change get root span method visibility * feat: allow append custom span
1 parent 5d014bc commit c8ffef9

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

src/Aspect/HttpClientAspect.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,31 +104,45 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
104104
$options['headers'] = array_replace($options['headers'] ?? [], $appendHeaders);
105105
$proceedingJoinPoint->arguments['keys']['options'] = $options;
106106

107+
$this->appendCustomSpan($span, $options);
108+
107109
/** @var PromiseInterface $result */
108110
$result = $proceedingJoinPoint->process();
109111
$result->then(
110-
$this->onFullFilled($span),
111-
$this->onRejected($span)
112+
$this->onFullFilled($span, $options),
113+
$this->onRejected($span, $options)
112114
);
113115
$span->finish();
114116

115117
return $result;
116118
}
117119

118-
private function onFullFilled(Span $span): callable
120+
protected function appendCustomSpan(Span $span, array $options): void
119121
{
120-
return function (ResponseInterface $response) use ($span) {
122+
// just for override
123+
}
124+
125+
protected function appendCustomResponseSpan(Span $span, array $options, ?ResponseInterface $response): void
126+
{
127+
// just for override
128+
}
129+
130+
private function onFullFilled(Span $span, array $options): callable
131+
{
132+
return function (ResponseInterface $response) use ($span, $options) {
121133
$span->setTag(
122134
$this->spanTagManager->get('http_client', 'http.status_code'),
123135
$response->getStatusCode()
124136
);
125137
$span->setTag('otel.status_code', 'OK');
138+
139+
$this->appendCustomResponseSpan($span, $options, $response);
126140
};
127141
}
128142

129-
private function onRejected(Span $span): callable
143+
private function onRejected(Span $span, array $options): callable
130144
{
131-
return function (RequestException $exception) use ($span) {
145+
return function (RequestException $exception) use ($span, $options) {
132146
if ($this->switchManager->isEnabled('exception')) {
133147
$this->appendExceptionToSpan($span, $exception);
134148
}
@@ -138,6 +152,8 @@ private function onRejected(Span $span): callable
138152
$exception->getResponse()->getStatusCode()
139153
);
140154

155+
$this->appendCustomResponseSpan($span, $options, $exception->getResponse());
156+
141157
return Create::rejectionFor($exception);
142158
};
143159
}

src/Middleware/TraceMiddleware.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
9292
$response = $handler->handle($request);
9393
$span->setTag($this->spanTagManager->get('response', 'status_code'), $response->getStatusCode());
9494
$span->setTag('otel.status_code', 'OK');
95-
$this->appendCustomResponseSpan($span, $response);
95+
$this->appendCustomResponseSpan($span, $request, $response);
9696
} catch (Throwable $exception) {
9797
$this->switchManager->isEnabled('exception') && $this->appendExceptionToSpan($span, $exception);
9898
if ($exception instanceof HttpException) {
@@ -112,7 +112,12 @@ protected function appendCustomExceptionSpan(Span $span, Throwable $exception):
112112
// just for override
113113
}
114114

115-
protected function appendCustomResponseSpan(Span $span, ResponseInterface $response): void
115+
protected function appendCustomSpan(Span $span, ServerRequestInterface $request): void
116+
{
117+
// just for override
118+
}
119+
120+
protected function appendCustomResponseSpan(Span $span, ServerRequestInterface $request, ResponseInterface $response): void
116121
{
117122
// just for override
118123
}
@@ -146,6 +151,8 @@ protected function buildSpan(ServerRequestInterface $request): Span
146151
$span->setTag("attribute.{$key}", $value);
147152
}
148153

154+
$this->appendCustomSpan($span, $request);
155+
149156
return $span;
150157
}
151158

src/SpanStarter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected function startSpan(
7070
return $child;
7171
}
7272

73-
private function getTracerRoot(int $coroutineId): ?Span
73+
public function getTracerRoot(int $coroutineId): ?Span
7474
{
7575
/** @var null|Span $root */
7676
$root = Context::get('tracer.root', null, $coroutineId);

0 commit comments

Comments
 (0)