|
6 | 6 |
|
7 | 7 | use OpenTelemetry\API\Metrics\MeterInterface; |
8 | 8 | use OpenTelemetry\API\Metrics\ObserverInterface; |
| 9 | +use const PHP_VERSION_ID; |
9 | 10 |
|
10 | 11 | /** |
11 | 12 | * @internal |
@@ -36,72 +37,66 @@ public static function register(MeterInterface $meter): void |
36 | 37 | '{object}', |
37 | 38 | 'Current number of objects in the root buffer', |
38 | 39 | ); |
| 40 | + $collectorTime = $meter->createObservableCounter( |
| 41 | + 'php.gc.collector_time', |
| 42 | + 's', |
| 43 | + 'Cumulative time spent in the garbage collector', |
| 44 | + ); |
| 45 | + $destructorTime = $meter->createObservableCounter( |
| 46 | + 'php.gc.destructor_time', |
| 47 | + 's', |
| 48 | + 'Cumulative time spent running destructors during GC', |
| 49 | + ); |
| 50 | + $freeTime = $meter->createObservableCounter( |
| 51 | + 'php.gc.free_time', |
| 52 | + 's', |
| 53 | + 'Cumulative time spent freeing memory during GC', |
| 54 | + ); |
| 55 | + $processUptime = $meter->createObservableGauge( |
| 56 | + 'process.uptime', |
| 57 | + 's', |
| 58 | + 'The time the process has been running', |
| 59 | + ); |
39 | 60 |
|
40 | 61 | $meter->batchObserve( |
41 | 62 | static function ( |
42 | 63 | ObserverInterface $runsObs, |
43 | 64 | ObserverInterface $collectedObs, |
44 | 65 | ObserverInterface $thresholdObs, |
45 | 66 | ObserverInterface $rootsObs, |
| 67 | + ObserverInterface $collectorObs, |
| 68 | + ObserverInterface $destructorObs, |
| 69 | + ObserverInterface $freeObs, |
| 70 | + ObserverInterface $uptimeObs, |
46 | 71 | ): void { |
47 | 72 | $status = gc_status(); |
48 | 73 | $runsObs->observe($status['runs']); |
49 | 74 | $collectedObs->observe($status['collected']); |
50 | 75 | $thresholdObs->observe($status['threshold']); |
51 | 76 | $rootsObs->observe($status['roots']); |
| 77 | + |
| 78 | + if (PHP_VERSION_ID < 80300) { // Timing metrics available since PHP 8.3 |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + /** @var array<string, int|float> $status */ |
| 83 | + // @phan-suppress-next-line PhanTypeInvalidDimOffset, PhanTypeMismatchArgument -- fields added in PHP 8.3 |
| 84 | + $collectorObs->observe($status['collector_time']); |
| 85 | + // @phan-suppress-next-line PhanTypeInvalidDimOffset, PhanTypeMismatchArgument -- fields added in PHP 8.3 |
| 86 | + $destructorObs->observe($status['destructor_time']); |
| 87 | + // @phan-suppress-next-line PhanTypeInvalidDimOffset, PhanTypeMismatchArgument -- fields added in PHP 8.3 |
| 88 | + $freeObs->observe($status['free_time']); |
| 89 | + // @phan-suppress-next-line PhanTypeInvalidDimOffset, PhanTypeMismatchArgument -- fields added in PHP 8.3 |
| 90 | + $uptimeObs->observe($status['application_time']); |
52 | 91 | }, |
53 | 92 | $runs, |
54 | 93 | $collected, |
55 | 94 | $threshold, |
56 | 95 | $roots, |
| 96 | + $collectorTime, |
| 97 | + $destructorTime, |
| 98 | + $freeTime, |
| 99 | + $processUptime, |
57 | 100 | ); |
58 | | - |
59 | | - // Timing metrics available since PHP 8.3 |
60 | | - if (PHP_VERSION_ID >= 80300) { |
61 | | - $collectorTime = $meter->createObservableCounter( |
62 | | - 'php.gc.collector_time', |
63 | | - 's', |
64 | | - 'Cumulative time spent in the garbage collector', |
65 | | - ); |
66 | | - $destructorTime = $meter->createObservableCounter( |
67 | | - 'php.gc.destructor_time', |
68 | | - 's', |
69 | | - 'Cumulative time spent running destructors during GC', |
70 | | - ); |
71 | | - $freeTime = $meter->createObservableCounter( |
72 | | - 'php.gc.free_time', |
73 | | - 's', |
74 | | - 'Cumulative time spent freeing memory during GC', |
75 | | - ); |
76 | | - $processUptime = $meter->createObservableGauge( |
77 | | - 'process.uptime', |
78 | | - 's', |
79 | | - 'The time the process has been running', |
80 | | - ); |
81 | | - |
82 | | - $meter->batchObserve( |
83 | | - static function ( |
84 | | - ObserverInterface $collectorObs, |
85 | | - ObserverInterface $destructorObs, |
86 | | - ObserverInterface $freeObs, |
87 | | - ObserverInterface $uptimeObs, |
88 | | - ): void { |
89 | | - /** @var array<string, int|float> $status */ |
90 | | - $status = gc_status(); |
91 | | - // @phan-suppress-next-line PhanTypeInvalidDimOffset, PhanTypeMismatchArgument -- fields added in PHP 8.3 |
92 | | - $collectorObs->observe($status['collector_time']); |
93 | | - // @phan-suppress-next-line PhanTypeInvalidDimOffset, PhanTypeMismatchArgument -- fields added in PHP 8.3 |
94 | | - $destructorObs->observe($status['destructor_time']); |
95 | | - // @phan-suppress-next-line PhanTypeInvalidDimOffset, PhanTypeMismatchArgument -- fields added in PHP 8.3 |
96 | | - $freeObs->observe($status['free_time']); |
97 | | - // @phan-suppress-next-line PhanTypeInvalidDimOffset, PhanTypeMismatchArgument -- fields added in PHP 8.3 |
98 | | - $uptimeObs->observe($status['application_time']); |
99 | | - }, |
100 | | - $collectorTime, |
101 | | - $destructorTime, |
102 | | - $freeTime, |
103 | | - $processUptime, |
104 | | - ); |
105 | | - } |
106 | 101 | } |
107 | 102 | } |
0 commit comments