PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / libraries / prometheus / RenderNewRelicFormat.php

RenderNewRelicFormat.php in DecaLog 4.4.0, at includes/libraries/prometheus/RenderNewRelicFormat.php

103 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Prometheus;
6
7 use Decalog\System\Environment;
8 use Decalog\System\Blog;
9
10 class RenderNewRelicFormat implements RendererInterface {
11
12 /**
13 * The array of tag names.
14 *
15 * @since 3.2.0
16 * @var array $tagnames The tag names.
17 */
18 private $tagnames = [];
19
20 /**
21 * The array of tag values.
22 *
23 * @since 3.2.0
24 * @var array $tagvalues The tag values.
25 */
26 private $tagvalues = [];
27
28 /**
29 * @param MetricFamilySamples[] $metrics
30 * @return string
31 */
32 public function render( array $metrics ): string {
33 $this->tagnames = [
34 'instance',
35 'version',
36 ];
37 $this->tagvalues = [
38 gethostname(),
39 Environment::wordpress_version_text( true ),
40 ];
41 if ( Environment::is_wordpress_multisite() ) {
42 $this->tagnames[] = 'site';
43 $this->tagvalues[] = Blog::get_current_blog_id( 0 );
44 }
45 $lines = [];
46 foreach ( $metrics as $metric ) {
47 foreach ( $metric->getSamples() as $sample ) {
48 $lines[] = $this->renderSample( $metric, $sample );
49 }
50 }
51 return '[{"metrics":[' . implode( ',', $lines ) . ']}]';
52 }
53
54 /**
55 * @param MetricFamilySamples $metric
56 * @param Sample $sample
57 * @return string
58 */
59 private function renderSample( MetricFamilySamples $metric, Sample $sample ): string {
60 if ( in_array( $metric->getType(), [ 'counter', 'gauge' ], true ) ) {
61 $rmetric = [
62 'type' => 'counter' === $metric->getType() ? 'count' : $metric->getType(),
63 'name' => str_replace( '_', '.', $sample->getName() ),
64 'value' => (float) $sample->getValue(),
65 'interval.ms' => 1000,
66 'timestamp' => time(),
67 ];
68 $labelNames = $metric->getLabelNames();
69 if ( $metric->hasLabelNames() || $sample->hasLabelNames() ) {
70 $rmetric['attributes'] = $this->escapeAllLabels( $labelNames, $sample );
71 }
72 return wp_json_encode( $rmetric );
73 }
74 return '';
75 }
76
77 /**
78 * @param string $v
79 * @return string
80 */
81 private function escapeLabelValue( string $v ): string {
82 return str_replace( [ '\\', "\n", '"', '=', ',' ], [ '\\\\', "\\n", '\\"', '', '' ], $v );
83 }
84
85 /**
86 * @param string[] $labelNames
87 * @param Sample $sample
88 *
89 * @return string[]
90 */
91 private function escapeAllLabels( array $labelNames, Sample $sample ): array {
92 $escapedLabels = [];
93 $labels = array_combine( array_merge( $labelNames, $sample->getLabelNames(), $this->tagnames ), array_merge( $sample->getLabelValues(), $this->tagvalues ) );
94 if ( $labels === false ) {
95 return [];
96 }
97 foreach ( $labels as $labelName => $labelValue ) {
98 $escapedLabels[$labelName] = $this->escapeLabelValue( (string) $labelValue );
99 }
100 return $escapedLabels;
101 }
102 }
103