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 --- .../ExplicitBucketHistogramAggregation.php | 167 +++++++++++ .../Aggregation/ExplicitBucketHistogramSummary.php | 40 +++ .../Metrics/Aggregation/LastValueAggregation.php | 81 ++++++ .../sdk/Metrics/Aggregation/LastValueSummary.php | 22 ++ .../sdk/Metrics/Aggregation/SumAggregation.php | 91 ++++++ .../sdk/Metrics/Aggregation/SumSummary.php | 20 ++ .../sdk/Metrics/AggregationInterface.php | 57 ++++ .../AggregationTemporalitySelectorInterface.php | 21 ++ .../FilteredAttributeProcessor.php | 33 +++ .../IdentityAttributeProcessor.php | 20 ++ .../sdk/Metrics/AttributeProcessorInterface.php | 16 ++ vendor/open-telemetry/sdk/Metrics/Counter.php | 37 +++ .../sdk/Metrics/Data/DataInterface.php | 9 + .../open-telemetry/sdk/Metrics/Data/Exemplar.php | 65 +++++ vendor/open-telemetry/sdk/Metrics/Data/Gauge.php | 22 ++ .../open-telemetry/sdk/Metrics/Data/Histogram.php | 29 ++ .../sdk/Metrics/Data/HistogramDataPoint.php | 76 +++++ vendor/open-telemetry/sdk/Metrics/Data/Metric.php | 46 +++ .../sdk/Metrics/Data/NumberDataPoint.php | 43 +++ vendor/open-telemetry/sdk/Metrics/Data/Sum.php | 34 +++ .../sdk/Metrics/Data/Temporality.php | 20 ++ .../DefaultAggregationProviderInterface.php | 13 + .../Metrics/DefaultAggregationProviderTrait.php | 28 ++ .../sdk/Metrics/Exemplar/BucketEntry.php | 26 ++ .../sdk/Metrics/Exemplar/BucketStorage.php | 92 ++++++ .../Exemplar/ExemplarFilter/AllExemplarFilter.php | 21 ++ .../Exemplar/ExemplarFilter/NoneExemplarFilter.php | 21 ++ .../WithSampledTraceExemplarFilter.php | 22 ++ .../Metrics/Exemplar/ExemplarFilterInterface.php | 20 ++ .../Exemplar/ExemplarReservoirInterface.php | 24 ++ .../sdk/Metrics/Exemplar/FilteredReservoir.php | 36 +++ .../sdk/Metrics/Exemplar/FixedSizeReservoir.php | 38 +++ .../Metrics/Exemplar/HistogramBucketReservoir.php | 40 +++ .../sdk/Metrics/Exemplar/NoopReservoir.php | 21 ++ vendor/open-telemetry/sdk/Metrics/Histogram.php | 37 +++ vendor/open-telemetry/sdk/Metrics/Instrument.php | 36 +++ .../open-telemetry/sdk/Metrics/InstrumentType.php | 25 ++ vendor/open-telemetry/sdk/Metrics/Meter.php | 314 +++++++++++++++++++++ .../sdk/Metrics/MeterInstruments.php | 29 ++ .../open-telemetry/sdk/Metrics/MeterProvider.php | 130 +++++++++ .../sdk/Metrics/MeterProviderBuilder.php | 62 ++++ .../sdk/Metrics/MeterProviderFactory.php | 78 +++++ .../sdk/Metrics/MeterProviderInterface.php | 12 + .../MetricExporter/ConsoleMetricExporter.php | 105 +++++++ .../ConsoleMetricExporterFactory.php | 16 ++ .../Metrics/MetricExporter/InMemoryExporter.php | 78 +++++ .../MetricExporter/InMemoryExporterFactory.php | 16 ++ .../Metrics/MetricExporter/NoopMetricExporter.php | 23 ++ .../MetricExporter/NoopMetricExporterFactory.php | 16 ++ .../sdk/Metrics/MetricExporter/_register.php | 7 + .../sdk/Metrics/MetricExporterFactoryInterface.php | 10 + .../sdk/Metrics/MetricExporterInterface.php | 17 ++ .../sdk/Metrics/MetricFactory/StreamFactory.php | 187 ++++++++++++ .../Metrics/MetricFactory/StreamMetricSource.php | 44 +++ .../MetricFactory/StreamMetricSourceProvider.php | 98 +++++++ .../sdk/Metrics/MetricFactoryInterface.php | 41 +++ .../sdk/Metrics/MetricMetadataInterface.php | 28 ++ .../sdk/Metrics/MetricReader/ExportingReader.php | 156 ++++++++++ .../sdk/Metrics/MetricReaderInterface.php | 14 + .../MultiRegistryRegistration.php | 36 +++ .../MetricRegistration/RegistryRegistration.php | 31 ++ .../sdk/Metrics/MetricRegistrationInterface.php | 13 + .../MetricRegistry/MetricCollectorInterface.php | 13 + .../sdk/Metrics/MetricRegistry/MetricRegistry.php | 184 ++++++++++++ .../MetricRegistry/MetricRegistryInterface.php | 22 ++ .../MetricRegistry/MetricWriterInterface.php | 20 ++ .../sdk/Metrics/MetricRegistry/MultiObserver.php | 37 +++ .../sdk/Metrics/MetricRegistry/NoopObserver.php | 18 ++ .../sdk/Metrics/MetricSourceInterface.php | 24 ++ .../sdk/Metrics/MetricSourceProviderInterface.php | 15 + .../sdk/Metrics/MetricSourceRegistryInterface.php | 10 + .../sdk/Metrics/NoopMeterProvider.php | 26 ++ .../sdk/Metrics/ObservableCallback.php | 58 ++++ .../sdk/Metrics/ObservableCallbackDestructor.php | 32 +++ .../sdk/Metrics/ObservableCounter.php | 15 + .../open-telemetry/sdk/Metrics/ObservableGauge.php | 15 + .../sdk/Metrics/ObservableInstrumentTrait.php | 61 ++++ .../sdk/Metrics/ObservableUpDownCounter.php | 15 + .../sdk/Metrics/PushMetricExporterInterface.php | 12 + .../sdk/Metrics/ReferenceCounterInterface.php | 15 + .../StalenessHandler/DelayedStalenessHandler.php | 71 +++++ .../DelayedStalenessHandlerFactory.php | 64 +++++ .../StalenessHandler/ImmediateStalenessHandler.php | 50 ++++ .../ImmediateStalenessHandlerFactory.php | 16 ++ .../StalenessHandler/NoopStalenessHandler.php | 30 ++ .../NoopStalenessHandlerFactory.php | 18 ++ .../Metrics/StalenessHandlerFactoryInterface.php | 13 + .../sdk/Metrics/StalenessHandlerInterface.php | 12 + .../Metrics/Stream/AsynchronousMetricStream.php | 111 ++++++++ vendor/open-telemetry/sdk/Metrics/Stream/Delta.php | 33 +++ .../sdk/Metrics/Stream/DeltaStorage.php | 110 ++++++++ .../open-telemetry/sdk/Metrics/Stream/Metric.php | 44 +++ .../sdk/Metrics/Stream/MetricAggregator.php | 73 +++++ .../sdk/Metrics/Stream/MetricAggregatorFactory.php | 28 ++ .../Stream/MetricAggregatorFactoryInterface.php | 13 + .../Metrics/Stream/MetricAggregatorInterface.php | 12 + .../Metrics/Stream/MetricCollectorInterface.php | 13 + .../sdk/Metrics/Stream/MetricStreamInterface.php | 58 ++++ .../sdk/Metrics/Stream/SynchronousMetricStream.php | 126 +++++++++ .../Stream/WritableMetricStreamInterface.php | 19 ++ .../open-telemetry/sdk/Metrics/UpDownCounter.php | 37 +++ .../sdk/Metrics/View/CriteriaViewRegistry.php | 40 +++ .../Metrics/View/SelectionCriteria/AllCriteria.php | 33 +++ .../SelectionCriteria/InstrumentNameCriteria.php | 28 ++ .../SelectionCriteria/InstrumentTypeCriteria.php | 29 ++ .../InstrumentationScopeNameCriteria.php | 24 ++ .../InstrumentationScopeSchemaUrlCriteria.php | 24 ++ .../InstrumentationScopeVersionCriteria.php | 24 ++ .../Metrics/View/SelectionCriteriaInterface.php | 13 + .../sdk/Metrics/View/ViewTemplate.php | 77 +++++ .../open-telemetry/sdk/Metrics/ViewProjection.php | 47 +++ .../sdk/Metrics/ViewRegistryInterface.php | 15 + 112 files changed, 4907 insertions(+) create mode 100644 vendor/open-telemetry/sdk/Metrics/Aggregation/ExplicitBucketHistogramAggregation.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Aggregation/ExplicitBucketHistogramSummary.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Aggregation/LastValueAggregation.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Aggregation/LastValueSummary.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Aggregation/SumAggregation.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Aggregation/SumSummary.php create mode 100644 vendor/open-telemetry/sdk/Metrics/AggregationInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/AggregationTemporalitySelectorInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/AttributeProcessor/FilteredAttributeProcessor.php create mode 100644 vendor/open-telemetry/sdk/Metrics/AttributeProcessor/IdentityAttributeProcessor.php create mode 100644 vendor/open-telemetry/sdk/Metrics/AttributeProcessorInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Counter.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Data/DataInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Data/Exemplar.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Data/Gauge.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Data/Histogram.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Data/HistogramDataPoint.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Data/Metric.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Data/NumberDataPoint.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Data/Sum.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Data/Temporality.php create mode 100644 vendor/open-telemetry/sdk/Metrics/DefaultAggregationProviderInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/DefaultAggregationProviderTrait.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Exemplar/BucketEntry.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Exemplar/BucketStorage.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Exemplar/ExemplarFilter/AllExemplarFilter.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Exemplar/ExemplarFilter/NoneExemplarFilter.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Exemplar/ExemplarFilter/WithSampledTraceExemplarFilter.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Exemplar/ExemplarFilterInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Exemplar/ExemplarReservoirInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Exemplar/FilteredReservoir.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Exemplar/FixedSizeReservoir.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Exemplar/HistogramBucketReservoir.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Exemplar/NoopReservoir.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Histogram.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Instrument.php create mode 100644 vendor/open-telemetry/sdk/Metrics/InstrumentType.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Meter.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MeterInstruments.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MeterProvider.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MeterProviderBuilder.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MeterProviderFactory.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MeterProviderInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricExporter/ConsoleMetricExporter.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricExporter/ConsoleMetricExporterFactory.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricExporter/InMemoryExporter.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricExporter/InMemoryExporterFactory.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricExporter/NoopMetricExporter.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricExporter/NoopMetricExporterFactory.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricExporter/_register.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricExporterFactoryInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricExporterInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamFactory.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamMetricSource.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricFactory/StreamMetricSourceProvider.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricFactoryInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricMetadataInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricReader/ExportingReader.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricReaderInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricRegistration/MultiRegistryRegistration.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricRegistration/RegistryRegistration.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricRegistrationInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricRegistry/MetricCollectorInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricRegistry/MetricRegistry.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricRegistry/MetricRegistryInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricRegistry/MetricWriterInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricRegistry/MultiObserver.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricRegistry/NoopObserver.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricSourceInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricSourceProviderInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/MetricSourceRegistryInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/NoopMeterProvider.php create mode 100644 vendor/open-telemetry/sdk/Metrics/ObservableCallback.php create mode 100644 vendor/open-telemetry/sdk/Metrics/ObservableCallbackDestructor.php create mode 100644 vendor/open-telemetry/sdk/Metrics/ObservableCounter.php create mode 100644 vendor/open-telemetry/sdk/Metrics/ObservableGauge.php create mode 100644 vendor/open-telemetry/sdk/Metrics/ObservableInstrumentTrait.php create mode 100644 vendor/open-telemetry/sdk/Metrics/ObservableUpDownCounter.php create mode 100644 vendor/open-telemetry/sdk/Metrics/PushMetricExporterInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/ReferenceCounterInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/StalenessHandler/DelayedStalenessHandler.php create mode 100644 vendor/open-telemetry/sdk/Metrics/StalenessHandler/DelayedStalenessHandlerFactory.php create mode 100644 vendor/open-telemetry/sdk/Metrics/StalenessHandler/ImmediateStalenessHandler.php create mode 100644 vendor/open-telemetry/sdk/Metrics/StalenessHandler/ImmediateStalenessHandlerFactory.php create mode 100644 vendor/open-telemetry/sdk/Metrics/StalenessHandler/NoopStalenessHandler.php create mode 100644 vendor/open-telemetry/sdk/Metrics/StalenessHandler/NoopStalenessHandlerFactory.php create mode 100644 vendor/open-telemetry/sdk/Metrics/StalenessHandlerFactoryInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/StalenessHandlerInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Stream/AsynchronousMetricStream.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Stream/Delta.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Stream/DeltaStorage.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Stream/Metric.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregator.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregatorFactory.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregatorFactoryInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregatorInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Stream/MetricCollectorInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Stream/MetricStreamInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Stream/SynchronousMetricStream.php create mode 100644 vendor/open-telemetry/sdk/Metrics/Stream/WritableMetricStreamInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/UpDownCounter.php create mode 100644 vendor/open-telemetry/sdk/Metrics/View/CriteriaViewRegistry.php create mode 100644 vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/AllCriteria.php create mode 100644 vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentNameCriteria.php create mode 100644 vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentTypeCriteria.php create mode 100644 vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentationScopeNameCriteria.php create mode 100644 vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentationScopeSchemaUrlCriteria.php create mode 100644 vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentationScopeVersionCriteria.php create mode 100644 vendor/open-telemetry/sdk/Metrics/View/SelectionCriteriaInterface.php create mode 100644 vendor/open-telemetry/sdk/Metrics/View/ViewTemplate.php create mode 100644 vendor/open-telemetry/sdk/Metrics/ViewProjection.php create mode 100644 vendor/open-telemetry/sdk/Metrics/ViewRegistryInterface.php (limited to 'vendor/open-telemetry/sdk/Metrics') diff --git a/vendor/open-telemetry/sdk/Metrics/Aggregation/ExplicitBucketHistogramAggregation.php b/vendor/open-telemetry/sdk/Metrics/Aggregation/ExplicitBucketHistogramAggregation.php new file mode 100644 index 000000000..d68ecd830 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Aggregation/ExplicitBucketHistogramAggregation.php @@ -0,0 +1,167 @@ + + */ +final class ExplicitBucketHistogramAggregation implements AggregationInterface +{ + /** + * @var list + * @readonly + */ + public array $boundaries; + + /** + * @param list $boundaries strictly ascending histogram bucket boundaries + */ + public function __construct(array $boundaries) + { + $this->boundaries = $boundaries; + } + + public function initialize(): ExplicitBucketHistogramSummary + { + return new ExplicitBucketHistogramSummary( + 0, + 0, + +INF, + -INF, + array_fill(0, count($this->boundaries) + 1, 0), + ); + } + + /** + * @param ExplicitBucketHistogramSummary $summary + */ + public function record($summary, $value, AttributesInterface $attributes, ContextInterface $context, int $timestamp): void + { + $boundariesCount = count($this->boundaries); + for ($i = 0; $i < $boundariesCount && $this->boundaries[$i] < $value; $i++) { + } + $summary->count++; + $summary->sum += $value; + $summary->min = self::min($value, $summary->min); + $summary->max = self::max($value, $summary->max); + $summary->buckets[$i]++; + } + + /** + * @param ExplicitBucketHistogramSummary $left + * @param ExplicitBucketHistogramSummary $right + */ + public function merge($left, $right): ExplicitBucketHistogramSummary + { + $count = $left->count + $right->count; + $sum = $left->sum + $right->sum; + $min = self::min($left->min, $right->min); + $max = self::max($left->max, $right->max); + $buckets = $right->buckets; + foreach ($left->buckets as $i => $bucketCount) { + $buckets[$i] += $bucketCount; + } + + return new ExplicitBucketHistogramSummary( + $count, + $sum, + $min, + $max, + $buckets, + ); + } + + /** + * @param ExplicitBucketHistogramSummary $left + * @param ExplicitBucketHistogramSummary $right + */ + public function diff($left, $right): ExplicitBucketHistogramSummary + { + $count = -$left->count + $right->count; + $sum = -$left->sum + $right->sum; + $min = $left->min > $right->min ? $right->min : NAN; + $max = $left->max < $right->max ? $right->max : NAN; + $buckets = $right->buckets; + foreach ($left->buckets as $i => $bucketCount) { + $buckets[$i] -= $bucketCount; + } + + return new ExplicitBucketHistogramSummary( + $count, + $sum, + $min, + $max, + $buckets, + ); + } + + /** + * @param array $summaries + */ + public function toData( + array $attributes, + array $summaries, + array $exemplars, + int $startTimestamp, + int $timestamp, + $temporality + ): Data\Histogram { + $dataPoints = []; + foreach ($attributes as $key => $dataPointAttributes) { + if ($summaries[$key]->count === 0) { + continue; + } + + $dataPoints[] = new Data\HistogramDataPoint( + $summaries[$key]->count, + $summaries[$key]->sum, + $summaries[$key]->min, + $summaries[$key]->max, + $summaries[$key]->buckets, + $this->boundaries, + $dataPointAttributes, + $startTimestamp, + $timestamp, + $exemplars[$key] ?? [], + ); + } + + return new Data\Histogram( + $dataPoints, + $temporality, + ); + } + + /** + * @param float|int $left + * @param float|int $right + * @return float|int + */ + private static function min($left, $right) + { + /** @noinspection PhpConditionAlreadyCheckedInspection */ + return $left <= $right ? $left : ($right <= $left ? $right : NAN); + } + + /** + * @param float|int $left + * @param float|int $right + * @return float|int + */ + private static function max($left, $right) + { + /** @noinspection PhpConditionAlreadyCheckedInspection */ + return $left >= $right ? $left : ($right >= $left ? $right : NAN); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Aggregation/ExplicitBucketHistogramSummary.php b/vendor/open-telemetry/sdk/Metrics/Aggregation/ExplicitBucketHistogramSummary.php new file mode 100644 index 000000000..1878a34a0 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Aggregation/ExplicitBucketHistogramSummary.php @@ -0,0 +1,40 @@ +count = $count; + $this->sum = $sum; + $this->min = $min; + $this->max = $max; + $this->buckets = $buckets; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Aggregation/LastValueAggregation.php b/vendor/open-telemetry/sdk/Metrics/Aggregation/LastValueAggregation.php new file mode 100644 index 000000000..aff04e315 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Aggregation/LastValueAggregation.php @@ -0,0 +1,81 @@ + + */ +final class LastValueAggregation implements AggregationInterface +{ + public function initialize(): LastValueSummary + { + return new LastValueSummary(null, 0); + } + + /** + * @param LastValueSummary $summary + */ + public function record($summary, $value, AttributesInterface $attributes, ContextInterface $context, int $timestamp): void + { + if ($summary->value === null || $timestamp >= $summary->timestamp) { + $summary->value = $value; + $summary->timestamp = $timestamp; + } + } + + /** + * @param LastValueSummary $left + * @param LastValueSummary $right + */ + public function merge($left, $right): LastValueSummary + { + return $right->timestamp >= $left->timestamp ? $right : $left; + } + + /** + * @param LastValueSummary $left + * @param LastValueSummary $right + */ + public function diff($left, $right): LastValueSummary + { + return $right->timestamp >= $left->timestamp ? $right : $left; + } + + /** + * @param array $summaries + */ + public function toData( + array $attributes, + array $summaries, + array $exemplars, + int $startTimestamp, + int $timestamp, + $temporality + ): Data\Gauge { + $dataPoints = []; + foreach ($attributes as $key => $dataPointAttributes) { + if ($summaries[$key]->value === null) { + continue; + } + + $dataPoints[] = new Data\NumberDataPoint( + $summaries[$key]->value, + $dataPointAttributes, + $startTimestamp, + $timestamp, + $exemplars[$key] ?? [], + ); + } + + return new Data\Gauge( + $dataPoints, + ); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Aggregation/LastValueSummary.php b/vendor/open-telemetry/sdk/Metrics/Aggregation/LastValueSummary.php new file mode 100644 index 000000000..6cdb5ac9f --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Aggregation/LastValueSummary.php @@ -0,0 +1,22 @@ +value = $value; + $this->timestamp = $timestamp; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Aggregation/SumAggregation.php b/vendor/open-telemetry/sdk/Metrics/Aggregation/SumAggregation.php new file mode 100644 index 000000000..dc317ce73 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Aggregation/SumAggregation.php @@ -0,0 +1,91 @@ + + */ +final class SumAggregation implements AggregationInterface +{ + private bool $monotonic; + + public function __construct(bool $monotonic = false) + { + $this->monotonic = $monotonic; + } + + public function initialize(): SumSummary + { + return new SumSummary(0); + } + + /** + * @param SumSummary $summary + */ + public function record($summary, $value, AttributesInterface $attributes, ContextInterface $context, int $timestamp): void + { + $summary->value += $value; + } + + /** + * @param SumSummary $left + * @param SumSummary $right + */ + public function merge($left, $right): SumSummary + { + $sum = $left->value + $right->value; + + return new SumSummary( + $sum, + ); + } + + /** + * @param SumSummary $left + * @param SumSummary $right + */ + public function diff($left, $right): SumSummary + { + $sum = -$left->value + $right->value; + + return new SumSummary( + $sum, + ); + } + + /** + * @param array $summaries + */ + public function toData( + array $attributes, + array $summaries, + array $exemplars, + int $startTimestamp, + int $timestamp, + $temporality + ): Data\Sum { + $dataPoints = []; + foreach ($attributes as $key => $dataPointAttributes) { + $dataPoints[] = new Data\NumberDataPoint( + $summaries[$key]->value, + $dataPointAttributes, + $startTimestamp, + $timestamp, + $exemplars[$key] ?? [], + ); + } + + return new Data\Sum( + $dataPoints, + $temporality, + $this->monotonic, + ); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Aggregation/SumSummary.php b/vendor/open-telemetry/sdk/Metrics/Aggregation/SumSummary.php new file mode 100644 index 000000000..9b257193c --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Aggregation/SumSummary.php @@ -0,0 +1,20 @@ +value = $value; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/AggregationInterface.php b/vendor/open-telemetry/sdk/Metrics/AggregationInterface.php new file mode 100644 index 000000000..0a85207e0 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/AggregationInterface.php @@ -0,0 +1,57 @@ + $attributes + * @psalm-param array $summaries + * @param array> $exemplars + * @param string|Temporality $temporality + */ + public function toData( + array $attributes, + array $summaries, + array $exemplars, + int $startTimestamp, + int $timestamp, + $temporality + ): DataInterface; +} diff --git a/vendor/open-telemetry/sdk/Metrics/AggregationTemporalitySelectorInterface.php b/vendor/open-telemetry/sdk/Metrics/AggregationTemporalitySelectorInterface.php new file mode 100644 index 000000000..f046d033d --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/AggregationTemporalitySelectorInterface.php @@ -0,0 +1,21 @@ +attributeKeys = $attributeKeys; + } + + public function process(AttributesInterface $attributes, ContextInterface $context): AttributesInterface + { + $filtered = []; + foreach ($this->attributeKeys as $key) { + $filtered[$key] = $attributes->get($key); + } + + return new Attributes($filtered, 0); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/AttributeProcessor/IdentityAttributeProcessor.php b/vendor/open-telemetry/sdk/Metrics/AttributeProcessor/IdentityAttributeProcessor.php new file mode 100644 index 000000000..f261563ea --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/AttributeProcessor/IdentityAttributeProcessor.php @@ -0,0 +1,20 @@ +writer = $writer; + $this->instrument = $instrument; + $this->referenceCounter = $referenceCounter; + + $this->referenceCounter->acquire(); + } + + public function __destruct() + { + $this->referenceCounter->release(); + } + + public function add($amount, iterable $attributes = [], $context = null): void + { + $this->writer->record($this->instrument, $amount, $attributes, $context); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Data/DataInterface.php b/vendor/open-telemetry/sdk/Metrics/Data/DataInterface.php new file mode 100644 index 000000000..7aa0c0e20 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Data/DataInterface.php @@ -0,0 +1,9 @@ +index = $index; + $this->value = $value; + $this->timestamp = $timestamp; + $this->attributes = $attributes; + $this->traceId = $traceId; + $this->spanId = $spanId; + } + + /** + * @param iterable $exemplars + * @return array> + */ + public static function groupByIndex(iterable $exemplars): array + { + $grouped = []; + foreach ($exemplars as $exemplar) { + $grouped[$exemplar->index][] = $exemplar; + } + + return $grouped; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Data/Gauge.php b/vendor/open-telemetry/sdk/Metrics/Data/Gauge.php new file mode 100644 index 000000000..00eb50939 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Data/Gauge.php @@ -0,0 +1,22 @@ + + * @readonly + */ + public iterable $dataPoints; + /** + * @param iterable $dataPoints + */ + public function __construct(iterable $dataPoints) + { + $this->dataPoints = $dataPoints; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Data/Histogram.php b/vendor/open-telemetry/sdk/Metrics/Data/Histogram.php new file mode 100644 index 000000000..782698026 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Data/Histogram.php @@ -0,0 +1,29 @@ + + * @readonly + */ + public iterable $dataPoints; + /** + * @var string|Temporality + * @readonly + */ + public $temporality; + /** + * @param iterable $dataPoints + * @param string|Temporality $temporality + */ + public function __construct(iterable $dataPoints, $temporality) + { + $this->dataPoints = $dataPoints; + $this->temporality = $temporality; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Data/HistogramDataPoint.php b/vendor/open-telemetry/sdk/Metrics/Data/HistogramDataPoint.php new file mode 100644 index 000000000..4c9df07b4 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Data/HistogramDataPoint.php @@ -0,0 +1,76 @@ + + * @readonly + */ + public array $explicitBounds; + /** + * @readonly + */ + public AttributesInterface $attributes; + /** + * @readonly + */ + public int $startTimestamp; + /** + * @readonly + */ + public int $timestamp; + /** + * @readonly + */ + public iterable $exemplars = []; + /** + * @param float|int $sum + * @param float|int $min + * @param float|int $max + * @param int[] $bucketCounts + * @param list $explicitBounds + */ + public function __construct(int $count, $sum, $min, $max, array $bucketCounts, array $explicitBounds, AttributesInterface $attributes, int $startTimestamp, int $timestamp, iterable $exemplars = []) + { + $this->count = $count; + $this->sum = $sum; + $this->min = $min; + $this->max = $max; + $this->bucketCounts = $bucketCounts; + $this->explicitBounds = $explicitBounds; + $this->attributes = $attributes; + $this->startTimestamp = $startTimestamp; + $this->timestamp = $timestamp; + $this->exemplars = $exemplars; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Data/Metric.php b/vendor/open-telemetry/sdk/Metrics/Data/Metric.php new file mode 100644 index 000000000..41fcb52dd --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Data/Metric.php @@ -0,0 +1,46 @@ +instrumentationScope = $instrumentationScope; + $this->resource = $resource; + $this->name = $name; + $this->description = $description; + $this->unit = $unit; + $this->data = $data; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Data/NumberDataPoint.php b/vendor/open-telemetry/sdk/Metrics/Data/NumberDataPoint.php new file mode 100644 index 000000000..1d00e783a --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Data/NumberDataPoint.php @@ -0,0 +1,43 @@ +value = $value; + $this->attributes = $attributes; + $this->startTimestamp = $startTimestamp; + $this->timestamp = $timestamp; + $this->exemplars = $exemplars; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Data/Sum.php b/vendor/open-telemetry/sdk/Metrics/Data/Sum.php new file mode 100644 index 000000000..77c4c1021 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Data/Sum.php @@ -0,0 +1,34 @@ + + * @readonly + */ + public iterable $dataPoints; + /** + * @var string|Temporality + * @readonly + */ + public $temporality; + /** + * @readonly + */ + public bool $monotonic; + /** + * @param iterable $dataPoints + * @param string|Temporality $temporality + */ + public function __construct(iterable $dataPoints, $temporality, bool $monotonic) + { + $this->dataPoints = $dataPoints; + $this->temporality = $temporality; + $this->monotonic = $monotonic; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Data/Temporality.php b/vendor/open-telemetry/sdk/Metrics/Data/Temporality.php new file mode 100644 index 000000000..b6642ebd0 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Data/Temporality.php @@ -0,0 +1,20 @@ + */ + 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()); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Exemplar/ExemplarFilter/AllExemplarFilter.php b/vendor/open-telemetry/sdk/Metrics/Exemplar/ExemplarFilter/AllExemplarFilter.php new file mode 100644 index 000000000..b74e738aa --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Exemplar/ExemplarFilter/AllExemplarFilter.php @@ -0,0 +1,21 @@ +getContext()->isSampled(); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Exemplar/ExemplarFilterInterface.php b/vendor/open-telemetry/sdk/Metrics/Exemplar/ExemplarFilterInterface.php new file mode 100644 index 000000000..1d5dec7b8 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Exemplar/ExemplarFilterInterface.php @@ -0,0 +1,20 @@ + $dataPointAttributes + * @return array + */ + public function collect(array $dataPointAttributes): array; +} diff --git a/vendor/open-telemetry/sdk/Metrics/Exemplar/FilteredReservoir.php b/vendor/open-telemetry/sdk/Metrics/Exemplar/FilteredReservoir.php new file mode 100644 index 000000000..0e4f24357 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Exemplar/FilteredReservoir.php @@ -0,0 +1,36 @@ +reservoir = $reservoir; + $this->filter = $filter; + } + + public function offer($index, $value, AttributesInterface $attributes, ContextInterface $context, int $timestamp): void + { + if ($this->filter->accepts($value, $attributes, $context, $timestamp)) { + $this->reservoir->offer($index, $value, $attributes, $context, $timestamp); + } + } + + public function collect(array $dataPointAttributes): array + { + return $this->reservoir->collect($dataPointAttributes); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Exemplar/FixedSizeReservoir.php b/vendor/open-telemetry/sdk/Metrics/Exemplar/FixedSizeReservoir.php new file mode 100644 index 000000000..479292a4c --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Exemplar/FixedSizeReservoir.php @@ -0,0 +1,38 @@ +storage = new BucketStorage($size); + $this->size = $size; + } + + public function offer($index, $value, AttributesInterface $attributes, ContextInterface $context, int $timestamp): void + { + $bucket = random_int(0, $this->measurements); + $this->measurements++; + if ($bucket < $this->size) { + $this->storage->store($bucket, $index, $value, $attributes, $context, $timestamp); + } + } + + public function collect(array $dataPointAttributes): array + { + $this->measurements = 0; + + return $this->storage->collect($dataPointAttributes); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Exemplar/HistogramBucketReservoir.php b/vendor/open-telemetry/sdk/Metrics/Exemplar/HistogramBucketReservoir.php new file mode 100644 index 000000000..b56a1b2be --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Exemplar/HistogramBucketReservoir.php @@ -0,0 +1,40 @@ + + */ + private array $boundaries; + + /** + * @param list $boundaries + */ + public function __construct(array $boundaries) + { + $this->storage = new BucketStorage(count($boundaries) + 1); + $this->boundaries = $boundaries; + } + + public function offer($index, $value, AttributesInterface $attributes, ContextInterface $context, int $timestamp): void + { + $boundariesCount = count($this->boundaries); + for ($i = 0; $i < $boundariesCount && $this->boundaries[$i] < $value; $i++) { + } + $this->storage->store($i, $index, $value, $attributes, $context, $timestamp); + } + + public function collect(array $dataPointAttributes): array + { + return $this->storage->collect($dataPointAttributes); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Exemplar/NoopReservoir.php b/vendor/open-telemetry/sdk/Metrics/Exemplar/NoopReservoir.php new file mode 100644 index 000000000..010aeff20 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Exemplar/NoopReservoir.php @@ -0,0 +1,21 @@ +writer = $writer; + $this->instrument = $instrument; + $this->referenceCounter = $referenceCounter; + + $this->referenceCounter->acquire(); + } + + public function __destruct() + { + $this->referenceCounter->release(); + } + + public function record($amount, iterable $attributes = [], $context = null): void + { + $this->writer->record($this->instrument, $amount, $attributes, $context); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Instrument.php b/vendor/open-telemetry/sdk/Metrics/Instrument.php new file mode 100644 index 000000000..3543604c0 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Instrument.php @@ -0,0 +1,36 @@ +type = $type; + $this->name = $name; + $this->unit = $unit; + $this->description = $description; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/InstrumentType.php b/vendor/open-telemetry/sdk/Metrics/InstrumentType.php new file mode 100644 index 000000000..ae603b2fe --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/InstrumentType.php @@ -0,0 +1,25 @@ + */ + private iterable $metricRegistries; + private ViewRegistryInterface $viewRegistry; + private ?ExemplarFilterInterface $exemplarFilter; + private MeterInstruments $instruments; + private InstrumentationScopeInterface $instrumentationScope; + + private MetricRegistryInterface $registry; + private MetricWriterInterface $writer; + + private ?string $instrumentationScopeId = null; + + /** + * @param iterable $metricRegistries + */ + public function __construct( + MetricFactoryInterface $metricFactory, + ResourceInfo $resource, + ClockInterface $clock, + StalenessHandlerFactoryInterface $stalenessHandlerFactory, + iterable $metricRegistries, + ViewRegistryInterface $viewRegistry, + ?ExemplarFilterInterface $exemplarFilter, + MeterInstruments $instruments, + InstrumentationScopeInterface $instrumentationScope, + MetricRegistryInterface $registry, + MetricWriterInterface $writer + ) { + $this->metricFactory = $metricFactory; + $this->resource = $resource; + $this->clock = $clock; + $this->stalenessHandlerFactory = $stalenessHandlerFactory; + $this->metricRegistries = $metricRegistries; + $this->viewRegistry = $viewRegistry; + $this->exemplarFilter = $exemplarFilter; + $this->instruments = $instruments; + $this->instrumentationScope = $instrumentationScope; + $this->registry = $registry; + $this->writer = $writer; + } + + public function createCounter(string $name, ?string $unit = null, ?string $description = null): CounterInterface + { + [$instrument, $referenceCounter] = $this->createSynchronousWriter( + InstrumentType::COUNTER, + $name, + $unit, + $description, + ); + + return new Counter($this->writer, $instrument, $referenceCounter); + } + + public function createObservableCounter(string $name, ?string $unit = null, ?string $description = null, callable ...$callbacks): ObservableCounterInterface + { + [$instrument, $referenceCounter, $destructors] = $this->createAsynchronousObserver( + InstrumentType::ASYNCHRONOUS_COUNTER, + $name, + $unit, + $description, + ); + + foreach ($callbacks as $callback) { + $this->writer->registerCallback(closure($callback), $instrument); + $referenceCounter->acquire(true); + } + + return new ObservableCounter($this->writer, $instrument, $referenceCounter, $destructors); + } + + public function createHistogram(string $name, ?string $unit = null, ?string $description = null): HistogramInterface + { + [$instrument, $referenceCounter] = $this->createSynchronousWriter( + InstrumentType::HISTOGRAM, + $name, + $unit, + $description, + ); + + return new Histogram($this->writer, $instrument, $referenceCounter); + } + + public function createObservableGauge(string $name, ?string $unit = null, ?string $description = null, callable ...$callbacks): ObservableGaugeInterface + { + [$instrument, $referenceCounter, $destructors] = $this->createAsynchronousObserver( + InstrumentType::ASYNCHRONOUS_GAUGE, + $name, + $unit, + $description, + ); + + foreach ($callbacks as $callback) { + $this->writer->registerCallback(closure($callback), $instrument); + $referenceCounter->acquire(true); + } + + return new ObservableGauge($this->writer, $instrument, $referenceCounter, $destructors); + } + + public function createUpDownCounter(string $name, ?string $unit = null, ?string $description = null): UpDownCounterInterface + { + [$instrument, $referenceCounter] = $this->createSynchronousWriter( + InstrumentType::UP_DOWN_COUNTER, + $name, + $unit, + $description, + ); + + return new UpDownCounter($this->writer, $instrument, $referenceCounter); + } + + public function createObservableUpDownCounter(string $name, ?string $unit = null, ?string $description = null, callable ...$callbacks): ObservableUpDownCounterInterface + { + [$instrument, $referenceCounter, $destructors] = $this->createAsynchronousObserver( + InstrumentType::ASYNCHRONOUS_UP_DOWN_COUNTER, + $name, + $unit, + $description, + ); + + foreach ($callbacks as $callback) { + $this->writer->registerCallback(closure($callback), $instrument); + $referenceCounter->acquire(true); + } + + return new ObservableUpDownCounter($this->writer, $instrument, $referenceCounter, $destructors); + } + + /** + * @param string|InstrumentType $instrumentType + * @return array{Instrument, ReferenceCounterInterface} + */ + private function createSynchronousWriter($instrumentType, string $name, ?string $unit, ?string $description): array + { + $instrument = new Instrument($instrumentType, $name, $unit, $description); + + $instrumentationScopeId = $this->instrumentationScopeId($this->instrumentationScope); + $instrumentId = $this->instrumentId($instrument); + + $instruments = $this->instruments; + if ($writer = $instruments->writers[$instrumentationScopeId][$instrumentId] ?? null) { + return $writer; + } + + $stalenessHandler = $this->stalenessHandlerFactory->create(); + $instruments->startTimestamp ??= $this->clock->now(); + $streamIds = $this->metricFactory->createSynchronousWriter( + $this->registry, + $this->resource, + $this->instrumentationScope, + $instrument, + $instruments->startTimestamp, + $this->viewRegistrationRequests($instrument, $stalenessHandler), + $this->exemplarFilter, + ); + + $registry = $this->registry; + $stalenessHandler->onStale(static function () use ($instruments, $instrumentationScopeId, $instrumentId, $registry, $streamIds): void { + unset($instruments->writers[$instrumentationScopeId][$instrumentId]); + if (!$instruments->writers[$instrumentationScopeId]) { + unset($instruments->writers[$instrumentationScopeId]); + } + foreach ($streamIds as $streamId) { + $registry->unregisterStream($streamId); + } + + $instruments->startTimestamp = null; + }); + + return $instruments->writers[$instrumentationScopeId][$instrumentId] = [ + $instrument, + $stalenessHandler, + ]; + } + + /** + * @param string|InstrumentType $instrumentType + * @return array{Instrument, ReferenceCounterInterface, ArrayAccess} + */ + private function createAsynchronousObserver($instrumentType, string $name, ?string $unit, ?string $description): array + { + $instrument = new Instrument($instrumentType, $name, $unit, $description); + + $instrumentationScopeId = $this->instrumentationScopeId($this->instrumentationScope); + $instrumentId = $this->instrumentId($instrument); + + $instruments = $this->instruments; + /** @phan-suppress-next-line PhanDeprecatedProperty */ + $instruments->staleObservers = []; + if ($observer = $instruments->observers[$instrumentationScopeId][$instrumentId] ?? null) { + return $observer; + } + + $stalenessHandler = $this->stalenessHandlerFactory->create(); + $instruments->startTimestamp ??= $this->clock->now(); + $streamIds = $this->metricFactory->createAsynchronousObserver( + $this->registry, + $this->resource, + $this->instrumentationScope, + $instrument, + $instruments->startTimestamp, + $this->viewRegistrationRequests($instrument, $stalenessHandler), + ); + + $registry = $this->registry; + $stalenessHandler->onStale(static function () use ($instruments, $instrumentationScopeId, $instrumentId, $registry, $streamIds): void { + if (PHP_VERSION_ID < 80000) { + /** @phan-suppress-next-line PhanDeprecatedProperty */ + $instruments->staleObservers[] = $instruments->observers[$instrumentationScopeId][$instrumentId][2]; + } + + unset($instruments->observers[$instrumentationScopeId][$instrumentId]); + if (!$instruments->observers[$instrumentationScopeId]) { + unset($instruments->observers[$instrumentationScopeId]); + } + foreach ($streamIds as $streamId) { + $registry->unregisterStream($streamId); + } + + $instruments->startTimestamp = null; + }); + + /** @var ArrayAccess $destructors */ + $destructors = WeakMap::create(); + + return $instruments->observers[$instrumentationScopeId][$instrumentId] = [ + $instrument, + $stalenessHandler, + $destructors, + ]; + } + + /** + * @return iterable + */ + private function viewRegistrationRequests(Instrument $instrument, StalenessHandlerInterface $stalenessHandler): iterable + { + $views = $this->viewRegistry->find($instrument, $this->instrumentationScope) ?? [ + new ViewProjection( + $instrument->name, + $instrument->unit, + $instrument->description, + null, + null, + ), + ]; + + $compositeRegistration = new MultiRegistryRegistration($this->metricRegistries, $stalenessHandler); + foreach ($views as $view) { + if ($view->aggregation !== null) { + yield [$view, $compositeRegistration]; + } else { + foreach ($this->metricRegistries as $metricRegistry) { + yield [ + new ViewProjection( + $view->name, + $view->unit, + $view->description, + $view->attributeKeys, + $metricRegistry->defaultAggregation($instrument->type), + ), + new RegistryRegistration($metricRegistry, $stalenessHandler), + ]; + } + } + } + } + + private function instrumentationScopeId(InstrumentationScopeInterface $instrumentationScope): string + { + return $this->instrumentationScopeId ??= serialize($instrumentationScope); + } + + private function instrumentId(Instrument $instrument): string + { + return serialize($instrument); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/MeterInstruments.php b/vendor/open-telemetry/sdk/Metrics/MeterInstruments.php new file mode 100644 index 000000000..c331cb608 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MeterInstruments.php @@ -0,0 +1,29 @@ +}>> + */ + public array $observers = []; + /** + * @var array> + */ + public array $writers = []; + + /** + * @var list> + * @deprecated + */ + public array $staleObservers = []; +} diff --git a/vendor/open-telemetry/sdk/Metrics/MeterProvider.php b/vendor/open-telemetry/sdk/Metrics/MeterProvider.php new file mode 100644 index 000000000..36c17cf81 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MeterProvider.php @@ -0,0 +1,130 @@ + $metricReaders + */ + public function __construct( + ?ContextStorageInterface $contextStorage, + ResourceInfo $resource, + ClockInterface $clock, + AttributesFactoryInterface $attributesFactory, + InstrumentationScopeFactoryInterface $instrumentationScopeFactory, + iterable $metricReaders, + ViewRegistryInterface $viewRegistry, + ?ExemplarFilterInterface $exemplarFilter, + StalenessHandlerFactoryInterface $stalenessHandlerFactory, + MetricFactoryInterface $metricFactory = null + ) { + $this->metricFactory = $metricFactory ?? new StreamFactory(); + $this->resource = $resource; + $this->clock = $clock; + $this->instrumentationScopeFactory = $instrumentationScopeFactory; + $this->metricReaders = $metricReaders; + $this->viewRegistry = $viewRegistry; + $this->exemplarFilter = $exemplarFilter; + $this->stalenessHandlerFactory = $stalenessHandlerFactory; + $this->instruments = new MeterInstruments(); + + $registry = new MetricRegistry($contextStorage, $attributesFactory, $clock); + $this->registry = $registry; + $this->writer = $registry; + } + + public function getMeter( + string $name, + ?string $version = null, + ?string $schemaUrl = null, + iterable $attributes = [] + ): MeterInterface { + if ($this->closed || Sdk::isDisabled()) { //@todo create meter provider from factory, and move Sdk::isDisabled() there + return new NoopMeter(); + } + + return new Meter( + $this->metricFactory, + $this->resource, + $this->clock, + $this->stalenessHandlerFactory, + $this->metricReaders, + $this->viewRegistry, + $this->exemplarFilter, + $this->instruments, + $this->instrumentationScopeFactory->create($name, $version, $schemaUrl, $attributes), + $this->registry, + $this->writer, + ); + } + + public function shutdown(): bool + { + if ($this->closed) { + return false; + } + + $this->closed = true; + + $success = true; + foreach ($this->metricReaders as $metricReader) { + if (!$metricReader->shutdown()) { + $success = false; + } + } + + return $success; + } + + public function forceFlush(): bool + { + if ($this->closed) { + return false; + } + + $success = true; + foreach ($this->metricReaders as $metricReader) { + if (!$metricReader->forceFlush()) { + $success = false; + } + } + + return $success; + } + + public static function builder(): MeterProviderBuilder + { + return new MeterProviderBuilder(); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/MeterProviderBuilder.php b/vendor/open-telemetry/sdk/Metrics/MeterProviderBuilder.php new file mode 100644 index 000000000..17f0be895 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MeterProviderBuilder.php @@ -0,0 +1,62 @@ + + private array $metricReaders = []; + private ?ResourceInfo $resource = null; + private ?ExemplarFilterInterface $exemplarFilter = null; + + public function setResource(ResourceInfo $resource): self + { + $this->resource = $resource; + + return $this; + } + + public function setExemplarFilter(ExemplarFilterInterface $exemplarFilter): self + { + $this->exemplarFilter = $exemplarFilter; + + return $this; + } + + public function addReader(MetricReaderInterface $reader): self + { + $this->metricReaders[] = $reader; + + return $this; + } + + /** + * @psalm-suppress PossiblyInvalidArgument + */ + public function build(): MeterProviderInterface + { + return new MeterProvider( + null, + $this->resource ?? ResourceInfoFactory::emptyResource(), + ClockFactory::getDefault(), + Attributes::factory(), + new InstrumentationScopeFactory(Attributes::factory()), + $this->metricReaders, + new CriteriaViewRegistry(), + $this->exemplarFilter ?? new WithSampledTraceExemplarFilter(), + new NoopStalenessHandlerFactory(), + ); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/MeterProviderFactory.php b/vendor/open-telemetry/sdk/Metrics/MeterProviderFactory.php new file mode 100644 index 000000000..5f7f9988d --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MeterProviderFactory.php @@ -0,0 +1,78 @@ +create(); + } catch (\Throwable $t) { + self::logWarning(sprintf('Unable to create %s meter provider: %s', $exporterName, $t->getMessage())); + $exporter = new NoopMetricExporter(); + } + + // @todo "The exporter MUST be paired with a periodic exporting MetricReader" + $reader = new ExportingReader($exporter); + $resource = ResourceInfoFactory::defaultResource(); + $exemplarFilter = $this->createExemplarFilter(Configuration::getEnum(Variables::OTEL_METRICS_EXEMPLAR_FILTER)); + + return MeterProvider::builder() + ->setResource($resource) + ->addReader($reader) + ->setExemplarFilter($exemplarFilter) + ->build(); + } + + private function createExemplarFilter(string $name): ExemplarFilterInterface + { + switch ($name) { + case KnownValues::VALUE_WITH_SAMPLED_TRACE: + return new WithSampledTraceExemplarFilter(); + case KnownValues::VALUE_ALL: + return new AllExemplarFilter(); + case KnownValues::VALUE_NONE: + return new NoneExemplarFilter(); + default: + self::logWarning('Unknown exemplar filter: ' . $name); + + return new NoneExemplarFilter(); + } + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/MeterProviderInterface.php b/vendor/open-telemetry/sdk/Metrics/MeterProviderInterface.php new file mode 100644 index 000000000..fcb951106 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MeterProviderInterface.php @@ -0,0 +1,12 @@ +temporality = $temporality; + } + /** + * @inheritDoc + */ + public function temporality(MetricMetadataInterface $metric) + { + return $this->temporality ?? $metric->temporality(); + } + + /** + * @inheritDoc + */ + public function export(iterable $batch): bool + { + $resource = null; + $scope = null; + foreach ($batch as $metric) { + /** @var Metric $metric */ + if (!$resource) { + $resource = $this->convertResource($metric->resource); + } + if (!$scope) { + $scope = $this->convertInstrumentationScope($metric->instrumentationScope); + $scope['metrics'] = []; + } + $scope['metrics'][] = $this->convertMetric($metric); + } + $output = [ + 'resource' => $resource, + 'scope' => $scope, + ]; + echo json_encode($output, JSON_PRETTY_PRINT) . PHP_EOL; + + return true; + } + + public function shutdown(): bool + { + return true; + } + + public function forceFlush(): bool + { + return true; + } + + private function convertMetric(Metric $metric): array + { + return [ + 'name' => $metric->name, + 'description' => $metric->description, + 'unit' => $metric->unit, + 'data' => $metric->data, + ]; + } + + private function convertResource(ResourceInfo $resource): array + { + return [ + 'attributes' => $resource->getAttributes()->toArray(), + 'dropped_attributes_count' => $resource->getAttributes()->getDroppedAttributesCount(), + ]; + } + private function convertInstrumentationScope(InstrumentationScopeInterface $scope): array + { + return [ + 'name' => $scope->getName(), + 'version' => $scope->getVersion(), + 'attributes' => $scope->getAttributes()->toArray(), + 'dropped_attributes_count' => $scope->getAttributes()->getDroppedAttributesCount(), + 'schema_url' => $scope->getSchemaUrl(), + ]; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/MetricExporter/ConsoleMetricExporterFactory.php b/vendor/open-telemetry/sdk/Metrics/MetricExporter/ConsoleMetricExporterFactory.php new file mode 100644 index 000000000..19088738d --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MetricExporter/ConsoleMetricExporterFactory.php @@ -0,0 +1,16 @@ + + */ + private array $metrics = []; + /** + * @var string|Temporality|null + */ + private $temporality; + + private bool $closed = false; + + /** + * @param string|Temporality|null $temporality + */ + public function __construct($temporality = null) + { + $this->temporality = $temporality; + } + + public function temporality(MetricMetadataInterface $metric) + { + return $this->temporality ?? $metric->temporality(); + } + + /** + * @return list + */ + public function collect(bool $reset = false): array + { + $metrics = $this->metrics; + if ($reset) { + $this->metrics = []; + } + + return $metrics; + } + + public function export(iterable $batch): bool + { + if ($this->closed) { + return false; + } + + /** @psalm-suppress InvalidPropertyAssignmentValue */ + array_push($this->metrics, ...$batch); + + return true; + } + + public function shutdown(): bool + { + if ($this->closed) { + return false; + } + + $this->closed = true; + + return true; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/MetricExporter/InMemoryExporterFactory.php b/vendor/open-telemetry/sdk/Metrics/MetricExporter/InMemoryExporterFactory.php new file mode 100644 index 000000000..c72c7b169 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MetricExporter/InMemoryExporterFactory.php @@ -0,0 +1,16 @@ + $batch + */ + public function export(iterable $batch): bool; + + public function shutdown(): bool; +} 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 @@ +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 @@ +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 @@ +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(); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/MetricFactoryInterface.php b/vendor/open-telemetry/sdk/Metrics/MetricFactoryInterface.php new file mode 100644 index 000000000..a1e228eef --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MetricFactoryInterface.php @@ -0,0 +1,41 @@ + $views + */ + public function createAsynchronousObserver( + MetricRegistryInterface $registry, + ResourceInfo $resource, + InstrumentationScopeInterface $instrumentationScope, + Instrument $instrument, + int $timestamp, + iterable $views + ): array; + + /** + * @param iterable $views + */ + public function createSynchronousWriter( + MetricRegistryInterface $registry, + ResourceInfo $resource, + InstrumentationScopeInterface $instrumentationScope, + Instrument $instrument, + int $timestamp, + iterable $views, + ?ExemplarFilterInterface $exemplarFilter = null + ): array; +} diff --git a/vendor/open-telemetry/sdk/Metrics/MetricMetadataInterface.php b/vendor/open-telemetry/sdk/Metrics/MetricMetadataInterface.php new file mode 100644 index 000000000..aa1a02d60 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MetricMetadataInterface.php @@ -0,0 +1,28 @@ + */ + private array $sources = []; + + /** @var array */ + private array $registries = []; + /** @var array> */ + private array $streamIds = []; + + private bool $closed = false; + + public function __construct(MetricExporterInterface $exporter) + { + $this->exporter = $exporter; + } + + public function defaultAggregation($instrumentType): ?AggregationInterface + { + if ($this->exporter instanceof DefaultAggregationProviderInterface) { + return $this->exporter->defaultAggregation($instrumentType); + } + + return $this->_defaultAggregation($instrumentType); + } + + public function add(MetricSourceProviderInterface $provider, MetricMetadataInterface $metadata, StalenessHandlerInterface $stalenessHandler): void + { + if ($this->closed) { + return; + } + if (!$this->exporter instanceof AggregationTemporalitySelectorInterface) { + return; + } + if (!$temporality = $this->exporter->temporality($metadata)) { + return; + } + + $source = $provider->create($temporality); + $sourceId = spl_object_id($source); + + $this->sources[$sourceId] = $source; + $stalenessHandler->onStale(function () use ($sourceId): void { + unset($this->sources[$sourceId]); + }); + + if (!$provider instanceof StreamMetricSourceProvider) { + return; + } + + $streamId = $provider->streamId; + $registry = $provider->metricCollector; + $registryId = spl_object_id($registry); + + $this->registries[$registryId] = $registry; + $this->streamIds[$registryId][$streamId] ??= 0; + $this->streamIds[$registryId][$streamId]++; + + $stalenessHandler->onStale(function () use ($streamId, $registryId): void { + if (!--$this->streamIds[$registryId][$streamId]) { + unset($this->streamIds[$registryId][$streamId]); + if (!$this->streamIds[$registryId]) { + unset( + $this->registries[$registryId], + $this->streamIds[$registryId], + ); + } + } + }); + } + + private function doCollect(): bool + { + foreach ($this->registries as $registryId => $registry) { + $streamIds = $this->streamIds[$registryId] ?? []; + $registry->collectAndPush(array_keys($streamIds)); + } + + $metrics = []; + foreach ($this->sources as $source) { + $metrics[] = $source->collect(); + } + + if ($metrics === []) { + return true; + } + + return $this->exporter->export($metrics); + } + + public function collect(): bool + { + if ($this->closed) { + return false; + } + + return $this->doCollect(); + } + + public function shutdown(): bool + { + if ($this->closed) { + return false; + } + + $this->closed = true; + + $collect = $this->doCollect(); + $shutdown = $this->exporter->shutdown(); + + $this->sources = []; + + return $collect && $shutdown; + } + + public function forceFlush(): bool + { + if ($this->closed) { + return false; + } + if ($this->exporter instanceof PushMetricExporterInterface) { + $collect = $this->doCollect(); + $forceFlush = $this->exporter->forceFlush(); + + return $collect && $forceFlush; + } + + return true; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/MetricReaderInterface.php b/vendor/open-telemetry/sdk/Metrics/MetricReaderInterface.php new file mode 100644 index 000000000..f5900eef5 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MetricReaderInterface.php @@ -0,0 +1,14 @@ + $registries + */ + public function __construct(iterable $registries, StalenessHandlerInterface $stalenessHandler) + { + $this->registries = $registries; + $this->stalenessHandler = $stalenessHandler; + } + + public function register(MetricSourceProviderInterface $provider, MetricMetadataInterface $metadata): void + { + foreach ($this->registries as $registry) { + $registry->add($provider, $metadata, $this->stalenessHandler); + } + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/MetricRegistration/RegistryRegistration.php b/vendor/open-telemetry/sdk/Metrics/MetricRegistration/RegistryRegistration.php new file mode 100644 index 000000000..3c1108902 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MetricRegistration/RegistryRegistration.php @@ -0,0 +1,31 @@ +registry = $registry; + $this->stalenessHandler = $stalenessHandler; + } + + public function register(MetricSourceProviderInterface $provider, MetricMetadataInterface $metadata): void + { + $this->registry->add($provider, $metadata, $this->stalenessHandler); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/MetricRegistrationInterface.php b/vendor/open-telemetry/sdk/Metrics/MetricRegistrationInterface.php new file mode 100644 index 000000000..b0cc2484e --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MetricRegistrationInterface.php @@ -0,0 +1,13 @@ + */ + private array $streams = []; + /** @var array */ + private array $synchronousAggregators = []; + /** @var array */ + private array $asynchronousAggregatorFactories = []; + + /** @var array> */ + private array $instrumentToStreams = []; + /** @var array */ + private array $streamToInstrument = []; + /** @var array> */ + private array $instrumentToCallbacks = []; + /** @var array */ + private array $asynchronousCallbacks = []; + /** @var array> */ + private array $asynchronousCallbackArguments = []; + + public function __construct( + ?ContextStorageInterface $contextStorage, + AttributesFactoryInterface $attributesFactory, + ClockInterface $clock + ) { + $this->contextStorage = $contextStorage; + $this->attributesFactory = $attributesFactory; + $this->clock = $clock; + } + + public function registerSynchronousStream(Instrument $instrument, MetricStreamInterface $stream, MetricAggregatorInterface $aggregator): int + { + $this->streams[] = $stream; + $streamId = array_key_last($this->streams); + $instrumentId = spl_object_id($instrument); + + $this->synchronousAggregators[$streamId] = $aggregator; + $this->instrumentToStreams[$instrumentId][$streamId] = $streamId; + $this->streamToInstrument[$streamId] = $instrumentId; + + return $streamId; + } + + public function registerAsynchronousStream(Instrument $instrument, MetricStreamInterface $stream, MetricAggregatorFactoryInterface $aggregatorFactory): int + { + $this->streams[] = $stream; + $streamId = array_key_last($this->streams); + $instrumentId = spl_object_id($instrument); + + $this->asynchronousAggregatorFactories[$streamId] = $aggregatorFactory; + $this->instrumentToStreams[$instrumentId][$streamId] = $streamId; + $this->streamToInstrument[$streamId] = $instrumentId; + + return $streamId; + } + + public function unregisterStream(int $streamId): void + { + $instrumentId = $this->streamToInstrument[$streamId]; + unset( + $this->streams[$streamId], + $this->synchronousAggregators[$streamId], + $this->asynchronousAggregatorFactories[$streamId], + $this->instrumentToStreams[$instrumentId][$streamId], + $this->streamToInstrument[$streamId], + ); + if (!$this->instrumentToStreams[$instrumentId]) { + unset($this->instrumentToStreams[$instrumentId]); + } + } + + public function record(Instrument $instrument, $value, iterable $attributes = [], $context = null): void + { + $context = Context::resolve($context, $this->contextStorage); + $attributes = $this->attributesFactory->builder($attributes)->build(); + $timestamp = $this->clock->now(); + $instrumentId = spl_object_id($instrument); + foreach ($this->instrumentToStreams[$instrumentId] ?? [] as $streamId) { + if ($aggregator = $this->synchronousAggregators[$streamId] ?? null) { + $aggregator->record($value, $attributes, $context, $timestamp); + } + } + } + + public function registerCallback(Closure $callback, Instrument $instrument, Instrument ...$instruments): int + { + $callbackId = array_key_last($this->asynchronousCallbacks) + 1; + $this->asynchronousCallbacks[$callbackId] = $callback; + + $instrumentId = spl_object_id($instrument); + $this->asynchronousCallbackArguments[$callbackId] = [$instrumentId]; + $this->instrumentToCallbacks[$instrumentId][$callbackId] = $callbackId; + foreach ($instruments as $instrument) { + $instrumentId = spl_object_id($instrument); + $this->asynchronousCallbackArguments[$callbackId][] = $instrumentId; + $this->instrumentToCallbacks[$instrumentId][$callbackId] = $callbackId; + } + + return $callbackId; + } + + public function unregisterCallback(int $callbackId): void + { + $instrumentIds = $this->asynchronousCallbackArguments[$callbackId]; + unset( + $this->asynchronousCallbacks[$callbackId], + $this->asynchronousCallbackArguments[$callbackId], + ); + foreach ($instrumentIds as $instrumentId) { + unset($this->instrumentToCallbacks[$instrumentId][$callbackId]); + if (!$this->instrumentToCallbacks[$instrumentId]) { + unset($this->instrumentToCallbacks[$instrumentId]); + } + } + } + + public function collectAndPush(iterable $streamIds): void + { + $timestamp = $this->clock->now(); + $aggregators = []; + $observers = []; + $callbackIds = []; + foreach ($streamIds as $streamId) { + if (!$aggregator = $this->synchronousAggregators[$streamId] ?? null) { + $aggregator = $this->asynchronousAggregatorFactories[$streamId]->create(); + + $instrumentId = $this->streamToInstrument[$streamId]; + $observers[$instrumentId] ??= new MultiObserver($this->attributesFactory, $timestamp); + $observers[$instrumentId]->writers[] = $aggregator; + foreach ($this->instrumentToCallbacks[$instrumentId] ?? [] as $callbackId) { + $callbackIds[$callbackId] = $callbackId; + } + } + + $aggregators[$streamId] = $aggregator; + } + + $noopObserver = new NoopObserver(); + $callbacks = []; + foreach ($callbackIds as $callbackId) { + $args = []; + foreach ($this->asynchronousCallbackArguments[$callbackId] as $instrumentId) { + $args[] = $observers[$instrumentId] ?? $noopObserver; + } + $callback = $this->asynchronousCallbacks[$callbackId]; + $callbacks[] = static fn () => $callback(...$args); + } + foreach ($callbacks as $callback) { + $callback(); + } + + $timestamp = $this->clock->now(); + foreach ($aggregators as $streamId => $aggregator) { + if ($stream = $this->streams[$streamId] ?? null) { + $stream->push($aggregator->collect($timestamp)); + } + } + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/MetricRegistry/MetricRegistryInterface.php b/vendor/open-telemetry/sdk/Metrics/MetricRegistry/MetricRegistryInterface.php new file mode 100644 index 000000000..e86731138 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MetricRegistry/MetricRegistryInterface.php @@ -0,0 +1,22 @@ + */ + public array $writers = []; + + public function __construct(AttributesFactoryInterface $attributesFactory, int $timestamp) + { + $this->attributesFactory = $attributesFactory; + $this->timestamp = $timestamp; + } + + public function observe($amount, iterable $attributes = []): void + { + $context = Context::getRoot(); + $attributes = $this->attributesFactory->builder($attributes)->build(); + foreach ($this->writers as $writer) { + $writer->record($amount, $attributes, $context, $this->timestamp); + } + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/MetricRegistry/NoopObserver.php b/vendor/open-telemetry/sdk/Metrics/MetricRegistry/NoopObserver.php new file mode 100644 index 000000000..efbd94dac --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MetricRegistry/NoopObserver.php @@ -0,0 +1,18 @@ +writer = $writer; + $this->referenceCounter = $referenceCounter; + $this->callbackId = $callbackId; + $this->callbackDestructor = $callbackDestructor; + $this->target = $target; + } + + public function detach(): void + { + if ($this->callbackId === null) { + return; + } + + $this->writer->unregisterCallback($this->callbackId); + $this->referenceCounter->release(); + if ($this->callbackDestructor !== null) { + unset($this->callbackDestructor->callbackIds[$this->callbackId]); + } + + $this->callbackId = null; + } + + public function __destruct() + { + if ($this->callbackDestructor !== null) { + return; + } + if ($this->callbackId === null) { + return; + } + + $this->referenceCounter->acquire(true); + $this->referenceCounter->release(); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/ObservableCallbackDestructor.php b/vendor/open-telemetry/sdk/Metrics/ObservableCallbackDestructor.php new file mode 100644 index 000000000..0dfea3907 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/ObservableCallbackDestructor.php @@ -0,0 +1,32 @@ + */ + public array $callbackIds = []; + private MetricWriterInterface $writer; + private ReferenceCounterInterface $referenceCounter; + + public function __construct(MetricWriterInterface $writer, ReferenceCounterInterface $referenceCounter) + { + $this->writer = $writer; + $this->referenceCounter = $referenceCounter; + } + + public function __destruct() + { + foreach ($this->callbackIds as $callbackId) { + $this->writer->unregisterCallback($callbackId); + $this->referenceCounter->release(); + } + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/ObservableCounter.php b/vendor/open-telemetry/sdk/Metrics/ObservableCounter.php new file mode 100644 index 000000000..99ae43eee --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/ObservableCounter.php @@ -0,0 +1,15 @@ +writer = $writer; + $this->instrument = $instrument; + $this->referenceCounter = $referenceCounter; + $this->destructors = $destructors; + + $this->referenceCounter->acquire(); + } + + public function __destruct() + { + $this->referenceCounter->release(); + } + + /** + * @param callable(ObserverInterface): void $callback + */ + public function observe(callable $callback): ObservableCallbackInterface + { + $callback = weaken(closure($callback), $target); + + $callbackId = $this->writer->registerCallback($callback, $this->instrument); + $this->referenceCounter->acquire(); + + $destructor = null; + if ($target) { + $destructor = $this->destructors[$target] ??= new ObservableCallbackDestructor($this->writer, $this->referenceCounter); + $destructor->callbackIds[$callbackId] = $callbackId; + } + + return new ObservableCallback($this->writer, $this->referenceCounter, $callbackId, $destructor, $target); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/ObservableUpDownCounter.php b/vendor/open-telemetry/sdk/Metrics/ObservableUpDownCounter.php new file mode 100644 index 000000000..8d21be734 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/ObservableUpDownCounter.php @@ -0,0 +1,15 @@ +stale = $stale; + $this->freshen = $freshen; + } + + public function acquire(bool $persistent = false): void + { + if ($this->count === 0) { + ($this->freshen)($this); + } + + $this->count++; + + if ($persistent) { + $this->onStale = null; + } + } + + public function release(): void + { + if (--$this->count || $this->onStale === null) { + return; + } + + ($this->stale)($this); + } + + public function onStale(Closure $callback): void + { + if ($this->onStale === null) { + return; + } + + $this->onStale[] = $callback; + } + + public function triggerStale(): void + { + assert($this->onStale !== null); + + $callbacks = $this->onStale; + $this->onStale = []; + foreach ($callbacks as $callback) { + $callback(); + } + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/StalenessHandler/DelayedStalenessHandlerFactory.php b/vendor/open-telemetry/sdk/Metrics/StalenessHandler/DelayedStalenessHandlerFactory.php new file mode 100644 index 000000000..0d719c74f --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/StalenessHandler/DelayedStalenessHandlerFactory.php @@ -0,0 +1,64 @@ +&Traversable */ + private $staleHandlers; + + /** + * @param float $delay delay in seconds + */ + public function __construct(ClockInterface $clock, float $delay) + { + $this->clock = $clock; + $this->nanoDelay = (int) ($delay * 1e9); + + $this->stale = function (DelayedStalenessHandler $handler): void { + $this->staleHandlers[$handler] = $this->clock->now(); + }; + $this->freshen = function (DelayedStalenessHandler $handler): void { + unset($this->staleHandlers[$handler]); + }; + + $this->staleHandlers = WeakMap::create(); + } + + public function create(): StalenessHandlerInterface + { + $this->triggerStaleHandlers(); + + return new DelayedStalenessHandler($this->stale, $this->freshen); + } + + private function triggerStaleHandlers(): void + { + $expired = $this->clock->now() - $this->nanoDelay; + foreach ($this->staleHandlers as $handler => $timestamp) { + if ($timestamp > $expired) { + break; + } + + /** @var DelayedStalenessHandler $handler */ + unset($this->staleHandlers[$handler]); + $handler->triggerStale(); + } + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/StalenessHandler/ImmediateStalenessHandler.php b/vendor/open-telemetry/sdk/Metrics/StalenessHandler/ImmediateStalenessHandler.php new file mode 100644 index 000000000..a5b32d5c4 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/StalenessHandler/ImmediateStalenessHandler.php @@ -0,0 +1,50 @@ +count++; + + if ($persistent) { + $this->onStale = null; + } + } + + public function release(): void + { + if (--$this->count !== 0 || !$this->onStale) { + return; + } + + $callbacks = $this->onStale; + $this->onStale = []; + foreach ($callbacks as $callback) { + $callback(); + } + } + + public function onStale(Closure $callback): void + { + if ($this->onStale === null) { + return; + } + + $this->onStale[] = $callback; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/StalenessHandler/ImmediateStalenessHandlerFactory.php b/vendor/open-telemetry/sdk/Metrics/StalenessHandler/ImmediateStalenessHandlerFactory.php new file mode 100644 index 000000000..899615dea --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/StalenessHandler/ImmediateStalenessHandlerFactory.php @@ -0,0 +1,16 @@ + */ + private array $lastReads = []; + + public function __construct(AggregationInterface $aggregation, int $startTimestamp) + { + $this->aggregation = $aggregation; + $this->startTimestamp = $startTimestamp; + $this->metric = new Metric([], [], $startTimestamp); + } + + public function temporality() + { + return Temporality::CUMULATIVE; + } + + public function timestamp(): int + { + return $this->metric->timestamp; + } + + public function push(Metric $metric): void + { + $this->metric = $metric; + } + + public function register($temporality): int + { + if ($temporality === Temporality::CUMULATIVE) { + return -1; + } + + if (($reader = array_search(null, $this->lastReads, true)) === false) { + $reader = count($this->lastReads); + } + + $this->lastReads[$reader] = $this->metric; + + return $reader; + } + + public function unregister(int $reader): void + { + if (!isset($this->lastReads[$reader])) { + return; + } + + $this->lastReads[$reader] = null; + } + + public function collect(int $reader): DataInterface + { + $metric = $this->metric; + + if (($lastRead = $this->lastReads[$reader] ?? null) === null) { + $temporality = Temporality::CUMULATIVE; + $startTimestamp = $this->startTimestamp; + } else { + $temporality = Temporality::DELTA; + $startTimestamp = $lastRead->timestamp; + + $this->lastReads[$reader] = $metric; + $metric = $this->diff($lastRead, $metric); + } + + return $this->aggregation->toData( + $metric->attributes, + $metric->summaries, + Exemplar::groupByIndex($metric->exemplars), + $startTimestamp, + $metric->timestamp, + $temporality, + ); + } + + private function diff(Metric $lastRead, Metric $metric): Metric + { + $diff = clone $metric; + foreach ($metric->summaries as $k => $summary) { + if (!isset($lastRead->summaries[$k])) { + continue; + } + + $diff->summaries[$k] = $this->aggregation->diff($lastRead->summaries[$k], $summary); + } + + return $diff; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Stream/Delta.php b/vendor/open-telemetry/sdk/Metrics/Stream/Delta.php new file mode 100644 index 000000000..a4ff56d71 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Stream/Delta.php @@ -0,0 +1,33 @@ +metric = $metric; + $this->readers = $readers; + $this->prev = $prev; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Stream/DeltaStorage.php b/vendor/open-telemetry/sdk/Metrics/Stream/DeltaStorage.php new file mode 100644 index 000000000..b46a28d65 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Stream/DeltaStorage.php @@ -0,0 +1,110 @@ +aggregation = $aggregation; + $this->head = new Delta(new Metric([], [], 0), 0); + + /** @phan-suppress-next-line PhanTypeObjectUnsetDeclaredProperty */ + unset($this->head->metric); + } + + /** + * @psalm-suppress UndefinedDocblockClass + * @phan-suppress PhanUndeclaredTypeParameter + * @param int|GMP $readers + */ + public function add(Metric $metric, $readers): void + { + /** @phpstan-ignore-next-line */ + if ($readers == 0) { + return; + } + + if (($this->head->prev->readers ?? null) != $readers) { + $this->head->prev = new Delta($metric, $readers, $this->head->prev); + } else { + assert($this->head->prev !== null); + $this->mergeInto($this->head->prev->metric, $metric); + } + } + + public function collect(int $reader, bool $retain = false): ?Metric + { + $n = null; + for ($d = $this->head; $d->prev; $d = $d->prev) { + if (($d->prev->readers >> $reader & 1) != 0) { + if ($n !== null) { + assert($n->prev !== null); + $n->prev->readers ^= $d->prev->readers; + $this->mergeInto($d->prev->metric, $n->prev->metric); + $this->tryUnlink($n); + + if ($n->prev === $d->prev) { + continue; + } + } + + $n = $d; + } + } + + $metric = $n->prev->metric ?? null; + + if (!$retain && $n) { + assert($n->prev !== null); + $n->prev->readers ^= ($n->prev->readers & 1 | 1) << $reader; + $this->tryUnlink($n); + } + + return $metric; + } + + private function tryUnlink(Delta $n): void + { + assert($n->prev !== null); + /** @phpstan-ignore-next-line */ + if ($n->prev->readers == 0) { + $n->prev = $n->prev->prev; + + return; + } + + for ($c = $n->prev->prev; + $c && ($n->prev->readers & $c->readers) == 0; + $c = $c->prev) { + } + + if ($c && $n->prev->readers === $c->readers) { + $this->mergeInto($c->metric, $n->prev->metric); + $n->prev = $n->prev->prev; + } + } + + private function mergeInto(Metric $into, Metric $metric): void + { + foreach ($metric->summaries as $k => $summary) { + $into->attributes[$k] ??= $metric->attributes[$k]; + $into->summaries[$k] = isset($into->summaries[$k]) + ? $this->aggregation->merge($into->summaries[$k], $summary) + : $summary; + } + $into->exemplars += $metric->exemplars; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Stream/Metric.php b/vendor/open-telemetry/sdk/Metrics/Stream/Metric.php new file mode 100644 index 000000000..6b1db9eef --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Stream/Metric.php @@ -0,0 +1,44 @@ + + */ + public array $attributes; + /** + * @var array + */ + public array $summaries; + public int $timestamp; + /** + * @var array + */ + public array $exemplars; + + /** + * @param array $attributes + * @param array $summaries + * @param array $exemplars + */ + public function __construct(array $attributes, array $summaries, int $timestamp, array $exemplars = []) + { + $this->attributes = $attributes; + $this->summaries = $summaries; + $this->timestamp = $timestamp; + $this->exemplars = $exemplars; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregator.php b/vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregator.php new file mode 100644 index 000000000..b1328eb07 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregator.php @@ -0,0 +1,73 @@ + */ + private array $attributes = []; + private array $summaries = []; + + public function __construct( + ?AttributeProcessorInterface $attributeProcessor, + AggregationInterface $aggregation, + ?ExemplarReservoirInterface $exemplarReservoir = null + ) { + $this->attributeProcessor = $attributeProcessor; + $this->aggregation = $aggregation; + $this->exemplarReservoir = $exemplarReservoir; + } + + /** + * @param float|int $value + */ + public function record($value, AttributesInterface $attributes, ContextInterface $context, int $timestamp): void + { + $filteredAttributes = $this->attributeProcessor !== null + ? $this->attributeProcessor->process($attributes, $context) + : $attributes; + $raw = $filteredAttributes->toArray(); + $index = $raw !== [] ? serialize($raw) : 0; + $this->attributes[$index] ??= $filteredAttributes; + $this->aggregation->record( + $this->summaries[$index] ??= $this->aggregation->initialize(), + $value, + $attributes, + $context, + $timestamp, + ); + + if ($this->exemplarReservoir !== null) { + $this->exemplarReservoir->offer($index, $value, $attributes, $context, $timestamp); + } + } + + public function collect(int $timestamp): Metric + { + $exemplars = $this->exemplarReservoir + ? $this->exemplarReservoir->collect($this->attributes) + : []; + $metric = new Metric($this->attributes, $this->summaries, $timestamp, $exemplars); + + $this->attributes = []; + $this->summaries = []; + + return $metric; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregatorFactory.php b/vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregatorFactory.php new file mode 100644 index 000000000..5866a72b7 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregatorFactory.php @@ -0,0 +1,28 @@ +attributeProcessor = $attributeProcessor; + $this->aggregation = $aggregation; + } + + public function create(): MetricAggregatorInterface + { + return new MetricAggregator($this->attributeProcessor, $this->aggregation); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregatorFactoryInterface.php b/vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregatorFactoryInterface.php new file mode 100644 index 000000000..356f682f2 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Stream/MetricAggregatorFactoryInterface.php @@ -0,0 +1,13 @@ +aggregation = $aggregation; + $this->timestamp = $startTimestamp; + $this->delta = new DeltaStorage($aggregation); + } + + public function temporality() + { + return Temporality::DELTA; + } + + public function timestamp(): int + { + return $this->timestamp; + } + + public function push(Metric $metric): void + { + [$this->timestamp, $metric->timestamp] = [$metric->timestamp, $this->timestamp]; + $this->delta->add($metric, $this->readers); + } + + public function register($temporality): int + { + $reader = 0; + for ($r = $this->readers; ($r & 1) != 0; $r >>= 1, $reader++) { + } + + if ($reader === (PHP_INT_SIZE << 3) - 1 && is_int($this->readers)) { + if (!extension_loaded('gmp')) { + trigger_error(sprintf('GMP extension required to register over %d readers', (PHP_INT_SIZE << 3) - 1), E_USER_WARNING); + $reader = PHP_INT_SIZE << 3; + } else { + assert(is_int($this->cumulative)); + $this->readers = gmp_init($this->readers); + $this->cumulative = gmp_init($this->cumulative); + } + } + + $readerMask = ($this->readers & 1 | 1) << $reader; + $this->readers ^= $readerMask; + if ($temporality === Temporality::CUMULATIVE) { + $this->cumulative ^= $readerMask; + } + + return $reader; + } + + public function unregister(int $reader): void + { + $readerMask = ($this->readers & 1 | 1) << $reader; + if (($this->readers & $readerMask) == 0) { + return; + } + + $this->delta->collect($reader); + + $this->readers ^= $readerMask; + if (($this->cumulative & $readerMask) != 0) { + $this->cumulative ^= $readerMask; + } + } + + public function collect(int $reader): DataInterface + { + $cumulative = ($this->cumulative >> $reader & 1) != 0; + $metric = $this->delta->collect($reader, $cumulative) ?? new Metric([], [], $this->timestamp); + + $temporality = $cumulative + ? Temporality::CUMULATIVE + : Temporality::DELTA; + + return $this->aggregation->toData( + $metric->attributes, + $metric->summaries, + Exemplar::groupByIndex($metric->exemplars), + $metric->timestamp, + $this->timestamp, + $temporality, + ); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/Stream/WritableMetricStreamInterface.php b/vendor/open-telemetry/sdk/Metrics/Stream/WritableMetricStreamInterface.php new file mode 100644 index 000000000..9fd425a44 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/Stream/WritableMetricStreamInterface.php @@ -0,0 +1,19 @@ +writer = $writer; + $this->instrument = $instrument; + $this->referenceCounter = $referenceCounter; + + $this->referenceCounter->acquire(); + } + + public function __destruct() + { + $this->referenceCounter->release(); + } + + public function add($amount, iterable $attributes = [], $context = null): void + { + $this->writer->record($this->instrument, $amount, $attributes, $context); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/View/CriteriaViewRegistry.php b/vendor/open-telemetry/sdk/Metrics/View/CriteriaViewRegistry.php new file mode 100644 index 000000000..f387abf9c --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/View/CriteriaViewRegistry.php @@ -0,0 +1,40 @@ + */ + private array $criteria = []; + /** @var list */ + private array $views = []; + + public function register(SelectionCriteriaInterface $criteria, ViewTemplate $view): void + { + $this->criteria[] = $criteria; + $this->views[] = $view; + } + + public function find(Instrument $instrument, InstrumentationScopeInterface $instrumentationScope): ?iterable + { + $views = $this->generateViews($instrument, $instrumentationScope); + + return $views->valid() ? $views : null; + } + + private function generateViews(Instrument $instrument, InstrumentationScopeInterface $instrumentationScope): Generator + { + foreach ($this->criteria as $i => $criteria) { + if ($criteria->accepts($instrument, $instrumentationScope)) { + yield $this->views[$i]->project($instrument); + } + } + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/AllCriteria.php b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/AllCriteria.php new file mode 100644 index 000000000..438297324 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/AllCriteria.php @@ -0,0 +1,33 @@ + $criteria + */ + public function __construct(iterable $criteria) + { + $this->criteria = $criteria; + } + + public function accepts(Instrument $instrument, InstrumentationScopeInterface $instrumentationScope): bool + { + foreach ($this->criteria as $criterion) { + if (!$criterion->accepts($instrument, $instrumentationScope)) { + return false; + } + } + + return true; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentNameCriteria.php b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentNameCriteria.php new file mode 100644 index 000000000..ed6034755 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentNameCriteria.php @@ -0,0 +1,28 @@ +pattern = sprintf('/^%s$/', strtr(preg_quote($name, '/'), ['\\?' => '.', '\\*' => '.*'])); + } + + public function accepts(Instrument $instrument, InstrumentationScopeInterface $instrumentationScope): bool + { + return (bool) preg_match($this->pattern, $instrument->name); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentTypeCriteria.php b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentTypeCriteria.php new file mode 100644 index 000000000..46a88def0 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentTypeCriteria.php @@ -0,0 +1,29 @@ +instrumentTypes = (array) $instrumentType; + } + + public function accepts(Instrument $instrument, InstrumentationScopeInterface $instrumentationScope): bool + { + return in_array($instrument->type, $this->instrumentTypes, true); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentationScopeNameCriteria.php b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentationScopeNameCriteria.php new file mode 100644 index 000000000..201d1a7b2 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentationScopeNameCriteria.php @@ -0,0 +1,24 @@ +name = $name; + } + + public function accepts(Instrument $instrument, InstrumentationScopeInterface $instrumentationScope): bool + { + return $this->name === $instrumentationScope->getName(); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentationScopeSchemaUrlCriteria.php b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentationScopeSchemaUrlCriteria.php new file mode 100644 index 000000000..a11a1d589 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentationScopeSchemaUrlCriteria.php @@ -0,0 +1,24 @@ +schemaUrl = $schemaUrl; + } + + public function accepts(Instrument $instrument, InstrumentationScopeInterface $instrumentationScope): bool + { + return $this->schemaUrl === $instrumentationScope->getSchemaUrl(); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentationScopeVersionCriteria.php b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentationScopeVersionCriteria.php new file mode 100644 index 000000000..37d180f99 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteria/InstrumentationScopeVersionCriteria.php @@ -0,0 +1,24 @@ +version = $version; + } + + public function accepts(Instrument $instrument, InstrumentationScopeInterface $instrumentationScope): bool + { + return $this->version === $instrumentationScope->getVersion(); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteriaInterface.php b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteriaInterface.php new file mode 100644 index 000000000..8abd6fa69 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/View/SelectionCriteriaInterface.php @@ -0,0 +1,13 @@ + + */ + private ?array $attributeKeys = null; + private ?AggregationInterface $aggregation = null; + + private function __construct() + { + } + + public static function create(): self + { + static $instance; + + return $instance ??= new self(); + } + + public function withName(string $name): self + { + $self = clone $this; + $self->name = $name; + + return $self; + } + + public function withDescription(string $description): self + { + $self = clone $this; + $self->description = $description; + + return $self; + } + + /** + * @param list $attributeKeys + */ + public function withAttributeKeys(array $attributeKeys): self + { + $self = clone $this; + $self->attributeKeys = $attributeKeys; + + return $self; + } + + public function withAggregation(?AggregationInterface $aggregation): self + { + $self = clone $this; + $self->aggregation = $aggregation; + + return $self; + } + + public function project(Instrument $instrument): ViewProjection + { + return new ViewProjection( + $this->name ?? $instrument->name, + $instrument->unit, + $this->description ?? $instrument->description, + $this->attributeKeys, + $this->aggregation, + ); + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/ViewProjection.php b/vendor/open-telemetry/sdk/Metrics/ViewProjection.php new file mode 100644 index 000000000..046bd6bb1 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/ViewProjection.php @@ -0,0 +1,47 @@ +|null + */ + public ?array $attributeKeys; + /** + * @readonly + */ + public ?AggregationInterface $aggregation; + + /** + * @param list|null $attributeKeys + */ + public function __construct( + string $name, + ?string $unit, + ?string $description, + ?array $attributeKeys, + ?AggregationInterface $aggregation + ) { + $this->name = $name; + $this->unit = $unit; + $this->description = $description; + $this->attributeKeys = $attributeKeys; + $this->aggregation = $aggregation; + } +} diff --git a/vendor/open-telemetry/sdk/Metrics/ViewRegistryInterface.php b/vendor/open-telemetry/sdk/Metrics/ViewRegistryInterface.php new file mode 100644 index 000000000..19d8f9ffd --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/ViewRegistryInterface.php @@ -0,0 +1,15 @@ +|null + */ + public function find(Instrument $instrument, InstrumentationScopeInterface $instrumentationScope): ?iterable; +} -- cgit v1.2.3