@@ -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 $ ttl = 10368000 , int $ decimalPrecision = 3 )
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
}
@@ -113,7 +121,7 @@ public function updateHistogram(array $data): void
113
121
}
114
122
115
123
// Initialize and increment the bucket
116
- apcu_add ($ this ->histogramBucketValueKey ($ data , $ bucketToIncrease ), 0 );
124
+ apcu_add ($ this ->histogramBucketValueKey ($ data , $ bucketToIncrease ), 0 , $ this -> ttl );
117
125
apcu_inc ($ this ->histogramBucketValueKey ($ data , $ bucketToIncrease ));
118
126
}
119
127
@@ -134,7 +142,7 @@ public function updateSummary(array $data): void
134
142
{
135
143
// store value key; store metadata & labels if new
136
144
$ valueKey = $ this ->valueKey ($ data );
137
- $ new = apcu_add ($ valueKey , $ this ->encodeLabelValues ($ data ['labelValues ' ]), 0 );
145
+ $ new = apcu_add ($ valueKey , $ this ->encodeLabelValues ($ data ['labelValues ' ]), $ this -> ttl );
138
146
if ($ new ) {
139
147
$ this ->storeMetadata ($ data , false );
140
148
$ this ->storeLabelKeys ($ data );
@@ -167,7 +175,7 @@ public function updateGauge(array $data): void
167
175
{
168
176
$ valueKey = $ this ->valueKey ($ data );
169
177
if ($ data ['command ' ] === Adapter::COMMAND_SET ) {
170
- apcu_store ($ valueKey , $ this ->convertToIncrementalInteger ($ data ['value ' ]), 0 );
178
+ apcu_store ($ valueKey , $ this ->convertToIncrementalInteger ($ data ['value ' ]), $ this -> ttl );
171
179
$ this ->storeMetadata ($ data );
172
180
$ this ->storeLabelKeys ($ data );
173
181
@@ -177,7 +185,7 @@ public function updateGauge(array $data): void
177
185
$ old = apcu_fetch ($ valueKey );
178
186
179
187
if ($ old === false ) {
180
- apcu_add ($ valueKey , 0 , 0 );
188
+ apcu_add ($ valueKey , 0 , $ this -> ttl );
181
189
$ this ->storeMetadata ($ data );
182
190
$ this ->storeLabelKeys ($ data );
183
191
}
@@ -199,7 +207,7 @@ public function updateCounter(array $data): void
199
207
$ old = apcu_fetch ($ valueKey );
200
208
201
209
if ($ old === false ) {
202
- apcu_add ($ valueKey , 0 , 0 );
210
+ apcu_add ($ valueKey , 0 , $ this -> ttl );
203
211
$ this ->storeMetadata ($ data );
204
212
$ this ->storeLabelKeys ($ data );
205
213
}
@@ -253,7 +261,7 @@ private function addItemToKey(string $key, string $item): void
253
261
$ _item = $ this ->encodeLabelKey ($ item );
254
262
if (!array_key_exists ($ _item , $ arr )) {
255
263
$ arr [$ _item ] = 1 ;
256
- apcu_store ($ key , $ arr , 0 );
264
+ apcu_store ($ key , $ arr , $ this -> ttl );
257
265
}
258
266
}
259
267
@@ -335,7 +343,7 @@ private function scanAndBuildMetainfoCache(): array
335
343
$ arr [$ type ][] = ['key ' => $ metaKey , 'value ' => $ metaInfo ];
336
344
}
337
345
338
- apcu_store ($ this ->metainfoCacheKey , $ arr , 0 );
346
+ apcu_store ($ this ->metainfoCacheKey , $ arr , $ this -> ttl );
339
347
340
348
return $ arr ;
341
349
}
@@ -892,17 +900,17 @@ private function storeMetadata(array $data, bool $encoded = true): void
892
900
$ toStore = json_encode ($ metaData );
893
901
}
894
902
895
- $ stored = apcu_add ($ metaKey , $ toStore , 0 );
903
+ $ stored = apcu_add ($ metaKey , $ toStore , $ this -> ttl );
896
904
897
905
if (!$ stored ) {
898
906
return ;
899
907
}
900
908
901
- apcu_add ($ this ->metaInfoCounterKey , 0 , 0 );
909
+ apcu_add ($ this ->metaInfoCounterKey , 0 , $ this -> ttl );
902
910
$ counter = apcu_inc ($ this ->metaInfoCounterKey );
903
911
904
912
$ newCountedMetricKey = $ this ->metaCounterKey ($ counter );
905
- apcu_store ($ newCountedMetricKey , $ metaKey , 0 );
913
+ apcu_store ($ newCountedMetricKey , $ metaKey , $ this -> ttl );
906
914
}
907
915
908
916
private function metaCounterKey (int $ counter ): string
0 commit comments