summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Metrics/Aggregation/LastValueAggregation.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/open-telemetry/sdk/Metrics/Aggregation/LastValueAggregation.php')
-rw-r--r--vendor/open-telemetry/sdk/Metrics/Aggregation/LastValueAggregation.php81
1 files changed, 81 insertions, 0 deletions
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 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Metrics\Aggregation;
+
+use OpenTelemetry\Context\ContextInterface;
+use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
+use OpenTelemetry\SDK\Metrics\AggregationInterface;
+use OpenTelemetry\SDK\Metrics\Data;
+
+/**
+ * @implements AggregationInterface<LastValueSummary>
+ */
+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<LastValueSummary> $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,
+ );
+ }
+}