Skip to content

Commit 87e14e3

Browse files
authored
Both traceparent&servertiming for CodeIgniter, Yii, Laravel (#230)
1 parent d377253 commit 87e14e3

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/HttpInstrumentation.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ public static function register(CachedInstrumentation $instrumentation): void
7878
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $response->getStatusCode());
7979
$span->setAttribute(TraceAttributes::NETWORK_PROTOCOL_VERSION, $response->getProtocolVersion());
8080
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_BODY_SIZE, $response->headers->get('Content-Length'));
81+
82+
// Propagate server-timing header to response, if ServerTimingPropagator is present
83+
if (class_exists('OpenTelemetry\Contrib\Propagation\ServerTiming\ServerTimingPropagator')) {
84+
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
85+
$prop = new \OpenTelemetry\Contrib\Propagation\ServerTiming\ServerTimingPropagator();
86+
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
87+
$prop->inject($response, ResponsePropagationSetter::instance(), $scope->context());
88+
}
89+
90+
// Propagate traceresponse header to response, if TraceResponsePropagator is present
91+
if (class_exists('OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator')) {
92+
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
93+
$prop = new \OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator();
94+
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
95+
$prop->inject($response, ResponsePropagationSetter::instance(), $scope->context());
96+
}
8197
}
8298

8399
$span->end();

src/ResponsePropagationSetter.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\Contrib\Instrumentation\Laravel;
6+
7+
use function assert;
8+
use Illuminate\Http\Response;
9+
use OpenTelemetry\Context\Propagation\PropagationSetterInterface;
10+
11+
/**
12+
* @internal
13+
*/
14+
class ResponsePropagationSetter implements PropagationSetterInterface
15+
{
16+
public static function instance(): self
17+
{
18+
static $instance;
19+
20+
return $instance ??= new self();
21+
}
22+
23+
public function set(&$carrier, string $key, string $value): void
24+
{
25+
assert($carrier instanceof Response);
26+
27+
$carrier->headers->set($key, $value);
28+
}
29+
}

0 commit comments

Comments
 (0)