MediaWiki master
StatsdFormatter.php
Go to the documentation of this file.
1<?php
20declare( strict_types=1 );
21
23
25
34 public function getFormattedSamples( string $prefix, MetricInterface $metric ): array {
35 $output = [];
36
37 // append component to prefix if set
38 if ( $metric->getComponent() !== '' ) {
39 $prefix .= ".{$metric->getComponent()}";
40 }
41
42 // Metrics used in HistogramMetrics are not compatible with StatsD
43 if ( $metric->isHistogram() ) {
44 return [];
45 }
46
47 foreach ( $metric->getSamples() as $sample ) {
48 // dot-separate prefix, component, name, and label values `prefix.component.name.value1.value2`
49 $stat = implode( '.', array_merge( [ $prefix, $metric->getName() ], $sample->getLabelValues() ) );
50
51 // merge value with separator `:42`
52 $value = ':' . $sample->getValue();
53
54 // merge type indicator with separator `|c`
55 $type = '|' . $metric->getTypeIndicator();
56
57 // blank string if samplerate is 1.0, otherwise add samplerate indicator `|@0.5`
58 $sampleRate = $metric->getSampleRate() !== 1.0 ? '|@' . $metric->getSampleRate() : '';
59
60 // combine and append to output `prefix.component.name.value1.value2:42|c|@0.5`
61 $output[] = $stat . $value . $type . $sampleRate;
62 }
63 return $output;
64 }
65}
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.