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

Moved instrumentations to main module #8

Merged
merged 5 commits into from
Jan 13, 2025
Merged
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
8 changes: 8 additions & 0 deletions _register.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
declare(strict_types=1);

use OpenTelemetry\SDK\Sdk;
use Spryker\Service\Opentelemetry\Instrumentation\ElasticaInstrumentation;
use Spryker\Service\Opentelemetry\Instrumentation\PropelInstrumentation;
use Spryker\Service\Opentelemetry\Instrumentation\RabbitMqInstrumentation;
use Spryker\Service\Opentelemetry\Instrumentation\RedisInstrumentation;
use Spryker\Service\Opentelemetry\Instrumentation\SprykerInstrumentationBootstrap;

if (!class_exists(Sdk::class)) {
Expand All @@ -18,3 +22,7 @@
}

SprykerInstrumentationBootstrap::register();
ElasticaInstrumentation::register();
PropelInstrumentation::register();
RabbitMqInstrumentation::register();
RedisInstrumentation::register();
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

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

namespace Spryker\Service\Opentelemetry\Instrumentation;

use Elastica\Client;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Sdk;
use OpenTelemetry\SemConv\TraceAttributes;
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 Throwable;
use function OpenTelemetry\Instrumentation\hook;

class ElasticaInstrumentation
{
/**
* @var string
*/
protected const NAME = 'spryker_otel_elastica';

/**
* @var string
*/
protected const METHOD_NAME = 'request';

/**
* @var string
*/
protected const SPAN_NAME = 'elasticsearch-request';

/**
* @var string
*/
protected const HEADER_HOST = 'host';

/**
* @var string
*/
protected const ATTRIBUTE_QUERY_TIME = 'queryTime';

/**
* @var string
*/
protected const ATTRIBUTE_SEARCH_INDEX = 'search.index';

/**
* @var string
*/
protected const ATTRIBUTE_SEARCH_QUERY = 'search.query';

/**
* @var string
*/
protected const ATTRIBUTE_ROOT_URL = 'root.url';

/**
* @return void
*/
public static function register(): void
{
if (!class_exists(Client::class) || Sdk::isInstrumentationDisabled(static::NAME) === true) {
return;
}

hook(
class: Client::class,
function: static::METHOD_NAME,
pre: function (Client $client, array $params): void {
if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}

$instrumentation = CachedInstrumentation::getCachedInstrumentation();
$request = new RequestProcessor();
$context = Context::getCurrent();

$span = $instrumentation->tracer()
->spanBuilder(static::SPAN_NAME)
->setSpanKind(SpanKind::KIND_CLIENT)
->setParent($context)
->setAttribute(CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE, true)
->setAttribute(static::ATTRIBUTE_SEARCH_INDEX, $params[0])
->setAttribute(static::ATTRIBUTE_SEARCH_QUERY, serialize($params[2]))
->setAttribute(static::ATTRIBUTE_ROOT_URL, $request->getRequest()->getUri())
->setAttribute(TraceAttributes::URL_DOMAIN, $request->getRequest()->headers->get(static::HEADER_HOST))
->startSpan();

Context::storage()->attach($span->storeInContext($context));
},
post: function (Client $client, array $params, $response, ?Throwable $exception): void {
if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}

$scope = Context::storage()->scope();

if ($scope === null) {
return;
}

$scope->detach();
$span = Span::fromContext($scope->context());
if ($exception !== null) {
$span->recordException($exception);
$span->setStatus(StatusCode::STATUS_ERROR);
} else {
$span->setAttribute(static::ATTRIBUTE_QUERY_TIME, $response->getQueryTime());
$span->setStatus(StatusCode::STATUS_OK);
}

$span->end();
},
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

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

namespace Spryker\Service\Opentelemetry\Instrumentation;

use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\Context;
use OpenTelemetry\SDK\Sdk;
use Propel\Runtime\Connection\StatementInterface;
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 Throwable;
use function OpenTelemetry\Instrumentation\hook;

class PropelInstrumentation
{
/**
* @var string
*/
protected const NAME = 'spryker_otel_propel';

/**
* @var string
*/
protected const SPAN_NAME_PATTERN = 'Propel query: %s...';

/**
* @var string
*/
protected const METHOD_NAME = 'execute';

/**
* @var string
*/
protected const ATTRIBUTE_QUERY = 'query';

/**
* @return void
*/
public static function register(): void
{
if (!class_exists(StatementInterface::class) || Sdk::isInstrumentationDisabled(static::NAME) === true) {
return;
}

hook(
class: StatementInterface::class,
function: static::METHOD_NAME,
pre: function (StatementInterface $statement, array $params): void {
if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}

$context = Context::getCurrent();

$query = $statement->getStatement()->queryString;
$criticalAttr = str_contains($query, 'SELECT') ? CriticalSpanRatioSampler::NO_CRITICAL_ATTRIBUTE : CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE;
$span = CachedInstrumentation::getCachedInstrumentation()
->tracer()
->spanBuilder(sprintf(static::SPAN_NAME_PATTERN, substr($query, 0, 20)))
->setParent($context)
->setSpanKind(SpanKind::KIND_CLIENT)
->setAttribute(static::ATTRIBUTE_QUERY, $query)
->setAttribute($criticalAttr, true)
->startSpan();

Context::storage()->attach($span->storeInContext($context));
},
post: function (StatementInterface $statement, array $params, $response, ?Throwable $exception): void {
if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}

$scope = Context::storage()->scope();

if ($scope === null) {
return;
}

$scope->detach();

$span = Span::fromContext($scope->context());

if ($exception !== null) {
$span->recordException($exception);
$span->setStatus(StatusCode::STATUS_ERROR);
} else {
$span->setStatus(StatusCode::STATUS_OK);
}

$span->end();
}
);
}
}
Loading
Loading