Skip to content

Commit

Permalink
Fix tags not being serialized correctly for metrics payload (#1672)
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive authored Dec 22, 2023
1 parent b3dd043 commit 493d811
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/Serializer/EnvelopItems/MetricsItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
9 changes: 6 additions & 3 deletions tests/Serializer/PayloadSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,28 +412,31 @@ 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([
$counter,
$distribution,
$gauge,
$set,
$noTags,
]);

yield [
$event,
<<<TEXT
{"event_id":"fc9442f5aef34234bb22b9a615e30ccd","sent_at":"2020-08-18T22:47:15Z","dsn":"http:\/\/public@example.com\/sentry\/1","sdk":{"name":"sentry.php","version":"$sdkVersion"}}
{"type":"statsd","length":172}
counter@second:1|c|#foo:bar|T1597790835
{"type":"statsd","length":211}
counter@second:1|c|#foo:bar,baz:qux|T1597790835
distribution@second:1|d|#_foo_:bar|T1597790835
gauge@second:1:1:1:1:1|g|#f_:br|T1597790835
set@second:1|s|#_key_:\$value\$|T1597790835
no_tags@second:1|c|T1597790835
TEXT
,
false,
Expand Down

0 comments on commit 493d811

Please sign in to comment.