diff options
author | Andrew Dolgov <[email protected]> | 2023-10-20 17:12:29 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2023-10-20 21:13:39 +0300 |
commit | cdd7ad020e165fe680703b6d3319b908b682fb7a (patch) | |
tree | b51eb09b7b4587e8fbc5624ac8d88d28cfcd0b04 /vendor/open-telemetry/sdk/Metrics/MetricExporter/ConsoleMetricExporter.php | |
parent | 45a9ff0c88cbd33892ff16ab837e9059937d656e (diff) |
jaeger-client -> opentelemetry
Diffstat (limited to 'vendor/open-telemetry/sdk/Metrics/MetricExporter/ConsoleMetricExporter.php')
-rw-r--r-- | vendor/open-telemetry/sdk/Metrics/MetricExporter/ConsoleMetricExporter.php | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/vendor/open-telemetry/sdk/Metrics/MetricExporter/ConsoleMetricExporter.php b/vendor/open-telemetry/sdk/Metrics/MetricExporter/ConsoleMetricExporter.php new file mode 100644 index 000000000..62ea7b535 --- /dev/null +++ b/vendor/open-telemetry/sdk/Metrics/MetricExporter/ConsoleMetricExporter.php @@ -0,0 +1,105 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\SDK\Metrics\MetricExporter; + +use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface; +use OpenTelemetry\SDK\Metrics\AggregationTemporalitySelectorInterface; +use OpenTelemetry\SDK\Metrics\Data\Metric; +use OpenTelemetry\SDK\Metrics\Data\Temporality; +use OpenTelemetry\SDK\Metrics\MetricMetadataInterface; +use OpenTelemetry\SDK\Metrics\PushMetricExporterInterface; +use OpenTelemetry\SDK\Resource\ResourceInfo; + +/** + * Console metrics exporter. + * Note that the output is human-readable JSON, not compatible with OTLP. + */ +class ConsoleMetricExporter implements PushMetricExporterInterface, AggregationTemporalitySelectorInterface +{ + /** + * @var string|Temporality|null + */ + private $temporality; + + /** + * @param string|Temporality|null $temporality + */ + public function __construct($temporality = null) + { + $this->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(), + ]; + } +} |