@@ -18,6 +18,11 @@ class APCng implements Adapter
18
18
19
19
private const MAX_LOOPS = 10 ;
20
20
21
+ /**
22
+ * @var int ttl for all apcu entries reserved for prometheus
23
+ */
24
+ private $ ttl ;
25
+
21
26
/**
22
27
* @var int
23
28
*/
@@ -44,10 +49,11 @@ class APCng implements Adapter
44
49
* APCng constructor.
45
50
*
46
51
* @param string $prometheusPrefix Prefix for APCu keys (defaults to {@see PROMETHEUS_PREFIX}).
52
+ * @param int $ttl ttl for all apcu entries reserved for prometheus, default is 120 days
47
53
*
48
54
* @throws StorageException
49
55
*/
50
- public function __construct (string $ prometheusPrefix = self ::PROMETHEUS_PREFIX , int $ decimalPrecision = 3 )
56
+ public function __construct (string $ prometheusPrefix = self ::PROMETHEUS_PREFIX , int $ decimalPrecision = 3 , int $ ttl = 10368000 )
51
57
{
52
58
if (!extension_loaded ('apcu ' )) {
53
59
throw new StorageException ('APCu extension is not loaded ' );
@@ -61,6 +67,8 @@ public function __construct(string $prometheusPrefix = self::PROMETHEUS_PREFIX,
61
67
$ this ->metaInfoCounterKey = implode (': ' , [ $ this ->prometheusPrefix , 'metainfocounter ' ]);
62
68
$ this ->metaInfoCountedMetricKeyPattern = implode (': ' , [ $ this ->prometheusPrefix , 'metainfocountedmetric_#COUNTER# ' ]);
63
69
70
+ $ this ->ttl = $ ttl ;
71
+
64
72
if ($ decimalPrecision < 0 || $ decimalPrecision > 6 ) {
65
73
throw new UnexpectedValueException (
66
74
sprintf ('Decimal precision %d is not from interval <0;6>. ' , $ decimalPrecision )
@@ -96,7 +104,7 @@ public function updateHistogram(array $data): void
96
104
97
105
if ($ old === false ) {
98
106
// If sum does not exist, initialize it, store the metadata for the new histogram
99
- apcu_add ($ sumKey , 0 , 0 );
107
+ apcu_add ($ sumKey , 0 , $ this -> ttl );
100
108
$ this ->storeMetadata ($ data );
101
109
$ this ->storeLabelKeys ($ data );
102
110
}
@@ -115,7 +123,7 @@ public function updateHistogram(array $data): void
115
123
// Initialize and increment the bucket
116
124
$ bucketKey = $ this ->histogramBucketValueKey ($ data , $ bucketToIncrease );
117
125
if (!apcu_exists ($ bucketKey )) {
118
- apcu_add ($ bucketKey , 0 );
126
+ apcu_add ($ bucketKey , $ this -> ttl );
119
127
}
120
128
apcu_inc ($ bucketKey );
121
129
}
@@ -137,7 +145,7 @@ public function updateSummary(array $data): void
137
145
{
138
146
// store value key; store metadata & labels if new
139
147
$ valueKey = $ this ->valueKey ($ data );
140
- $ new = apcu_add ($ valueKey , $ this ->encodeLabelValues ($ data ['labelValues ' ]), 0 );
148
+ $ new = apcu_add ($ valueKey , $ this ->encodeLabelValues ($ data ['labelValues ' ]), $ this -> ttl );
141
149
if ($ new ) {
142
150
$ this ->storeMetadata ($ data , false );
143
151
$ this ->storeLabelKeys ($ data );
@@ -173,7 +181,7 @@ public function updateGauge(array $data): void
173
181
if ($ data ['command ' ] === Adapter::COMMAND_SET ) {
174
182
$ new = $ this ->convertToIncrementalInteger ($ data ['value ' ]);
175
183
if ($ old === false ) {
176
- apcu_store ($ valueKey , $ new , 0 );
184
+ apcu_store ($ valueKey , $ new , $ this -> ttl );
177
185
$ this ->storeMetadata ($ data );
178
186
$ this ->storeLabelKeys ($ data );
179
187
@@ -198,7 +206,7 @@ public function updateGauge(array $data): void
198
206
}
199
207
200
208
if ($ old === false ) {
201
- apcu_add ($ valueKey , 0 , 0 );
209
+ apcu_add ($ valueKey , 0 , $ this -> ttl );
202
210
$ this ->storeMetadata ($ data );
203
211
$ this ->storeLabelKeys ($ data );
204
212
}
@@ -220,7 +228,7 @@ public function updateCounter(array $data): void
220
228
$ old = apcu_fetch ($ valueKey );
221
229
222
230
if ($ old === false ) {
223
- apcu_add ($ valueKey , 0 , 0 );
231
+ apcu_add ($ valueKey , 0 , $ this -> ttl );
224
232
$ this ->storeMetadata ($ data );
225
233
$ this ->storeLabelKeys ($ data );
226
234
}
@@ -274,7 +282,7 @@ private function addItemToKey(string $key, string $item): void
274
282
$ _item = $ this ->encodeLabelKey ($ item );
275
283
if (!array_key_exists ($ _item , $ arr )) {
276
284
$ arr [$ _item ] = 1 ;
277
- apcu_store ($ key , $ arr , 0 );
285
+ apcu_store ($ key , $ arr , $ this -> ttl );
278
286
}
279
287
}
280
288
@@ -356,7 +364,7 @@ private function scanAndBuildMetainfoCache(): array
356
364
$ arr [$ type ][] = ['key ' => $ metaKey , 'value ' => $ metaInfo ];
357
365
}
358
366
359
- apcu_store ($ this ->metainfoCacheKey , $ arr , 0 );
367
+ apcu_store ($ this ->metainfoCacheKey , $ arr , $ this -> ttl );
360
368
361
369
return $ arr ;
362
370
}
@@ -915,17 +923,17 @@ private function storeMetadata(array $data, bool $encoded = true): void
915
923
$ toStore = json_encode ($ metaData );
916
924
}
917
925
918
- $ stored = apcu_add ($ metaKey , $ toStore , 0 );
926
+ $ stored = apcu_add ($ metaKey , $ toStore , $ this -> ttl );
919
927
920
928
if (!$ stored ) {
921
929
return ;
922
930
}
923
931
924
- apcu_add ($ this ->metaInfoCounterKey , 0 , 0 );
932
+ apcu_add ($ this ->metaInfoCounterKey , 0 , $ this -> ttl );
925
933
$ counter = apcu_inc ($ this ->metaInfoCounterKey );
926
934
927
935
$ newCountedMetricKey = $ this ->metaCounterKey ($ counter );
928
- apcu_store ($ newCountedMetricKey , $ metaKey , 0 );
936
+ apcu_store ($ newCountedMetricKey , $ metaKey , $ this -> ttl );
929
937
}
930
938
931
939
private function metaCounterKey (int $ counter ): string
0 commit comments