MediaWiki master
DogStatsdFormatter.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 foreach ( $metric->getSamples() as $sample ) {
30 // dot-separate prefix, component, and name `prefix.component.name`
31 $stat = implode( '.', [ $prefix, $metric->getName() ] );
32
33 // merge value with separator `:42`
34 $value = ':' . $sample->getValue();
35
36 // merge type indicator with separator `|c`
37 $type = '|' . $metric->getTypeIndicator();
38
39 // blank string if samplerate is 1.0, otherwise add samplerate indicator `|@0.5`
40 $sampleRate = $metric->getSampleRate() !== 1.0 ? '|@' . $metric->getSampleRate() : '';
41
42 // merge label keys and label values `key:value`
43 $labels = [];
44 $labelValues = $sample->getLabelValues();
45 foreach ( $metric->getLabelKeys() as $i => $labelKey ) {
46 $labels[] = $labelKey . ':' . $labelValues[$i];
47 }
48
49 // combine label kv pairs `|# key1:value1,key2:value2`
50 $tags = $labels === [] ? '' : '|#' . implode( ",", $labels );
51
52 // combine and append to output `prefix.component.name:42|c|@0.5|#key1:value1,key2:value2`
53 $output[] = $stat . $value . $type . $sampleRate . $tags;
54 }
55 return $output;
56 }
57}
DogStatsD Wire Format Implementation.
getFormattedSamples(string $prefix, MetricInterface $metric)
Renders metric to line format.string[]
getSamples()
Returns subset of samples corresponding to sample rate setting.
getLabelKeys()
Returns the list of defined label keys.