55
66
77use Prometheus \MetricFamilySamples ;
8+ use RuntimeException ;
89
910class APC implements Adapter
1011{
@@ -103,7 +104,13 @@ private function metaKey(array $data)
103104 */
104105 private function valueKey (array $ data )
105106 {
106- return implode (': ' , array (self ::PROMETHEUS_PREFIX , $ data ['type ' ], $ data ['name ' ], json_encode ($ data ['labelValues ' ]), 'value ' ));
107+ return implode (': ' , array (
108+ self ::PROMETHEUS_PREFIX ,
109+ $ data ['type ' ],
110+ $ data ['name ' ],
111+ $ this ->encodeLabelValues ($ data ['labelValues ' ]),
112+ 'value '
113+ ));
107114 }
108115
109116 /**
@@ -112,7 +119,14 @@ private function valueKey(array $data)
112119 */
113120 private function histogramBucketValueKey (array $ data , $ bucket )
114121 {
115- return implode (': ' , array (self ::PROMETHEUS_PREFIX , $ data ['type ' ], $ data ['name ' ], json_encode ($ data ['labelValues ' ]), $ bucket , 'value ' ));
122+ return implode (': ' , array (
123+ self ::PROMETHEUS_PREFIX ,
124+ $ data ['type ' ],
125+ $ data ['name ' ],
126+ $ this ->encodeLabelValues ($ data ['labelValues ' ]),
127+ $ bucket ,
128+ 'value '
129+ ));
116130 }
117131
118132 /**
@@ -148,7 +162,7 @@ private function collectCounters()
148162 $ data ['samples ' ][] = array (
149163 'name ' => $ metaData ['name ' ],
150164 'labelNames ' => array (),
151- 'labelValues ' => json_decode ($ labelValues ),
165+ 'labelValues ' => $ this -> decodeLabelValues ($ labelValues ),
152166 'value ' => $ value ['value ' ]
153167 );
154168 }
@@ -159,8 +173,8 @@ private function collectCounters()
159173 }
160174
161175 /**
162- * @return array
163- */
176+ * @return array
177+ */
164178 private function collectGauges ()
165179 {
166180 $ gauges = array ();
@@ -178,7 +192,7 @@ private function collectGauges()
178192 $ data ['samples ' ][] = array (
179193 'name ' => $ metaData ['name ' ],
180194 'labelNames ' => array (),
181- 'labelValues ' => json_decode ($ labelValues ),
195+ 'labelValues ' => $ this -> decodeLabelValues ($ labelValues ),
182196 'value ' => $ this ->fromInteger ($ value ['value ' ])
183197 );
184198 }
@@ -222,7 +236,7 @@ private function collectHistograms()
222236 sort ($ labels );
223237 foreach ($ labels as $ labelValues ) {
224238 $ acc = 0 ;
225- $ decodedLabelValues = json_decode ($ labelValues );
239+ $ decodedLabelValues = $ this -> decodeLabelValues ($ labelValues );
226240 foreach ($ data ['buckets ' ] as $ bucket ) {
227241 $ bucket = (string ) $ bucket ;
228242 if (!isset ($ histogramBuckets [$ labelValues ][$ bucket ])) {
@@ -289,4 +303,36 @@ private function sortSamples(array &$samples)
289303 return strcmp (implode ("" , $ a ['labelValues ' ]), implode ("" , $ b ['labelValues ' ]));
290304 });
291305 }
306+
307+ /**
308+ * @param array $values
309+ * @return string
310+ * @throws RuntimeException
311+ */
312+ private function encodeLabelValues (array $ values )
313+ {
314+ $ json = json_encode ($ values );
315+ if (false === $ json ) {
316+ throw new RuntimeException (json_last_error_msg ());
317+ }
318+ return base64_encode ($ json );
319+ }
320+
321+ /**
322+ * @param string $values
323+ * @return array
324+ * @throws RuntimeException
325+ */
326+ private function decodeLabelValues ($ values )
327+ {
328+ $ json = base64_decode ($ values , true );
329+ if (false === $ json ) {
330+ throw new RuntimeException ('Cannot base64 decode label values ' );
331+ }
332+ $ decodedValues = json_decode ($ json , true );
333+ if (false === $ decodedValues ) {
334+ throw new RuntimeException (json_last_error_msg ());
335+ }
336+ return $ decodedValues ;
337+ }
292338}
0 commit comments