Skip to content

Commit

Permalink
Added custom events processing
Browse files Browse the repository at this point in the history
  • Loading branch information
gechetspr committed Jan 9, 2025
1 parent 80106db commit cb16211
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 119 deletions.
2 changes: 1 addition & 1 deletion _register.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
return;
}

SprykerInstrumentationBootstrap::register();
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
Expand Up @@ -68,7 +68,12 @@ class ElasticaInstrumentation
*/
public static function register(): void
{
if (!class_exists(Client::class) || Sdk::isInstrumentationDisabled(static::NAME) === true) {
//BC check
if (class_exists('\Spryker\Service\OtelElasticaInstrumentation\OpenTelemetry\ElasticaInstrumentation')) {
return;
}

if (Sdk::isInstrumentationDisabled(static::NAME) === true) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ class PropelInstrumentation
*/
public static function register(): void
{
if (!class_exists(StatementInterface::class) || Sdk::isInstrumentationDisabled(static::NAME) === true) {
//BC check
if (class_exists('\Spryker\Service\OtelPropelInstrumentation\OpenTelemetry\PropelInstrumentation')) {
return;
}

if (Sdk::isInstrumentationDisabled(static::NAME) === true) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ public static function register(): void
*/
protected static function registerHook(string $functionName, string $spanName): void
{
if (!class_exists(RabbitMqAdapter::class) || Sdk::isInstrumentationDisabled(static::NAME) === true) {
//BC check
if (class_exists('\Spryker\Service\OtelRabbitMqInstrumentation\OpenTelemetry\RabbitMqInstrumentation')) {
return;
}

if (Sdk::isInstrumentationDisabled(static::NAME) === true) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use OpenTelemetry\SDK\Sdk;
use OpenTelemetry\SemConv\TraceAttributes;
use Redis;
use Spryker\Client\Redis\Adapter\RedisAdapterInterface;
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;
Expand Down Expand Up @@ -41,51 +43,16 @@ class RedisInstrumentation
*/
public static function register(): void
{
if (!class_exists(Redis::class) || Sdk::isInstrumentationDisabled(static::NAME) === true) {
if (Sdk::isInstrumentationDisabled(static::NAME) === true) {
return;
}

$instrumentation = CachedInstrumentation::getCachedInstrumentation();

hook(
Redis::class,
'select',
pre: static function (Redis $redis, array $params) use ($instrumentation): void {

if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
$context = Context::getCurrent();
$span = $instrumentation->tracer()
->spanBuilder('Redis::select')
->setSpanKind(SpanKind::KIND_CLIENT)
->setParent($context)
->setAttribute(TraceAttributes::DB_NAMESPACE, $params[0])
->startSpan();

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

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

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

hook(
Redis::class,
RedisAdapterInterface::class,
'get',
pre: static function (Redis $redis, array $params) use ($instrumentation): void {
pre: static function (RedisAdapterInterface $redis, array $params) use ($instrumentation): void {

if (TraceSampleResult::shouldSkipTraceBody()) {
return;
Expand All @@ -95,13 +62,13 @@ public static function register(): void
->spanBuilder('Redis::get')
->setSpanKind(SpanKind::KIND_CLIENT)
->setParent($context)
->setAttribute(CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE, true)
->setAttribute(TraceAttributes::DB_QUERY_TEXT, isset($params[0]) ? 'GET ' . $params[0] : 'undefined')
->setAttribute(TraceAttributes::DB_NAMESPACE, $redis->getDBNum())
->startSpan();

Context::storage()->attach($span->storeInContext($context));
},
post: static function (Redis $redis, array $params, mixed $ret, ?Throwable $exception): void {
post: static function (RedisAdapterInterface $redis, array $params, mixed $ret, ?Throwable $exception): void {
if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
Expand All @@ -119,25 +86,25 @@ public static function register(): void
);

hook(
Redis::class,
'eval',
pre: static function (Redis $redis, array $params) use ($instrumentation): void {
RedisAdapterInterface::class,
'mget',
pre: static function (RedisAdapterInterface $redis, array $params) use ($instrumentation): void {

if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
$context = Context::getCurrent();
$span = $instrumentation->tracer()
->spanBuilder('Redis::eval')
->spanBuilder('Redis::mget')
->setSpanKind(SpanKind::KIND_CLIENT)
->setParent($context)
->setAttribute(static::PARAM_EVAL_SCRIPT, $params[0] ?? 'undefined')
->setAttribute(TraceAttributes::DB_NAMESPACE, $redis->getDBNum())
->setAttribute(CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE, true)
->setAttribute(TraceAttributes::DB_QUERY_TEXT, implode(' ', $params[0]))
->startSpan();

Context::storage()->attach($span->storeInContext($context));
},
post: static function (Redis $redis, array $params, mixed $ret, ?Throwable $exception): void {
post: static function (RedisAdapterInterface $redis, array $params, mixed $ret, ?Throwable $exception): void {
if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
Expand All @@ -155,25 +122,25 @@ public static function register(): void
);

hook(
Redis::class,
'exists',
pre: static function (Redis $redis, array $params) use ($instrumentation): void {
RedisAdapterInterface::class,
'eval',
pre: static function (RedisAdapterInterface $redis, array $params) use ($instrumentation): void {

if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
$context = Context::getCurrent();
$span = $instrumentation->tracer()
->spanBuilder('Redis::exists')
->spanBuilder('Redis::eval')
->setSpanKind(SpanKind::KIND_CLIENT)
->setParent($context)
->setAttribute(TraceAttributes::DB_QUERY_TEXT, implode(' ', $params))
->setAttribute(TraceAttributes::DB_NAMESPACE, $redis->getDBNum())
->setAttribute(CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE, true)
->setAttribute(static::PARAM_EVAL_SCRIPT, $params[0] ?? 'undefined')
->startSpan();

Context::storage()->attach($span->storeInContext($context));
},
post: static function (Redis $redis, array $params, mixed $ret, ?Throwable $exception): void {
post: static function (RedisAdapterInterface $redis, array $params, mixed $ret, ?Throwable $exception): void {
if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
Expand All @@ -191,9 +158,9 @@ public static function register(): void
);

hook(
Redis::class,
RedisAdapterInterface::class,
'mset',
pre: static function (Redis $redis, array $params) use ($instrumentation): void {
pre: static function (RedisAdapterInterface $redis, array $params) use ($instrumentation): void {

if (TraceSampleResult::shouldSkipTraceBody()) {
return;
Expand All @@ -203,13 +170,13 @@ public static function register(): void
->spanBuilder('Redis::mset')
->setSpanKind(SpanKind::KIND_CLIENT)
->setParent($context)
->setAttribute(CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE, true)
->setAttribute(TraceAttributes::DB_QUERY_TEXT, implode(' ', array_keys($params[0])))
->setAttribute(TraceAttributes::DB_NAMESPACE, $redis->getDBNum())
->startSpan();

Context::storage()->attach($span->storeInContext($context));
},
post: static function (Redis $redis, array $params, mixed $ret, ?Throwable $exception): void {
post: static function (RedisAdapterInterface $redis, array $params, mixed $ret, ?Throwable $exception): void {
if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
Expand All @@ -227,45 +194,9 @@ public static function register(): void
);

hook(
Redis::class,
'msetnx',
pre: static function (Redis $redis, array $params) use ($instrumentation): void {

if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
$context = Context::getCurrent();
$span = $instrumentation->tracer()
->spanBuilder('Redis::msetnx')
->setSpanKind(SpanKind::KIND_CLIENT)
->setParent($context)
->setAttribute(TraceAttributes::DB_QUERY_TEXT, implode(' ', array_keys($params[0])))
->setAttribute(TraceAttributes::DB_NAMESPACE, $redis->getDBNum())
->startSpan();

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

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

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

hook(
Redis::class,
RedisAdapterInterface::class,
'set',
pre: static function (Redis $redis, array $params) use ($instrumentation): void {
pre: static function (RedisAdapterInterface $redis, array $params) use ($instrumentation): void {

if (TraceSampleResult::shouldSkipTraceBody()) {
return;
Expand All @@ -275,13 +206,13 @@ public static function register(): void
->spanBuilder('Redis::set')
->setSpanKind(SpanKind::KIND_CLIENT)
->setParent($context)
->setAttribute(CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE, true)
->setAttribute(TraceAttributes::DB_QUERY_TEXT, $params[0] ?? 'undefined')
->setAttribute(TraceAttributes::DB_NAMESPACE, $redis->getDBNum())
->startSpan();

Context::storage()->attach($span->storeInContext($context));
},
post: static function (Redis $redis, array $params, mixed $ret, ?Throwable $exception): void {
post: static function (RedisAdapterInterface $redis, array $params, mixed $ret, ?Throwable $exception): void {
if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
Expand All @@ -301,7 +232,7 @@ public static function register(): void
hook(
Redis::class,
'setex',
pre: static function (Redis $redis, array $params) use ($instrumentation): void {
pre: static function (RedisAdapterInterface $redis, array $params) use ($instrumentation): void {

if (TraceSampleResult::shouldSkipTraceBody()) {
return;
Expand All @@ -311,14 +242,15 @@ public static function register(): void
->spanBuilder('Redis::set')
->setSpanKind(SpanKind::KIND_CLIENT)
->setParent($context)
->setAttribute(CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE, true)
->setAttribute(TraceAttributes::DB_QUERY_TEXT, $params[0] ?? 'undefined')
->setAttribute(static::PARAM_EXPIRATION, $params[0] ?? 'undefined')
->setAttribute(TraceAttributes::DB_NAMESPACE, $redis->getDBNum())
->startSpan();

Context::storage()->attach($span->storeInContext($context));
},
post: static function (Redis $redis, array $params, mixed $ret, ?Throwable $exception): void {
post: static function (RedisAdapterInterface $redis, array $params, mixed $ret, ?Throwable $exception): void {
if (TraceSampleResult::shouldSkipTraceBody()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class TraceSampleResult
public static function shouldSample(Request $request): int
{
$route = $request->attributes->get('_route');
$isCli = (bool)$request->server->get('argv');

if ($request->getMethod() !== Request::METHOD_GET) {
static::$result = static::SAMPLING_RESULT_ALLOW_ALL;
Expand All @@ -48,7 +49,7 @@ public static function shouldSample(Request $request): int
return static::$result;
}

if (static::decideForRootSpan()) {
if (static::decideForRootSpan($isCli)) {
static::$result = static::SAMPLING_RESULT_ALLOW_ROOT_SPAN;

return static::$result;
Expand Down Expand Up @@ -76,10 +77,13 @@ public static function shouldSkipRootSpan(): bool
}

/**
* @param bool $isCli
*
* @return bool
*/
protected static function decideForRootSpan(): bool
protected static function decideForRootSpan(bool $isCli): bool
{
return (mt_rand() / mt_getrandmax()) >= OpentelemetryInstrumentationConfig::getTraceSamplerProbability();
$probability = $isCli ? OpentelemetryInstrumentationConfig::getTraceCLISamplerProbability() : OpentelemetryInstrumentationConfig::getTraceSamplerProbability();
return (mt_rand() / mt_getrandmax()) >= $probability;
}
}
Loading

0 comments on commit cb16211

Please sign in to comment.