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

Feature/otel optimisation #2

Merged
merged 9 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
fail-fast: false
matrix:
php-version: [
'8.1',
'8.2'
'8.2',
'8.3'
]

steps:
Expand Down
10 changes: 10 additions & 0 deletions _register.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

declare(strict_types=1);

use OpenTelemetry\SDK\Sdk;
use Spryker\Service\OtelRabbitMqInstrumentation\OpenTelemetry\RabbitMqInstrumentation;

if (Sdk::isDisabled()) {
return;
}

if (extension_loaded('opentelemetry') === false) {
error_log('The opentelemetry extension must be loaded in order to autoload the OpenTelemetry Spryker Framework auto-instrumentation', E_USER_WARNING);

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"require": {
"open-telemetry/api": "^1.0",
"open-telemetry/context": "^1.0",
"open-telemetry/sdk": "^1.0",
"open-telemetry/sem-conv": "^1.0",
"php": ">=8.1",
"php": ">=8.2",
"spryker/rabbit-mq": "^2.0.0",
"spryker/opentelemetry": "^0.1.0"
"spryker/opentelemetry": "^1.0.0"
},
"autoload": {
"psr-4": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@

use Generated\Shared\Transfer\EventQueueSendMessageBodyTransfer;
use Generated\Shared\Transfer\QueueSendMessageTransfer;
use OpenTelemetry\API\Trace\Span;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SemConv\TraceAttributes;
use Spryker\Client\RabbitMq\Model\RabbitMqAdapter;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\CriticalSpanRatioSampler;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\TraceSampleResult;
use Spryker\Service\Opentelemetry\Instrumentation\Span\Span;
use Spryker\Shared\Opentelemetry\Instrumentation\CachedInstrumentation;
use Spryker\Shared\Opentelemetry\Request\RequestProcessor;
use Spryker\Zed\Opentelemetry\Business\Generator\SpanFilter\SamplerSpanFilter;
use Throwable;
use function OpenTelemetry\Instrumentation\hook;

Expand Down Expand Up @@ -88,10 +89,15 @@ protected static function registerHook(string $functionName, string $spanName):
class: RabbitMqAdapter::class,
function: $functionName,
pre: function (RabbitMqAdapter $rabbitMqAdapter, array $params) use ($instrumentation, $spanName, $request): void {

if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
$context = Context::getCurrent();
$span = $instrumentation->tracer()
->spanBuilder($spanName)
->setSpanKind(SpanKind::KIND_CLIENT)
->setParent($context)
->setAttribute(CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE, true)
->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, $request->getMethod())
->setAttribute(static::ATTRIBUTE_QUEUE_NAME, $params[0]);

Expand All @@ -105,12 +111,15 @@ function: $functionName,
}
}

$span
->setAttribute(TraceAttributes::URL_DOMAIN, $request->headers->get(static::HEADER_HOST))
->startSpan()
->activate();
$span = $span->setAttribute(TraceAttributes::URL_DOMAIN, $request->headers->get(static::HEADER_HOST))
->startSpan();

Context::storage()->attach($span->storeInContext($context));
},
post: function (RabbitMqAdapter $rabbitMqAdapter, array $params, $response, ?Throwable $exception): void {
if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
$span = Span::fromContext(Context::getCurrent());

if ($exception !== null) {
Expand Down
Loading