MediaWiki
master
Toggle main menu visibility
DogStatsdFormatter.php
Go to the documentation of this file.
1
<?php
6
7
declare( strict_types=1 );
8
9
namespace
Wikimedia\Stats\Formatters
;
10
11
use
Wikimedia\Stats\Metrics\MetricInterface
;
12
19
class
DogStatsdFormatter
implements
FormatterInterface
{
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
}
Wikimedia\Stats\Formatters\DogStatsdFormatter
DogStatsD Wire Format Implementation.
Definition
DogStatsdFormatter.php:19
Wikimedia\Stats\Formatters\DogStatsdFormatter\getFormattedSamples
getFormattedSamples(string $prefix, MetricInterface $metric)
Renders metric to line format.string[]
Definition
DogStatsdFormatter.php:21
Wikimedia\Stats\Formatters\FormatterInterface
Metrics Formatter Interface.
Definition
FormatterInterface.php:19
Wikimedia\Stats\Metrics\MetricInterface
Metric Interface.
Definition
MetricInterface.php:20
Wikimedia\Stats\Metrics\MetricInterface\getSamples
getSamples()
Returns subset of samples corresponding to sample rate setting.
Wikimedia\Stats\Metrics\MetricInterface\getTypeIndicator
getTypeIndicator()
Wikimedia\Stats\Metrics\MetricInterface\getSampleRate
getSampleRate()
Wikimedia\Stats\Metrics\MetricInterface\getComponent
getComponent()
Wikimedia\Stats\Metrics\MetricInterface\getLabelKeys
getLabelKeys()
Returns the list of defined label keys.
Wikimedia\Stats\Metrics\MetricInterface\getName
getName()
Wikimedia\Stats\Formatters
Definition
DogStatsdFormatter.php:9
includes
libs
Stats
Formatters
DogStatsdFormatter.php
Generated on
for MediaWiki by
1.18.0