-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from spryker/feature/instrumentation-merge
Moved instrumentations to main module
- Loading branch information
Showing
15 changed files
with
881 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
src/Spryker/Service/Opentelemetry/Instrumentation/ElasticaInstrumentation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/Spryker/Service/Opentelemetry/Instrumentation/PropelInstrumentation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.