Skip to content

Commit

Permalink
Merge pull request #8 from spryker/feature/instrumentation-merge
Browse files Browse the repository at this point in the history
Moved instrumentations to main module
  • Loading branch information
gechetspr authored Jan 13, 2025
2 parents 1352af1 + 68454b8 commit ccb868f
Show file tree
Hide file tree
Showing 15 changed files with 881 additions and 16 deletions.
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 @@ -17,4 +21,8 @@
return;
}

ElasticaInstrumentation::register();
PropelInstrumentation::register();
RabbitMqInstrumentation::register();
RedisInstrumentation::register();
SprykerInstrumentationBootstrap::register();
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"description": "Opentelemetry module",
"license": "proprietary",
"require": {
"ext-grpc": "*",
"ext-opentelemetry": "*",
"open-telemetry/api": "^1.0",
"open-telemetry/exporter-otlp": "^1.0",
"open-telemetry/gen-otlp-protobuf": "^1.1",
Expand All @@ -14,7 +12,7 @@
"open-telemetry/sem-conv": "^1.0",
"php": ">=8.2",
"spryker/kernel": "^3.30.0",
"spryker/monitoring-extension": "^1.0.0",
"spryker/monitoring-extension": "^1.1.0",
"spryker/symfony": "^3.0.0"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?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
{
//BC check
if (class_exists('\Spryker\Service\OtelElasticaInstrumentation\OpenTelemetry\ElasticaInstrumentation')) {
return;
}

if (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,107 @@
<?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
{
//BC check
if (class_exists('\Spryker\Service\OtelPropelInstrumentation\OpenTelemetry\PropelInstrumentation')) {
return;
}

if (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

0 comments on commit ccb868f

Please sign in to comment.