/** * Tests mapAsString function */ public function testMapAsString() { $emptyMap = []; $this->assertEquals('', Helper::mapAsString($emptyMap)); $mapNormal = ['name1' => 'value1', 'name2' => 'value2']; $expected1 = 'name1.value1.name2.value2'; $expected2 = 'name1:value1:name2:value2'; $this->assertEquals($expected1, Helper::mapAsString($mapNormal)); $this->assertEquals($expected2, Helper::mapAsString($mapNormal, true, ':')); $mapSanitize = ['name1' => 'should.be.sanitized', 'name2' => 'S@n1TiZ3.M*.Hard']; $expected3 = 'name1.should_be_sanitized.name2.S_n1TiZ3_M_Hard'; $expected4 = 'name1:should.be.sanitized:name2:S@n1TiZ3.M*.Hard'; $this->assertEquals($expected3, Helper::mapAsString($mapSanitize)); $this->assertEquals($expected4, Helper::mapAsString($mapSanitize, false, ':')); }
/** * Formats metric to stats daemon format * @see https://github.com/etsy/statsd/blob/master/docs/metric_types.md * * @param Metric $metric * @param string $daemonType * @return bool|string */ protected function process(Metric &$metric, $daemonType) { // If no value, nothing to add $value = $metric->getValue(); if (!isset($value['value'])) { return false; } $value = $value['value']; $name = $metric->getName(); // Add tags to the beginning of the name if ($metric->hasTags()) { $name = Helper::mapAsString($metric->getTags()) . '.' . $name; } // Name with value and type $result = $name . ':' . $value . '|' . $daemonType; // Add sampling rate if ($metric instanceof SamplingMetricInterface) { if ($metric->getSampleRate() < 1.0) { $result .= '|@' . $metric->getSampleRate(); } } return $result; }