summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Trace/Sampler
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/open-telemetry/sdk/Trace/Sampler')
-rw-r--r--vendor/open-telemetry/sdk/Trace/Sampler/AlwaysOffSampler.php50
-rw-r--r--vendor/open-telemetry/sdk/Trace/Sampler/AlwaysOnSampler.php50
-rw-r--r--vendor/open-telemetry/sdk/Trace/Sampler/ParentBased.php100
-rw-r--r--vendor/open-telemetry/sdk/Trace/Sampler/TraceIdRatioBasedSampler.php70
4 files changed, 270 insertions, 0 deletions
diff --git a/vendor/open-telemetry/sdk/Trace/Sampler/AlwaysOffSampler.php b/vendor/open-telemetry/sdk/Trace/Sampler/AlwaysOffSampler.php
new file mode 100644
index 000000000..ee7e70a56
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Trace/Sampler/AlwaysOffSampler.php
@@ -0,0 +1,50 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Trace\Sampler;
+
+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;
+
+/**
+ * This implementation of the SamplerInterface always skips record.
+ * Example:
+ * ```
+ * use OpenTelemetry\Sdk\Trace\AlwaysOffSampler;
+ * $sampler = new AlwaysOffSampler();
+ * ```
+ */
+class AlwaysOffSampler implements SamplerInterface
+{
+ /**
+ * Returns false because we never want to sample.
+ * {@inheritdoc}
+ */
+ public function shouldSample(
+ ContextInterface $parentContext,
+ string $traceId,
+ string $spanName,
+ int $spanKind,
+ AttributesInterface $attributes,
+ array $links
+ ): SamplingResult {
+ $parentSpan = Span::fromContext($parentContext);
+ $parentSpanContext = $parentSpan->getContext();
+ $traceState = $parentSpanContext->getTraceState();
+
+ return new SamplingResult(
+ SamplingResult::DROP,
+ [],
+ $traceState
+ );
+ }
+
+ public function getDescription(): string
+ {
+ return 'AlwaysOffSampler';
+ }
+}
diff --git a/vendor/open-telemetry/sdk/Trace/Sampler/AlwaysOnSampler.php b/vendor/open-telemetry/sdk/Trace/Sampler/AlwaysOnSampler.php
new file mode 100644
index 000000000..df61d1aee
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Trace/Sampler/AlwaysOnSampler.php
@@ -0,0 +1,50 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Trace\Sampler;
+
+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;
+
+/**
+ * This implementation of the SamplerInterface always records.
+ * Example:
+ * ```
+ * use OpenTelemetry\Sdk\Trace\AlwaysOnSampler;
+ * $sampler = new AlwaysOnSampler();
+ * ```
+ */
+class AlwaysOnSampler implements SamplerInterface
+{
+ /**
+ * Returns true because we always want to sample.
+ * {@inheritdoc}
+ */
+ public function shouldSample(
+ ContextInterface $parentContext,
+ string $traceId,
+ string $spanName,
+ int $spanKind,
+ AttributesInterface $attributes,
+ array $links
+ ): SamplingResult {
+ $parentSpan = Span::fromContext($parentContext);
+ $parentSpanContext = $parentSpan->getContext();
+ $traceState = $parentSpanContext->getTraceState();
+
+ return new SamplingResult(
+ SamplingResult::RECORD_AND_SAMPLE,
+ [],
+ $traceState
+ );
+ }
+
+ public function getDescription(): string
+ {
+ return 'AlwaysOnSampler';
+ }
+}
diff --git a/vendor/open-telemetry/sdk/Trace/Sampler/ParentBased.php b/vendor/open-telemetry/sdk/Trace/Sampler/ParentBased.php
new file mode 100644
index 000000000..db801d3d8
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Trace/Sampler/ParentBased.php
@@ -0,0 +1,100 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Trace\Sampler;
+
+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;
+
+/**
+ * Phan seems to struggle with the variadic arguments in the latest version
+ * @phan-file-suppress PhanParamTooFewUnpack
+ */
+
+/**
+ * This implementation of the SamplerInterface that respects parent context's sampling decision
+ * and delegates for the root span.
+ * Example:
+ * ```
+ * use OpenTelemetry\API\Trace\ParentBased;
+ * use OpenTelemetry\API\Trace\AlwaysOnSampler
+ *
+ * $rootSampler = new AlwaysOnSampler();
+ * $sampler = new ParentBased($rootSampler);
+ * ```
+ */
+class ParentBased implements SamplerInterface
+{
+ private SamplerInterface $root;
+
+ private SamplerInterface $remoteParentSampler;
+
+ private SamplerInterface $remoteParentNotSampler;
+
+ private SamplerInterface $localParentSampler;
+
+ private SamplerInterface $localParentNotSampler;
+
+ /**
+ * ParentBased sampler delegates the sampling decision based on the parent context.
+ *
+ * @param SamplerInterface $root Sampler called for the span with no parent (root span).
+ * @param SamplerInterface|null $remoteParentSampler Sampler called for the span with the remote sampled parent. When null, `AlwaysOnSampler` is used.
+ * @param SamplerInterface|null $remoteParentNotSampler Sampler called for the span with the remote not sampled parent. When null, `AlwaysOffSampler` is used.
+ * @param SamplerInterface|null $localParentSampler Sampler called for the span with local the sampled parent. When null, `AlwaysOnSampler` is used.
+ * @param SamplerInterface|null $localParentNotSampler Sampler called for the span with the local not sampled parent. When null, `AlwaysOffSampler` is used.
+ */
+ public function __construct(
+ SamplerInterface $root,
+ ?SamplerInterface $remoteParentSampler = null,
+ ?SamplerInterface $remoteParentNotSampler = null,
+ ?SamplerInterface $localParentSampler = null,
+ ?SamplerInterface $localParentNotSampler = null
+ ) {
+ $this->root = $root;
+ $this->remoteParentSampler = $remoteParentSampler ?? new AlwaysOnSampler();
+ $this->remoteParentNotSampler = $remoteParentNotSampler ?? new AlwaysOffSampler();
+ $this->localParentSampler = $localParentSampler ?? new AlwaysOnSampler();
+ $this->localParentNotSampler = $localParentNotSampler ?? new AlwaysOffSampler();
+ }
+
+ /**
+ * Invokes the respective delegate sampler when parent is set or uses root sampler for the root span.
+ * {@inheritdoc}
+ */
+ public function shouldSample(
+ ContextInterface $parentContext,
+ string $traceId,
+ string $spanName,
+ int $spanKind,
+ AttributesInterface $attributes,
+ array $links
+ ): SamplingResult {
+ $parentSpan = Span::fromContext($parentContext);
+ $parentSpanContext = $parentSpan->getContext();
+
+ // Invalid parent SpanContext indicates root span is being created
+ if (!$parentSpanContext->isValid()) {
+ return $this->root->shouldSample(...func_get_args());
+ }
+
+ if ($parentSpanContext->isRemote()) {
+ return $parentSpanContext->isSampled()
+ ? $this->remoteParentSampler->shouldSample(...func_get_args())
+ : $this->remoteParentNotSampler->shouldSample(...func_get_args());
+ }
+
+ return $parentSpanContext->isSampled()
+ ? $this->localParentSampler->shouldSample(...func_get_args())
+ : $this->localParentNotSampler->shouldSample(...func_get_args());
+ }
+
+ public function getDescription(): string
+ {
+ return 'ParentBased+' . $this->root->getDescription();
+ }
+}
diff --git a/vendor/open-telemetry/sdk/Trace/Sampler/TraceIdRatioBasedSampler.php b/vendor/open-telemetry/sdk/Trace/Sampler/TraceIdRatioBasedSampler.php
new file mode 100644
index 000000000..c11a90d5d
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Trace/Sampler/TraceIdRatioBasedSampler.php
@@ -0,0 +1,70 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Trace\Sampler;
+
+use InvalidArgumentException;
+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;
+
+/**
+ * This implementation of the SamplerInterface records with given probability.
+ * Example:
+ * ```
+ * use OpenTelemetry\API\Trace\TraceIdRatioBasedSampler;
+ * $sampler = new TraceIdRatioBasedSampler(0.01);
+ * ```
+ */
+class TraceIdRatioBasedSampler implements SamplerInterface
+{
+ private float $probability;
+
+ /**
+ * TraceIdRatioBasedSampler constructor.
+ * @param float $probability Probability float value between 0.0 and 1.0.
+ */
+ public function __construct(float $probability)
+ {
+ if ($probability < 0.0 || $probability > 1.0) {
+ throw new InvalidArgumentException('probability should be be between 0.0 and 1.0.');
+ }
+ $this->probability = $probability;
+ }
+
+ /**
+ * Returns `SamplingResult` based on probability. Respects the parent `SampleFlag`
+ * {@inheritdoc}
+ */
+ public function shouldSample(
+ ContextInterface $parentContext,
+ string $traceId,
+ string $spanName,
+ int $spanKind,
+ AttributesInterface $attributes,
+ array $links
+ ): SamplingResult {
+ // TODO: Add config to adjust which spans get sampled (only default from specification is implemented)
+ $parentSpan = Span::fromContext($parentContext);
+ $parentSpanContext = $parentSpan->getContext();
+ $traceState = $parentSpanContext->getTraceState();
+
+ /**
+ * Since php can only store up to 63 bit positive integers
+ */
+ $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);
+ }
+
+ public function getDescription(): string
+ {
+ return sprintf('%s{%.6F}', 'TraceIdRatioBasedSampler', $this->probability);
+ }
+}