From cdd7ad020e165fe680703b6d3319b908b682fb7a Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 20 Oct 2023 17:12:29 +0300 Subject: jaeger-client -> opentelemetry --- .../sdk/Metrics/Exemplar/BucketStorage.php | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 vendor/open-telemetry/sdk/Metrics/Exemplar/BucketStorage.php (limited to 'vendor/open-telemetry/sdk/Metrics/Exemplar/BucketStorage.php') diff --git a/vendor/open-telemetry/sdk/Metrics/Exemplar/BucketStorage.php b/vendor/open-telemetry/sdk/Metrics/Exemplar/BucketStorage.php new file mode 100644 index 000000000..574ce92af --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Exemplar/BucketStorage.php @@ -0,0 +1,92 @@ + */ + private array $buckets; + + public function __construct(int $size = 0) + { + $this->buckets = array_fill(0, $size, null); + } + + /** + * @param int|string $index + * @param float|int $value + */ + public function store(int $bucket, $index, $value, AttributesInterface $attributes, ContextInterface $context, int $timestamp): void + { + assert($bucket <= count($this->buckets)); + + $exemplar = $this->buckets[$bucket] ??= new BucketEntry(); + $exemplar->index = $index; + $exemplar->value = $value; + $exemplar->timestamp = $timestamp; + $exemplar->attributes = $attributes; + + if (($spanContext = Span::fromContext($context)->getContext())->isValid()) { + $exemplar->traceId = $spanContext->getTraceId(); + $exemplar->spanId = $spanContext->getSpanId(); + } else { + $exemplar->traceId = null; + $exemplar->spanId = null; + } + } + + /** + * @param array $dataPointAttributes + * @return array + */ + public function collect(array $dataPointAttributes): array + { + $exemplars = []; + foreach ($this->buckets as $index => &$exemplar) { + if (!$exemplar) { + continue; + } + + $exemplars[$index] = new Exemplar( + $exemplar->index, + $exemplar->value, + $exemplar->timestamp, + $this->filterExemplarAttributes( + $dataPointAttributes[$exemplar->index], + $exemplar->attributes, + ), + $exemplar->traceId, + $exemplar->spanId, + ); + $exemplar = null; + } + + return $exemplars; + } + + private function filterExemplarAttributes(AttributesInterface $dataPointAttributes, AttributesInterface $exemplarAttributes): AttributesInterface + { + $attributes = []; + foreach ($exemplarAttributes as $key => $value) { + if ($dataPointAttributes->get($key) === null) { + $attributes[$key] = $value; + } + } + + return new Attributes($attributes, $exemplarAttributes->getDroppedAttributesCount()); + } +} -- cgit v1.2.3