1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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(),
];
}
}
|