diff --git a/src/Serializer/EnvelopItems/MetricsItem.php b/src/Serializer/EnvelopItems/MetricsItem.php index 3bd0b7c3a..306ee44fa 100644 --- a/src/Serializer/EnvelopItems/MetricsItem.php +++ b/src/Serializer/EnvelopItems/MetricsItem.php @@ -53,14 +53,17 @@ public static function toEnvelopeItem(Event $event): string // type - |c|, |d|, ... $line .= '|' . $metric->getType() . '|'; - $tags = ''; + $tags = []; foreach ($metric->getTags() as $key => $value) { - $tags .= preg_replace(self::KEY_PATTERN, '_', $key) . + $tags[] = preg_replace(self::KEY_PATTERN, '_', $key) . ':' . preg_replace(self::VALUE_PATTERN, '', $value); } - // tags - #key:value,key:value... - $line .= '#' . $tags . '|'; + if (!empty($tags)) { + // tags - #key:value,key:value... + $line .= '#' . implode(',', $tags) . '|'; + } + // timestamp - T123456789 $line .= 'T' . $metric->getTimestamp(); diff --git a/tests/Serializer/PayloadSerializerTest.php b/tests/Serializer/PayloadSerializerTest.php index 2819b941f..3746423c4 100644 --- a/tests/Serializer/PayloadSerializerTest.php +++ b/tests/Serializer/PayloadSerializerTest.php @@ -412,10 +412,11 @@ public static function serializeAsEnvelopeDataProvider(): iterable false, ]; - $counter = new CounterType('counter', 1.0, MetricsUnit::second(), ['foo' => 'bar'], 1597790835); + $counter = new CounterType('counter', 1.0, MetricsUnit::second(), ['foo' => 'bar', 'baz' => 'qux'], 1597790835); $distribution = new DistributionType('distribution', 1.0, MetricsUnit::second(), ['$foo$' => '%bar%'], 1597790835); $gauge = new GaugeType('gauge', 1.0, MetricsUnit::second(), ['föö' => 'bär'], 1597790835); $set = new SetType('set', 1.0, MetricsUnit::second(), ['%{key}' => '$value$'], 1597790835); + $noTags = new CounterType('no_tags', 1.0, MetricsUnit::second(), [], 1597790835); $event = Event::createMetrics(new EventId('fc9442f5aef34234bb22b9a615e30ccd')); $event->setMetrics([ @@ -423,17 +424,19 @@ public static function serializeAsEnvelopeDataProvider(): iterable $distribution, $gauge, $set, + $noTags, ]); yield [ $event, <<