Skip to content

Commit

Permalink
Incriased visibility and fixed tree structure
Browse files Browse the repository at this point in the history
  • Loading branch information
gechetspr committed Dec 4, 2024
1 parent 8deb8d4 commit 871640b
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@

use InvalidArgumentException;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\TraceStateInterface;
use OpenTelemetry\Context\ContextInterface;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
use OpenTelemetry\SDK\Trace\SamplerInterface;
use OpenTelemetry\SDK\Trace\SamplingResult;
use Spryker\Service\Opentelemetry\OpentelemetryInstrumentationConfig;

class CriticalSpanTraceIdRatioSampler implements SamplerInterface, TraceStateAwareSamplerInterface
class CriticalSpanRatioSampler implements SamplerInterface, ParentSpanAwareSamplerInterface
{
/**
* @var string
Expand Down Expand Up @@ -85,13 +83,6 @@ public function shouldSample(
$probability = $this->nonCriticalProbability;
}

// $traceIdLimit = (1 << 60) - 1;
// $lowerOrderBytes = hexdec(substr($traceId, strlen($traceId) - 15, 15));
// $traceIdCondition = $lowerOrderBytes < round($probability * $traceIdLimit);
// if (!$traceIdCondition && $spanName !== 'Backoffice GET http://backoffice.de.spryker.local/') {
// var_dump($traceId,$traceIdCondition, $lowerOrderBytes, round($probability * $traceIdLimit), $traceIdLimit, $spanName);die;
// }

$result = mt_rand() / mt_getrandmax();
$traceIdCondition = $result <= $probability;
$decision = $traceIdCondition ? SamplingResult::RECORD_AND_SAMPLE : SamplingResult::DROP;
Expand All @@ -114,7 +105,7 @@ public function addParentSpan(SpanInterface $parentSpan): void
*/
public function getDescription(): string
{
return sprintf('%s{%.6F}', 'CriticalSpanTraceIdRatioSampler', $this->probability);
return sprintf('%s{%.6F}', 'CriticalSpanRatioSampler', $this->probability);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\TraceStateInterface;

interface TraceStateAwareSamplerInterface
interface ParentSpanAwareSamplerInterface
{
/**
* @param \OpenTelemetry\API\Trace\SpanInterface $parentSpan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use OpenTelemetry\SDK\Common\Attribute\AttributeValidator;
use OpenTelemetry\SDK\Common\Attribute\AttributeValidatorInterface;
use OpenTelemetry\SemConv\TraceAttributes;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\CriticalSpanTraceIdRatioSampler;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\CriticalSpanRatioSampler;

class AttributesBuilder implements AttributesBuilderInterface
{
Expand Down Expand Up @@ -139,7 +139,7 @@ protected function getSafeAttributes(): array
'search.query',
'root.url',
TraceAttributes::URL_DOMAIN,
CriticalSpanTraceIdRatioSampler::IS_CRITICAL_ATTRIBUTE,
CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use OpenTelemetry\API\Common\Time\Clock;
use OpenTelemetry\API\Trace\Span as OtelSpan;
use OpenTelemetry\API\Trace\SpanContextInterface;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\Context\ContextInterface;
use OpenTelemetry\SDK\Common\Attribute\AttributesBuilderInterface;
Expand Down Expand Up @@ -94,7 +93,7 @@ protected function __construct(
* @param \OpenTelemetry\API\Trace\SpanContextInterface $context
* @param \OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface $instrumentationScope
* @param int $kind
* @param \OpenTelemetry\API\Trace\SpanInterface $parentSpan
* @param \OpenTelemetry\API\Trace\SpanContextInterface $parentSpanContext
* @param \OpenTelemetry\Context\ContextInterface $parentContext
* @param \OpenTelemetry\SDK\Trace\SpanLimits $spanLimits
* @param \OpenTelemetry\SDK\Trace\SpanProcessorInterface $spanProcessor
Expand All @@ -111,7 +110,7 @@ public static function startSpan(
SpanContextInterface $context,
InstrumentationScopeInterface $instrumentationScope,
int $kind,
SpanInterface $parentSpan,
SpanContextInterface $parentSpanContext,
ContextInterface $parentContext,
SpanLimits $spanLimits,
SpanProcessorInterface $spanProcessor,
Expand All @@ -126,7 +125,7 @@ public static function startSpan(
$context,
$instrumentationScope,
$kind,
$parentSpan->getContext(),
$parentSpanContext,
$spanLimits,
$spanProcessor,
$resource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use OpenTelemetry\SDK\Trace\Link;
use OpenTelemetry\SDK\Trace\SamplingResult;
use OpenTelemetry\SDK\Trace\TracerSharedState;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\TraceStateAwareSamplerInterface;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\ParentSpanAwareSamplerInterface;

class SpanBuilder implements SpanBuilderInterface
{
Expand Down Expand Up @@ -53,6 +53,11 @@ class SpanBuilder implements SpanBuilderInterface
*/
protected int $startEpochNanos = 0;

/**
* @var array<string, string>
*/
protected static $parentMapping = [];

/**
* @param string $spanName
* @param \OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface $instrumentationScope
Expand Down Expand Up @@ -187,15 +192,25 @@ public function startSpan(): SpanInterface
);

if (!in_array($samplingDecision, [SamplingResult::RECORD_AND_SAMPLE, SamplingResult::RECORD_ONLY], true)) {
static::$parentMapping[$spanId] = $parentSpanContext->getSpanId();

if ($spanContext instanceof SpanIdUpdateAwareSpanContextInterface) {
$spanContext->updateSpanId($parentSpanContext->getSpanId());
}

return Span::wrap($spanContext);
}

if (isset(static::$parentMapping[$parentSpanContext->getSpanId()]) && $parentSpanContext instanceof SpanIdUpdateAwareSpanContextInterface) {
$parentSpanContext->updateSpanId(static::$parentMapping[$parentSpanContext->getSpanId()]);
}

return Span::startSpan(
$this->spanName,
$spanContext,
$this->instrumentationScope,
$this->spanKind,
$parentSpan,
$parentSpanContext,
$parentContext,
$this->tracerSharedState->getSpanLimits(),
$this->tracerSharedState->getSpanProcessor(),
Expand All @@ -220,7 +235,7 @@ protected function getSamplingResult(SpanInterface $parentSpan, ContextInterface
->tracerSharedState
->getSampler();

if ($sampler instanceof TraceStateAwareSamplerInterface) {
if ($sampler instanceof ParentSpanAwareSamplerInterface) {
$sampler->addParentSpan($parentSpan);;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use OpenTelemetry\API\Trace\TraceStateInterface;
use function hex2bin;

class SpanContext implements SpanContextInterface
class SpanContext implements SpanContextInterface, SpanIdUpdateAwareSpanContextInterface
{
protected static ?SpanContextInterface $invalidContext = null;

Expand Down Expand Up @@ -172,4 +172,14 @@ public static function getInvalid(): SpanContextInterface

return static::$invalidContext;
}

/**
* @param string $spanId
*
* @return void
*/
public function updateSpanId(string $spanId): void
{
$this->spanId = $spanId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Spryker\Service\Opentelemetry\Instrumentation\Span;

interface SpanIdUpdateAwareSpanContextInterface
{
/**
* @param string $spanId
* @return void
*/
public function updateSpanId(string $spanId): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
use OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessorBuilder;
use OpenTelemetry\SDK\Trace\SpanProcessorInterface;
use SplQueue;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\CriticalSpanTraceIdRatioSampler;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\CriticalSpanRatioSampler;
use Spryker\Service\Opentelemetry\Instrumentation\Span\SpanIdUpdateAwareSpanContextInterface;
use Spryker\Service\Opentelemetry\OpentelemetryInstrumentationConfig;
use Throwable;

Expand Down Expand Up @@ -145,6 +146,11 @@ class PostFilterBatchSpanProcessor implements SpanProcessorInterface
*/
protected bool $closed = false;

/**
* @var array<string, string>
*/
protected static $parentSpanIdMapping = [];

/**
* @param \OpenTelemetry\SDK\Trace\SpanExporterInterface $exporter
* @param \OpenTelemetry\API\Common\Time\ClockInterface $clock
Expand Down Expand Up @@ -200,7 +206,7 @@ public function onEnd(ReadableSpanInterface $span): void
return;
}

$duration = $span->getAttribute(CriticalSpanTraceIdRatioSampler::IS_CRITICAL_ATTRIBUTE) === true
$duration = $span->getAttribute(CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE) === true
? OpentelemetryInstrumentationConfig::getSamplerThresholdNanoForCriticalSpan()
: OpentelemetryInstrumentationConfig::getSamplerThresholdNano();

Expand All @@ -210,6 +216,7 @@ public function onEnd(ReadableSpanInterface $span): void
&& $span->getStatus()->getCode() === StatusCode::STATUS_OK
) {
$this->dropped++;
static::$parentSpanIdMapping[$span->getSpanId()] = $span->getParentContext()->getSpanId();

return;
}
Expand All @@ -221,6 +228,11 @@ public function onEnd(ReadableSpanInterface $span): void
}

$this->queueSize++;

if (isset(static::$parentSpanIdMapping[$span->getParentContext()->getSpanId()]) && $span->getParentContext() instanceof SpanIdUpdateAwareSpanContextInterface) {
$span->getParentContext()->updateSpanId(static::$parentSpanIdMapping[$span->getParentContext()->getSpanId()]);
}

$this->batch[] = $span;
$this->nextScheduledRun ??= $this->clock->now() + $this->scheduledDelayNanos;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use OpenTelemetry\SemConv\TraceAttributes;
use Spryker\Service\Opentelemetry\Instrumentation\Resource\ResourceInfo;
use Spryker\Service\Opentelemetry\Instrumentation\Resource\ResourceInfoFactory;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\CriticalSpanTraceIdRatioSampler;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\CriticalSpanRatioSampler;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\TraceSampleResult;
use Spryker\Service\Opentelemetry\Instrumentation\Span\Attributes;
use Spryker\Service\Opentelemetry\Instrumentation\Span\SpanConverter;
Expand Down Expand Up @@ -159,7 +159,7 @@ protected static function createSpanProcessor(): SpanProcessorInterface
*/
protected static function createSampler(): SamplerInterface
{
return new CriticalSpanTraceIdRatioSampler(
return new CriticalSpanRatioSampler(
OpentelemetryInstrumentationConfig::getSamplerProbability(),
OpentelemetryInstrumentationConfig::getSamplerProbabilityForCriticalSpans(),
OpentelemetryInstrumentationConfig::getSamplerProbabilityForNonCriticalSpans(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static function getSamplerProbability(): float
{
$probability = getenv(static::OTEL_TRACES_SAMPLER_ARG) ?: 0.4;

return (float)$probability;
return (float)0.4;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected function extractClassDetailsFromFile(string $filePath): array
$tokens = token_get_all($content);
$namespace = $this->parseNamespace($tokens);
$class = $this->parseClassName($tokens);
$methods = $this->parseMethods($tokens);
$methods = $this->parseMethods($tokens, $class);

if ($class && $methods !== []) {
return [
Expand Down Expand Up @@ -187,10 +187,11 @@ protected function parseClassName(array $tokens): string

/**
* @param array<mixed> $tokens
* @param string $class
*
* @return array<string>
*/
protected function parseMethods(array $tokens): array
protected function parseMethods(array $tokens, string $class): array
{
$methods = [];
$functionLevel = 0;
Expand All @@ -199,7 +200,7 @@ protected function parseMethods(array $tokens): array

for ($i = 0; $i < $tokensCount; $i++) {
if ($tokens[$i][0] === T_FUNCTION) {
if (!$this->isMethodAllowed($tokens, $i)) {
if (!$this->isMethodAllowed($tokens, $i, $class)) {
break;
}
$isAnonymousFunction = false;
Expand Down Expand Up @@ -241,20 +242,28 @@ protected function parseMethods(array $tokens): array
return $methods;
}

protected function isMethodAllowed(array $tokens, int $functionIndex): bool
/**
* @param array $tokens
* @param int $functionIndex
* @param string $class
*
* @return bool
*/
protected function isMethodAllowed(array $tokens, int $functionIndex, string $class): bool
{
return $this->checkForPublicMethods($tokens, $functionIndex) && $this->checkForSimpleMethods($tokens, $functionIndex);
return $this->checkForPublicMethods($tokens, $functionIndex, $class) && $this->checkForSimpleMethods($tokens, $functionIndex);
}

/**
* @param array $tokens
* @param int $functionIndex
* @param string $class
*
* @return bool
*/
protected function checkForPublicMethods(array $tokens, int $functionIndex): bool
protected function checkForPublicMethods(array $tokens, int $functionIndex, string $class): bool
{
if (!$this->config->areOnlyPublicMethodsInstrumented()) {
if (!$this->config->areOnlyPublicMethodsInstrumented() || str_contains($class, 'Controller')) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ function: \'%s\',
->setAttribute(\\OpenTelemetry\\SemConv\\TraceAttributes::CODE_NAMESPACE, $class)
->setAttribute(\\OpenTelemetry\\SemConv\\TraceAttributes::CODE_FILEPATH, $filename)
->setAttribute(\\OpenTelemetry\\SemConv\\TraceAttributes::CODE_LINENO, $lineno)
->setAttribute(\\Spryker\\Service\\Opentelemetry\\Instrumentation\\Sampler\\CriticalSpanRatioSampler::IS_CRITICAL_ATTRIBUTE, %s)
->startSpan();
\\OpenTelemetry\\Context\\Context::storage()->attach($span->storeInContext($context));
Expand Down Expand Up @@ -122,6 +123,7 @@ function: \'%s\',
$class[static::CLASS_NAME_KEY],
$method,
$methodHookName,
$this->isCriticalHook($class) ? 'true' : 'null',
);
}

Expand All @@ -143,4 +145,14 @@ protected function buildMethodHookName(array $class, string $method): string
$method,
);
}

/**
* @param array<string, mixed> $class
*
* @return bool
*/
protected function isCriticalHook(array $class): bool
{
return str_contains($class[static::CLASS_NAME_KEY], 'Facade') || str_contains($class[static::CLASS_NAME_KEY], 'Controller');
}
}
Loading

0 comments on commit 871640b

Please sign in to comment.