| 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 RenderLineFormat implements RendererInterface { |
| 11 |
|
| 12 |
/** |
| 13 |
* The array of tag names. |
| 14 |
* |
| 15 |
* @since 3.0.0 |
| 16 |
* @var array $tagnames The tag names. |
| 17 |
*/ |
| 18 |
private $tagnames = []; |
| 19 |
|
| 20 |
/** |
| 21 |
* The array of tag values. |
| 22 |
* |
| 23 |
* @since 3.0.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 implode( "\n", $lines ) . "\n"; |
| 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 |
$measurement = ''; |
| 61 |
$metricname = ''; |
| 62 |
foreach ( explode( '_', $sample->getName() ) as $i => $k ) { |
| 63 |
if ( $i < 3 ) { |
| 64 |
$measurement .= ( '' === $measurement ? '' : '_' ) . $k; |
| 65 |
} else { |
| 66 |
$metricname .= ( '' === $metricname ? '' : '_' ) . $k; |
| 67 |
} |
| 68 |
} |
| 69 |
$labelNames = $metric->getLabelNames(); |
| 70 |
if ( $metric->hasLabelNames() || $sample->hasLabelNames() ) { |
| 71 |
$escapedLabels = $this->escapeAllLabels( $labelNames, $sample ); |
| 72 |
$measurement .= ',' . implode( ',', $escapedLabels ); |
| 73 |
} |
| 74 |
return $measurement . ' ' . $metricname . '=' . $sample->getValue(); |
| 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 |
|