Skip to content

Commit 46a9089

Browse files
committed
Add DDM support
1 parent 76f01d0 commit 46a9089

File tree

7 files changed

+182
-0
lines changed

7 files changed

+182
-0
lines changed

src/Event.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ final class Event
5555
*/
5656
private $checkIn;
5757

58+
/**
59+
* @var array<string, array<string>|float|int|string>|null The check in data
60+
*/
61+
private $metric;
62+
5863
/**
5964
* @var string|null The name of the server (e.g. the host name)
6065
*/
@@ -210,6 +215,11 @@ public static function createCheckIn(?EventId $eventId = null): self
210215
return new self($eventId, EventType::checkIn());
211216
}
212217

218+
public static function createMetric(?EventId $eventId = null): self
219+
{
220+
return new self($eventId, EventType::metric());
221+
}
222+
213223
/**
214224
* Gets the ID of this event.
215225
*/
@@ -354,6 +364,24 @@ public function setCheckIn(?CheckIn $checkIn): self
354364
return $this;
355365
}
356366

367+
/**
368+
* @return array<string, array<string>|float|int|string>|null
369+
*/
370+
public function getMetric(): ?array
371+
{
372+
return $this->metric;
373+
}
374+
375+
/**
376+
* @param array<string, array<string>|float|int|string> $metric
377+
*/
378+
public function setMetric(array $metric): self
379+
{
380+
$this->metric = $metric;
381+
382+
return $this;
383+
}
384+
357385
/**
358386
* Gets the name of the server.
359387
*/

src/EventType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public static function checkIn(): self
4242
return self::getInstance('check_in');
4343
}
4444

45+
public static function metric(): self
46+
{
47+
return self::getInstance('metric_buckets');
48+
}
49+
4550
public function __toString(): string
4651
{
4752
return $this->value;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\Serializer\EnvelopItems;
6+
7+
use Sentry\Event;
8+
use Sentry\Util\JSON;
9+
10+
/**
11+
* @internal
12+
*/
13+
class MetricsItem implements EnvelopeItemInterface
14+
{
15+
public static function toEnvelopeItem(Event $event): string
16+
{
17+
$header = [
18+
'type' => (string) $event->getType(),
19+
'content_type' => 'application/json',
20+
];
21+
22+
$payload = [];
23+
24+
$metric = $event->getMetric();
25+
if ($event->getMetric() !== null) {
26+
$payload[] = $metric;
27+
}
28+
29+
return sprintf("%s\n%s", JSON::encode($header), JSON::encode($payload));
30+
}
31+
}

src/Serializer/PayloadSerializer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Sentry\Options;
1010
use Sentry\Serializer\EnvelopItems\CheckInItem;
1111
use Sentry\Serializer\EnvelopItems\EventItem;
12+
use Sentry\Serializer\EnvelopItems\MetricsItem;
1213
use Sentry\Serializer\EnvelopItems\ProfileItem;
1314
use Sentry\Serializer\EnvelopItems\TransactionItem;
1415
use Sentry\Tracing\DynamicSamplingContext;
@@ -77,6 +78,9 @@ public function serialize(Event $event): string
7778
case EventType::checkIn():
7879
$items = CheckInItem::toEnvelopeItem($event);
7980
break;
81+
case EventType::metric():
82+
$items = MetricsItem::toEnvelopeItem($event);
83+
break;
8084
}
8185

8286
return sprintf("%s\n%s", JSON::encode($envelopeHeader), $items);

src/State/Hub.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,84 @@ public function getTransaction(): ?Transaction
319319
return $this->getScope()->getTransaction();
320320
}
321321

322+
/**
323+
* @param int|float $value
324+
* @param string[] $tags
325+
*/
326+
public function metricsIncr(string $name, $value, array $tags): ?EventId
327+
{
328+
$client = $this->getClient();
329+
330+
if ($client === null) {
331+
return null;
332+
}
333+
334+
$event = Event::createMetric();
335+
$metric = [
336+
'timestamp' => time(),
337+
'width' => 0,
338+
'name' => 'c:custom/' . $name . '@none',
339+
'type' => 'c',
340+
'value' => $value,
341+
'tags' => $tags,
342+
];
343+
$event->setMetric($metric);
344+
345+
return $this->captureEvent($event);
346+
}
347+
348+
/**
349+
* @param int|float $value
350+
* @param string[] $tags
351+
*/
352+
public function metricsDistribution(string $name, $value, array $tags, ?string $unit = null): ?EventId
353+
{
354+
$client = $this->getClient();
355+
356+
if ($client === null) {
357+
return null;
358+
}
359+
360+
$event = Event::createMetric();
361+
$metric = [
362+
'timestamp' => time(),
363+
'width' => 0,
364+
'name' => 'd:custom/' . $name . '@' . ($unit ?? 'none'),
365+
'type' => 'd',
366+
'value' => $value,
367+
'tags' => $tags,
368+
];
369+
$event->setMetric($metric);
370+
371+
return $this->captureEvent($event);
372+
}
373+
374+
/**
375+
* @param int|float $value
376+
* @param string[] $tags
377+
*/
378+
public function metricsSet(string $name, $value, array $tags): ?EventId
379+
{
380+
$client = $this->getClient();
381+
382+
if ($client === null) {
383+
return null;
384+
}
385+
386+
$event = Event::createMetric();
387+
$metric = [
388+
'timestamp' => time(),
389+
'width' => 0,
390+
'name' => 's:custom/' . $name . '@none',
391+
'type' => 's',
392+
'value' => $value,
393+
'tags' => $tags,
394+
];
395+
$event->setMetric($metric);
396+
397+
return $this->captureEvent($event);
398+
}
399+
322400
/**
323401
* {@inheritdoc}
324402
*/

src/State/HubInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
use Sentry\Tracing\Transaction;
1818
use Sentry\Tracing\TransactionContext;
1919

20+
/**
21+
* This interface represent the class which is responsible for maintaining a
22+
* stack of pairs of clients and scopes. It is the main entry point to talk
23+
* with the Sentry client.
24+
*
25+
* @method EventId|null metricsIncr(string $name, $value, array $tags)
26+
* @method EventId|null metricsDistribution(string $name, $value, array $tags, ?string $unit = null)
27+
* @method EventId|null metricsSet(string $name, $value, array $tags)
28+
*/
2029
interface HubInterface
2130
{
2231
/**

src/functions.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,30 @@ function continueTrace(string $sentryTrace, string $baggage): TransactionContext
268268

269269
return TransactionContext::fromHeaders($sentryTrace, $baggage);
270270
}
271+
272+
/**
273+
* @param int|float $value
274+
* @param string[] $tags
275+
*/
276+
function metricsIncr(string $key, $value, array $tags): ?EventId
277+
{
278+
return SentrySdk::getCurrentHub()->metricsIncr($key, $value, $tags);
279+
}
280+
281+
/**
282+
* @param int|float $value
283+
* @param string[] $tags
284+
*/
285+
function metricsDistribution(string $key, $value, array $tags, ?string $unit = null): ?EventId
286+
{
287+
return SentrySdk::getCurrentHub()->metricsDistribution($key, $value, $tags, $unit);
288+
}
289+
290+
/**
291+
* @param int|float $value
292+
* @param string[] $tags
293+
*/
294+
function metricsSet(string $key, $value, array $tags): ?EventId
295+
{
296+
return SentrySdk::getCurrentHub()->metricsSet($key, $value, $tags);
297+
}

0 commit comments

Comments
 (0)