Skip to content
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

Add traceresponse propagation to Slim auto-instrumentation #147

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Instrumentation/Slim/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"open-telemetry/api": "^1.0",
"slim/slim": "^4"
},
"suggest": {
"open-telemetry/opentelemetry-propagation-traceresponse": "Automatically propagate the context to the client."
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3",
"mockery/mockery": "^1.5",
Expand All @@ -27,7 +30,10 @@
"psalm/plugin-phpunit": "^0.16",
"open-telemetry/sdk": "^1.0",
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^4.0"
"vimeo/psalm": "^4.0",
"open-telemetry/opentelemetry-propagation-traceresponse": "*",
"symfony/http-client": "^6.0",
"guzzlehttp/promises": "^1.5"
},
"autoload": {
"psr-4": {
Expand All @@ -41,5 +47,10 @@
"psr-4": {
"OpenTelemetry\\Tests\\Instrumentation\\Slim\\": "tests/"
}
},
"config": {
"allow-plugins": {
"php-http/discovery": true
}
}
}
14 changes: 13 additions & 1 deletion src/Instrumentation/Slim/src/SlimInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\Propagation\ArrayAccessGetterSetter;
use function OpenTelemetry\Instrumentation\hook;
use OpenTelemetry\SemConv\TraceAttributes;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -61,7 +62,7 @@ public static function register(): void

return [$request];
},
post: static function (App $app, array $params, ?ResponseInterface $response, ?Throwable $exception) {
post: static function (App $app, array $params, ?ResponseInterface &$response, ?Throwable $exception) {
cedricziel marked this conversation as resolved.
Show resolved Hide resolved
$scope = Context::storage()->scope();
if (!$scope) {
return;
Expand All @@ -79,6 +80,17 @@ public static function register(): void
$span->setAttribute(TraceAttributes::HTTP_STATUS_CODE, $response->getStatusCode());
$span->setAttribute(TraceAttributes::HTTP_FLAVOR, $response->getProtocolVersion());
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_CONTENT_LENGTH, $response->getHeaderLine('Content-Length'));

// Propagate traceresponse header to response, if TraceResponsePropagator is present
if (class_exists('OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator')) {
$carrier = [];
$prop = new \OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator();
$prop->inject($carrier, ArrayAccessGetterSetter::getInstance(), $scope->context());

foreach ($carrier as $name => $value) {
$response = $response->withHeader($name, $value);
}
}
}

$span->end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Nyholm\Psr7\ServerRequest;
use OpenTelemetry\API\Common\Instrumentation\Configurator;
use OpenTelemetry\Context\ScopeInterface;
use OpenTelemetry\SDK\Trace\ImmutableSpan;
use OpenTelemetry\Contrib\Propagation\TraceResponse\TraceResponsePropagator;
use OpenTelemetry\SDK\Trace\SpanExporter\InMemoryExporter;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\TracerProvider;
Expand Down Expand Up @@ -67,10 +67,13 @@ public function performRouting(ServerRequestInterface $request): ServerRequestIn
$this->createMock(ResponseInterface::class),
$routingMiddleware
);
$app->handle($request->withAttribute(RouteContext::ROUTE, $route));
$response = $app->handle($request->withAttribute(RouteContext::ROUTE, $route));

$this->assertCount(1, $this->storage);
$span = $this->storage->offsetGet(0); // @var ImmutableSpan $span
$this->assertSame($expected, $span->getName());

$this->assertTrue($response->hasHeader(TraceResponsePropagator::TRACERESPONSE), 'traceresponse header is added');
}

/**
Expand Down Expand Up @@ -125,14 +128,19 @@ public function performRouting(ServerRequestInterface $request): ServerRequestIn
$routingMiddleware
);

$response = null;

try {
$app->handle($request);
$response = $app->handle($request);
} catch (\Exception $e) {
$this->assertSame('routing failed', $e->getMessage());
}
$this->assertCount(1, $this->storage);
$span = $this->storage->offsetGet(0); // @var ImmutableSpan $span
$this->assertSame('HTTP GET', $span->getName(), 'span name was not updated because routing failed');

/** @psalm-suppress PossiblyNullReference */
$this->assertTrue($response->hasHeader(TraceResponsePropagator::TRACERESPONSE), 'traceresponse header is added');
}

public function createMockStrategy(): InvocationStrategyInterface
Expand Down