Skip to content

Commit

Permalink
Performance improvementes
Browse files Browse the repository at this point in the history
  • Loading branch information
gechetspr committed Nov 17, 2024
1 parent 4ce318a commit 0e3f99d
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use InvalidArgumentException;
use OpenTelemetry\API\Trace\SpanContextInterface;
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 OpenTelemetry\SDK\Trace\Span;

class CriticalSpanTraceIdRatioSampler implements SamplerInterface, ParentSpanAwareSamplerInterface
{
Expand All @@ -29,14 +29,9 @@ class CriticalSpanTraceIdRatioSampler implements SamplerInterface, ParentSpanAwa
protected float $probability;

/**
* @var \OpenTelemetry\API\Trace\SpanInterface|null
* @var \OpenTelemetry\API\Trace\TraceStateInterface|null
*/
protected ?SpanInterface $parentSpan = null;

/**
* @var \OpenTelemetry\API\Trace\SpanContextInterface|null
*/
protected ?SpanContextInterface $parentSpanContext = null;
protected ?TraceStateInterface $traceState = null;

/**
* @param float $probability
Expand Down Expand Up @@ -67,20 +62,16 @@ public function shouldSample(
AttributesInterface $attributes,
array $links,
): SamplingResult {
$parentSpan = $this->parentSpan ?: Span::fromContext($parentContext);
$parentSpanContext = $this->parentSpanContext ?: $parentSpan->getContext();
$traceState = $parentSpanContext->getTraceState();

if ($attributes->has(static::IS_CRITICAL_ATTRIBUTE)) {
return new SamplingResult(SamplingResult::RECORD_AND_SAMPLE, [], $traceState);
return new SamplingResult(SamplingResult::RECORD_AND_SAMPLE, [], $this->traceState);
}

$traceIdLimit = (1 << 60) - 1;
$lowerOrderBytes = hexdec(substr($traceId, strlen($traceId) - 15, 15));
$traceIdCondition = $lowerOrderBytes < round($this->probability * $traceIdLimit);
$decision = $traceIdCondition ? SamplingResult::RECORD_AND_SAMPLE : SamplingResult::DROP;

return new SamplingResult($decision, [], $traceState);
return new SamplingResult($decision, [], $this->traceState);
}

/**
Expand All @@ -93,6 +84,11 @@ public function addParentSpan(SpanInterface $span): void
$this->parentSpan = $span;
}

public function addTraceState(TraceStateInterface $traceState): void
{

}

/**
* @return string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,14 @@

namespace Spryker\Service\Opentelemetry\Instrumentation\Sampler;

use OpenTelemetry\API\Trace\SpanContextInterface;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\TraceStateInterface;

interface ParentSpanAwareSamplerInterface
{
/**
* @param \OpenTelemetry\API\Trace\SpanInterface $span
* @param \OpenTelemetry\API\Trace\TraceStateInterface $span
*
* @return void
*/
public function addParentSpan(SpanInterface $span): void;

/**
* @param \OpenTelemetry\API\Trace\SpanContextInterface $spanContext
*
* @return void
*/
public function addParentSpanContext(SpanContextInterface $spanContext): void;
public function addTraceState(TraceStateInterface $span): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function offsetSet($offset, $value): void

return;
}
if (!in_array($offset, $this->getSafeAttributes()) && !$this->attributeValidator->validate($value)) {
if (!in_array($offset, $this->getSafeAttributes(), true) && !$this->attributeValidator->validate($value)) {
$this->droppedAttributesCount++;

return;
Expand Down
34 changes: 17 additions & 17 deletions src/Spryker/Service/Opentelemetry/Instrumentation/Span/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,22 @@ public static function startSpan(

return $span;
}

/**
* Backward compatibility methods
*
* @codeCoverageIgnore
*/
public static function formatStackTrace(Throwable $e, ?array &$seen = null): string
{
BcUtil::triggerMethodDeprecationNotice(
__METHOD__,
'format',
StackTraceFormatter::class
);

return StackTraceFormatter::format($e);
}
//
// /**
// * Backward compatibility methods
// *
// * @codeCoverageIgnore
// */
// public static function formatStackTrace(Throwable $e, ?array &$seen = null): string
// {
// BcUtil::triggerMethodDeprecationNotice(
// __METHOD__,
// 'format',
// StackTraceFormatter::class
// );
//
// return StackTraceFormatter::format($e);
// }

/** @inheritDoc */
public function getContext(): SpanContextInterface
Expand Down Expand Up @@ -292,7 +292,7 @@ public function toSpanData(): SpanDataInterface
$this->totalRecordedEvents,
$this->status,
$this->endEpochNanos,
$this->hasEnded
$this->hasEnded,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function startSpan(): SpanInterface
$spanId = $this->tracerSharedState->getIdGenerator()->generateSpanId();
$traceId = $parentSpanContext->isValid() ? $parentSpanContext->getTraceId() : $this->tracerSharedState->getIdGenerator()->generateTraceId();

$samplingResult = $this->getSamplingResult($parentSpan, $parentContext, $traceId, $parentSpanContext);
$samplingResult = $this->getSamplingResult($parentSpan, $parentContext, $traceId);
$samplingDecision = $samplingResult->getDecision();
$samplingResultTraceState = $samplingResult->getTraceState();

Expand Down Expand Up @@ -212,19 +212,17 @@ public function startSpan(): SpanInterface
* @param \OpenTelemetry\API\Trace\SpanInterface $parentSpan
* @param \OpenTelemetry\Context\ContextInterface $parentContext
* @param string $traceId
* @param \OpenTelemetry\API\Trace\SpanContextInterface $spanContext
*
* @return \OpenTelemetry\SDK\Trace\SamplingResult
*/
protected function getSamplingResult(SpanInterface $parentSpan, ContextInterface $parentContext, string $traceId, SpanContextInterface $spanContext): SamplingResult
protected function getSamplingResult(SpanInterface $parentSpan, ContextInterface $parentContext, string $traceId): SamplingResult
{
$sampler = $this
->tracerSharedState
->getSampler();

if ($sampler instanceof ParentSpanAwareSamplerInterface) {
$sampler->addParentSpan($parentSpan);
$sampler->addParentSpanContext($spanContext);
$sampler->addTraceState($parentSpan->getContext()->getTraceState());
}

return $sampler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Spryker\Service\Opentelemetry\Instrumentation\Span;

use OpenTelemetry\API\Behavior\LogsMessagesTrait;
use OpenTelemetry\Contrib\Otlp\SpanConverter;
use Opentelemetry\Proto\Collector\Trace\V1\ExportTraceServiceResponse;
use OpenTelemetry\SDK\Common\Export\TransportInterface;
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
Expand All @@ -15,7 +14,10 @@ class SpanExporter implements SpanExporterInterface
{
use LogsMessagesTrait;

public function __construct(protected TransportInterface $transport)
public function __construct(
protected TransportInterface $transport,
protected SpanConverter $spanConverter,
)
{
}

Expand All @@ -28,7 +30,7 @@ public function __construct(protected TransportInterface $transport)
public function export(iterable $batch, ?CancellationInterface $cancellation = null): FutureInterface
{
return $this->transport
->send((new SpanConverter())->convert($batch)->serializeToString(), $cancellation)
->send($this->spanConverter->convert($batch)->serializeToString(), $cancellation)
->map(function (?string $payload): bool {
if ($payload === null) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ public function onEnd(ReadableSpanInterface $span): void

if (
$span->getDuration() < OpentelemetryConfig::getSamplerThresholdNano()
&& $span->toSpanData()->getParentSpanId()
&& $span->toSpanData()->getStatus()->getCode() === StatusCode::STATUS_OK
&& $span->getParentSpanId()
&& $span->getStatus()->getCode() === StatusCode::STATUS_OK
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OpenTelemetry\SemConv\ResourceAttributes;
use OpenTelemetry\SemConv\TraceAttributes;
use Spryker\Service\Opentelemetry\Instrumentation\Sampler\CriticalSpanTraceIdRatioSampler;
use Spryker\Service\Opentelemetry\Instrumentation\Span\SpanConverter;
use Spryker\Service\Opentelemetry\Instrumentation\Span\SpanExporter;
use Spryker\Service\Opentelemetry\Instrumentation\SpanProcessor\PostFilterBatchSpanProcessor;
use Spryker\Service\Opentelemetry\Instrumentation\Tracer\TracerProvider;
Expand Down Expand Up @@ -127,6 +128,7 @@ protected static function createSpanExporter(): SpanExporterInterface
{
return new SpanExporter(
(new GrpcTransportFactory())->create(OpentelemetryConfig::getExporterEndpoint() . OtlpUtil::method(Signals::TRACE)),
new SpanConverter(),
);
}

Expand Down

0 comments on commit 0e3f99d

Please sign in to comment.