Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
StatsdFormatter
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 1
 getFormattedSamples
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7declare( strict_types=1 );
8
9namespace Wikimedia\Stats\Formatters;
10
11use Wikimedia\Stats\Metrics\MetricInterface;
12
13/**
14 * StatsD Wire Format Implementation
15 *
16 * @author Cole White
17 * @since 1.41
18 */
19class StatsdFormatter implements FormatterInterface {
20    /** @inheritDoc */
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}