-
Notifications
You must be signed in to change notification settings - Fork 187
/
ExportingReader.php
154 lines (124 loc) · 4.82 KB
/
ExportingReader.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
namespace OpenTelemetry\SDK\Metrics\MetricReader;
use function array_keys;
use OpenTelemetry\SDK\Metrics\AggregationInterface;
use OpenTelemetry\SDK\Metrics\AggregationTemporalitySelectorInterface;
use OpenTelemetry\SDK\Metrics\DefaultAggregationProviderInterface;
use OpenTelemetry\SDK\Metrics\DefaultAggregationProviderTrait;
use OpenTelemetry\SDK\Metrics\MetricExporterInterface;
use OpenTelemetry\SDK\Metrics\MetricFactory\StreamMetricSourceProvider;
use OpenTelemetry\SDK\Metrics\MetricMetadataInterface;
use OpenTelemetry\SDK\Metrics\MetricReaderInterface;
use OpenTelemetry\SDK\Metrics\MetricRegistry\MetricCollectorInterface;
use OpenTelemetry\SDK\Metrics\MetricSourceInterface;
use OpenTelemetry\SDK\Metrics\MetricSourceProviderInterface;
use OpenTelemetry\SDK\Metrics\MetricSourceRegistryInterface;
use OpenTelemetry\SDK\Metrics\PushMetricExporterInterface;
use OpenTelemetry\SDK\Metrics\StalenessHandlerInterface;
use function spl_object_id;
final class ExportingReader implements MetricReaderInterface, MetricSourceRegistryInterface, DefaultAggregationProviderInterface
{
use DefaultAggregationProviderTrait { defaultAggregation as private _defaultAggregation; }
/** @var array<int, MetricSourceInterface> */
private array $sources = [];
/** @var array<int, MetricCollectorInterface> */
private array $registries = [];
/** @var array<int, array<int, int>> */
private array $streamIds = [];
private bool $closed = false;
public function __construct(private readonly MetricExporterInterface $exporter)
{
}
public function defaultAggregation($instrumentType, array $advisory = []): ?AggregationInterface
{
if ($this->exporter instanceof DefaultAggregationProviderInterface) {
/** @phan-suppress-next-line PhanParamTooMany @phpstan-ignore-next-line */
return $this->exporter->defaultAggregation($instrumentType, $advisory);
}
return $this->_defaultAggregation($instrumentType, $advisory);
}
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;
}
}