MediaWiki master
StatsdFormatter.php
Go to the documentation of this file.
1<?php
7declare( strict_types=1 );
8
10
12
21 public function getFormattedSamples( string $prefix, MetricInterface $metric ): array {
22 $output = [];
23
24 // append component to prefix if set
25 if ( $metric->getComponent() !== '' ) {
26 $prefix .= ".{$metric->getComponent()}";
27 }
28
29 // Metrics used in HistogramMetrics are not compatible with StatsD
30 if ( $metric->isHistogram() ) {
31 return [];
32 }
33
34 foreach ( $metric->getSamples() as $sample ) {
35 // dot-separate prefix, component, name, and label values `prefix.component.name.value1.value2`
36 $stat = implode( '.', [ $prefix, $metric->getName(), ...$sample->getLabelValues() ] );
37
38 // merge value with separator `:42`
39 $value = ':' . $sample->getValue();
40
41 // merge type indicator with separator `|c`
42 $type = '|' . $metric->getTypeIndicator();
43
44 // blank string if samplerate is 1.0, otherwise add samplerate indicator `|@0.5`
45 $sampleRate = $metric->getSampleRate() !== 1.0 ? '|@' . $metric->getSampleRate() : '';
46
47 // combine and append to output `prefix.component.name.value1.value2:42|c|@0.5`
48 $output[] = $stat . $value . $type . $sampleRate;
49 }
50 return $output;
51 }
52}
StatsD Wire Format Implementation.
getFormattedSamples(string $prefix, MetricInterface $metric)
Renders metric to line format.string[]
getSamples()
Returns subset of samples corresponding to sample rate setting.
isHistogram()
Indicates the metric instance is used in a Histogram.