summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Metrics/MetricFactory
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2023-10-20 17:12:29 +0300
committerAndrew Dolgov <[email protected]>2023-10-20 21:13:39 +0300
commitcdd7ad020e165fe680703b6d3319b908b682fb7a (patch)
treeb51eb09b7b4587e8fbc5624ac8d88d28cfcd0b04 /vendor/open-telemetry/sdk/Metrics/MetricFactory
parent45a9ff0c88cbd33892ff16ab837e9059937d656e (diff)
jaeger-client -> opentelemetry
Diffstat (limited to 'vendor/open-telemetry/sdk/Metrics/MetricFactory')
-rw-r--r--vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamFactory.php187
-rw-r--r--vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamMetricSource.php44
-rw-r--r--vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamMetricSourceProvider.php98
3 files changed, 329 insertions, 0 deletions
diff --git a/vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamFactory.php b/vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamFactory.php
new file mode 100644
index 000000000..2c3af4c06
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamFactory.php
@@ -0,0 +1,187 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Metrics\MetricFactory;
+
+use function array_keys;
+use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;
+use OpenTelemetry\SDK\Metrics\Aggregation\ExplicitBucketHistogramAggregation;
+use OpenTelemetry\SDK\Metrics\AggregationInterface;
+use OpenTelemetry\SDK\Metrics\AttributeProcessor\FilteredAttributeProcessor;
+use OpenTelemetry\SDK\Metrics\AttributeProcessorInterface;
+use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarFilterInterface;
+use OpenTelemetry\SDK\Metrics\Exemplar\ExemplarReservoirInterface;
+use OpenTelemetry\SDK\Metrics\Exemplar\FilteredReservoir;
+use OpenTelemetry\SDK\Metrics\Exemplar\FixedSizeReservoir;
+use OpenTelemetry\SDK\Metrics\Exemplar\HistogramBucketReservoir;
+use OpenTelemetry\SDK\Metrics\Instrument;
+use OpenTelemetry\SDK\Metrics\MetricFactoryInterface;
+use OpenTelemetry\SDK\Metrics\MetricRegistrationInterface;
+use OpenTelemetry\SDK\Metrics\MetricRegistry\MetricCollectorInterface;
+use OpenTelemetry\SDK\Metrics\MetricRegistry\MetricRegistryInterface;
+use OpenTelemetry\SDK\Metrics\Stream\AsynchronousMetricStream;
+use OpenTelemetry\SDK\Metrics\Stream\MetricAggregator;
+use OpenTelemetry\SDK\Metrics\Stream\MetricAggregatorFactory;
+use OpenTelemetry\SDK\Metrics\Stream\MetricStreamInterface;
+use OpenTelemetry\SDK\Metrics\Stream\SynchronousMetricStream;
+use OpenTelemetry\SDK\Metrics\ViewProjection;
+use OpenTelemetry\SDK\Resource\ResourceInfo;
+use function serialize;
+use function spl_object_id;
+use Throwable;
+
+/**
+ * @internal
+ */
+final class StreamFactory implements MetricFactoryInterface
+{
+ public function createAsynchronousObserver(
+ MetricRegistryInterface $registry,
+ ResourceInfo $resource,
+ InstrumentationScopeInterface $instrumentationScope,
+ Instrument $instrument,
+ int $timestamp,
+ iterable $views
+ ): array {
+ $streams = [];
+ $dedup = [];
+ foreach ($views as [$view, $registration]) {
+ if ($view->aggregation === null) {
+ continue;
+ }
+
+ $dedupId = $this->streamId($view->aggregation, $view->attributeKeys);
+ if (($streamId = $dedup[$dedupId] ?? null) === null) {
+ $stream = new AsynchronousMetricStream($view->aggregation, $timestamp);
+ $streamId = $registry->registerAsynchronousStream($instrument, $stream, new MetricAggregatorFactory(
+ $this->attributeProcessor($view->attributeKeys),
+ $view->aggregation,
+ ));
+
+ $streams[$streamId] = $stream;
+ $dedup[$dedupId] = $streamId;
+ }
+
+ $this->registerSource(
+ $view,
+ $instrument,
+ $instrumentationScope,
+ $resource,
+ $streams[$streamId],
+ $registry,
+ $registration,
+ $streamId,
+ );
+ }
+
+ return array_keys($streams);
+ }
+
+ public function createSynchronousWriter(
+ MetricRegistryInterface $registry,
+ ResourceInfo $resource,
+ InstrumentationScopeInterface $instrumentationScope,
+ Instrument $instrument,
+ int $timestamp,
+ iterable $views,
+ ?ExemplarFilterInterface $exemplarFilter = null
+ ): array {
+ $streams = [];
+ $dedup = [];
+ foreach ($views as [$view, $registration]) {
+ if ($view->aggregation === null) {
+ continue;
+ }
+
+ $dedupId = $this->streamId($view->aggregation, $view->attributeKeys);
+ if (($streamId = $dedup[$dedupId] ?? null) === null) {
+ $stream = new SynchronousMetricStream($view->aggregation, $timestamp);
+ $streamId = $registry->registerSynchronousStream($instrument, $stream, new MetricAggregator(
+ $this->attributeProcessor($view->attributeKeys),
+ $view->aggregation,
+ $this->createExemplarReservoir($view->aggregation, $exemplarFilter),
+ ));
+
+ $streams[$streamId] = $stream;
+ $dedup[$dedupId] = $streamId;
+ }
+
+ $this->registerSource(
+ $view,
+ $instrument,
+ $instrumentationScope,
+ $resource,
+ $streams[$streamId],
+ $registry,
+ $registration,
+ $streamId,
+ );
+ }
+
+ return array_keys($streams);
+ }
+
+ private function attributeProcessor(
+ ?array $attributeKeys
+ ): ?AttributeProcessorInterface {
+ return $attributeKeys !== null
+ ? new FilteredAttributeProcessor($attributeKeys)
+ : null;
+ }
+
+ private function createExemplarReservoir(
+ AggregationInterface $aggregation,
+ ?ExemplarFilterInterface $exemplarFilter
+ ): ?ExemplarReservoirInterface {
+ if (!$exemplarFilter) {
+ return null;
+ }
+
+ if ($aggregation instanceof ExplicitBucketHistogramAggregation && $aggregation->boundaries) {
+ $exemplarReservoir = new HistogramBucketReservoir($aggregation->boundaries);
+ } else {
+ $exemplarReservoir = new FixedSizeReservoir();
+ }
+
+ return new FilteredReservoir($exemplarReservoir, $exemplarFilter);
+ }
+
+ private function registerSource(
+ ViewProjection $view,
+ Instrument $instrument,
+ InstrumentationScopeInterface $instrumentationScope,
+ ResourceInfo $resource,
+ MetricStreamInterface $stream,
+ MetricCollectorInterface $metricCollector,
+ MetricRegistrationInterface $metricRegistration,
+ int $streamId
+ ): void {
+ $provider = new StreamMetricSourceProvider(
+ $view,
+ $instrument,
+ $instrumentationScope,
+ $resource,
+ $stream,
+ $metricCollector,
+ $streamId,
+ );
+
+ $metricRegistration->register($provider, $provider);
+ }
+
+ private function streamId(AggregationInterface $aggregation, ?array $attributeKeys): string
+ {
+ return $this->trySerialize($aggregation) . serialize($attributeKeys);
+ }
+
+ private function trySerialize(object $object)
+ {
+ try {
+ return serialize($object);
+ } catch (Throwable $e) {
+ }
+
+ return spl_object_id($object);
+ }
+}
diff --git a/vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamMetricSource.php b/vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamMetricSource.php
new file mode 100644
index 000000000..4939a5341
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamMetricSource.php
@@ -0,0 +1,44 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Metrics\MetricFactory;
+
+use OpenTelemetry\SDK\Metrics\Data\Metric;
+use OpenTelemetry\SDK\Metrics\MetricSourceInterface;
+
+/**
+ * @internal
+ */
+final class StreamMetricSource implements MetricSourceInterface
+{
+ private StreamMetricSourceProvider $provider;
+ private int $reader;
+ public function __construct(StreamMetricSourceProvider $provider, int $reader)
+ {
+ $this->provider = $provider;
+ $this->reader = $reader;
+ }
+
+ public function collectionTimestamp(): int
+ {
+ return $this->provider->stream->timestamp();
+ }
+
+ public function collect(): Metric
+ {
+ return new Metric(
+ $this->provider->instrumentationLibrary,
+ $this->provider->resource,
+ $this->provider->view->name,
+ $this->provider->view->unit,
+ $this->provider->view->description,
+ $this->provider->stream->collect($this->reader),
+ );
+ }
+
+ public function __destruct()
+ {
+ $this->provider->stream->unregister($this->reader);
+ }
+}
diff --git a/vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamMetricSourceProvider.php b/vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamMetricSourceProvider.php
new file mode 100644
index 000000000..657c3ce62
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamMetricSourceProvider.php
@@ -0,0 +1,98 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Metrics\MetricFactory;
+
+use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;
+use OpenTelemetry\SDK\Metrics\Instrument;
+use OpenTelemetry\SDK\Metrics\MetricMetadataInterface;
+use OpenTelemetry\SDK\Metrics\MetricRegistry\MetricCollectorInterface;
+use OpenTelemetry\SDK\Metrics\MetricSourceInterface;
+use OpenTelemetry\SDK\Metrics\MetricSourceProviderInterface;
+use OpenTelemetry\SDK\Metrics\Stream\MetricStreamInterface;
+use OpenTelemetry\SDK\Metrics\ViewProjection;
+use OpenTelemetry\SDK\Resource\ResourceInfo;
+
+/**
+ * @internal
+ */
+final class StreamMetricSourceProvider implements MetricSourceProviderInterface, MetricMetadataInterface
+{
+ /**
+ * @readonly
+ */
+ public ViewProjection $view;
+ /**
+ * @readonly
+ */
+ public Instrument $instrument;
+ /**
+ * @readonly
+ */
+ public InstrumentationScopeInterface $instrumentationLibrary;
+ /**
+ * @readonly
+ */
+ public ResourceInfo $resource;
+ /**
+ * @readonly
+ */
+ public MetricStreamInterface $stream;
+ /**
+ * @readonly
+ */
+ public MetricCollectorInterface $metricCollector;
+ /**
+ * @readonly
+ */
+ public int $streamId;
+
+ public function __construct(
+ ViewProjection $view,
+ Instrument $instrument,
+ InstrumentationScopeInterface $instrumentationLibrary,
+ ResourceInfo $resource,
+ MetricStreamInterface $stream,
+ MetricCollectorInterface $metricCollector,
+ int $streamId
+ ) {
+ $this->view = $view;
+ $this->instrument = $instrument;
+ $this->instrumentationLibrary = $instrumentationLibrary;
+ $this->resource = $resource;
+ $this->stream = $stream;
+ $this->metricCollector = $metricCollector;
+ $this->streamId = $streamId;
+ }
+
+ public function create($temporality): MetricSourceInterface
+ {
+ return new StreamMetricSource($this, $this->stream->register($temporality));
+ }
+
+ public function instrumentType()
+ {
+ return $this->instrument->type;
+ }
+
+ public function name(): string
+ {
+ return $this->view->name;
+ }
+
+ public function unit(): ?string
+ {
+ return $this->view->unit;
+ }
+
+ public function description(): ?string
+ {
+ return $this->view->description;
+ }
+
+ public function temporality()
+ {
+ return $this->stream->temporality();
+ }
+}