Skip to content

Commit

Permalink
Both traceparent&servertiming for CodeIgniter, Yii, Laravel (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
agoallikmaa authored Jan 29, 2024
1 parent d377253 commit 87e14e3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/HttpInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ public static function register(CachedInstrumentation $instrumentation): void
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $response->getStatusCode());
$span->setAttribute(TraceAttributes::NETWORK_PROTOCOL_VERSION, $response->getProtocolVersion());
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_BODY_SIZE, $response->headers->get('Content-Length'));

// Propagate server-timing header to response, if ServerTimingPropagator is present
if (class_exists('OpenTelemetry\Contrib\Propagation\ServerTiming\ServerTimingPropagator')) {
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
$prop = new \OpenTelemetry\Contrib\Propagation\ServerTiming\ServerTimingPropagator();
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
$prop->inject($response, ResponsePropagationSetter::instance(), $scope->context());
}

// Propagate traceresponse header to response, if TraceResponsePropagator is present
if (class_exists('OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator')) {
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
$prop = new \OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator();
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
$prop->inject($response, ResponsePropagationSetter::instance(), $scope->context());
}
}

$span->end();
Expand Down
29 changes: 29 additions & 0 deletions src/ResponsePropagationSetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Contrib\Instrumentation\Laravel;

use function assert;
use Illuminate\Http\Response;
use OpenTelemetry\Context\Propagation\PropagationSetterInterface;

/**
* @internal
*/
class ResponsePropagationSetter implements PropagationSetterInterface
{
public static function instance(): self
{
static $instance;

return $instance ??= new self();
}

public function set(&$carrier, string $key, string $value): void
{
assert($carrier instanceof Response);

$carrier->headers->set($key, $value);
}
}

0 comments on commit 87e14e3

Please sign in to comment.