Skip to content

Commit

Permalink
Add DDM support
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric committed Oct 31, 2023
1 parent 5523e9c commit 62bb5b7
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ final class Event
*/
private $checkIn;

/**
* @var array<string, array<string>|float|int|string>|null The check in data
*/
private $metric;

/**
* @var string|null The name of the server (e.g. the host name)
*/
Expand Down Expand Up @@ -210,6 +215,11 @@ public static function createCheckIn(?EventId $eventId = null): self
return new self($eventId, EventType::checkIn());
}

public static function createMetric(?EventId $eventId = null): self

Check warning on line 218 in src/Event.php

View check run for this annotation

Codecov / codecov/patch

src/Event.php#L218

Added line #L218 was not covered by tests
{
return new self($eventId, EventType::metric());

Check warning on line 220 in src/Event.php

View check run for this annotation

Codecov / codecov/patch

src/Event.php#L220

Added line #L220 was not covered by tests
}

/**
* Gets the ID of this event.
*/
Expand Down Expand Up @@ -354,6 +364,24 @@ public function setCheckIn(?CheckIn $checkIn): self
return $this;
}

/**
* @return array<string, array<string>|float|int|string>|null
*/
public function getMetric(): ?array

Check warning on line 370 in src/Event.php

View check run for this annotation

Codecov / codecov/patch

src/Event.php#L370

Added line #L370 was not covered by tests
{
return $this->metric;

Check warning on line 372 in src/Event.php

View check run for this annotation

Codecov / codecov/patch

src/Event.php#L372

Added line #L372 was not covered by tests
}

/**
* @param array<string, array<string>|float|int|string> $metric
*/
public function setMetric(array $metric): self

Check warning on line 378 in src/Event.php

View check run for this annotation

Codecov / codecov/patch

src/Event.php#L378

Added line #L378 was not covered by tests
{
$this->metric = $metric;

Check warning on line 380 in src/Event.php

View check run for this annotation

Codecov / codecov/patch

src/Event.php#L380

Added line #L380 was not covered by tests

return $this;

Check warning on line 382 in src/Event.php

View check run for this annotation

Codecov / codecov/patch

src/Event.php#L382

Added line #L382 was not covered by tests
}

/**
* Gets the name of the server.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/EventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public static function checkIn(): self
return self::getInstance('check_in');
}

public static function metric(): self

Check warning on line 45 in src/EventType.php

View check run for this annotation

Codecov / codecov/patch

src/EventType.php#L45

Added line #L45 was not covered by tests
{
return self::getInstance('metric_buckets');

Check warning on line 47 in src/EventType.php

View check run for this annotation

Codecov / codecov/patch

src/EventType.php#L47

Added line #L47 was not covered by tests
}

public function __toString(): string
{
return $this->value;
Expand Down
31 changes: 31 additions & 0 deletions src/Serializer/EnvelopItems/MetricsItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Sentry\Serializer\EnvelopItems;

use Sentry\Event;
use Sentry\Util\JSON;

/**
* @internal
*/
class MetricsItem implements EnvelopeItemInterface
{
public static function toEnvelopeItem(Event $event): string
{
$header = [
'type' => (string) $event->getType(),
'content_type' => 'application/json',
];

Check warning on line 20 in src/Serializer/EnvelopItems/MetricsItem.php

View check run for this annotation

Codecov / codecov/patch

src/Serializer/EnvelopItems/MetricsItem.php#L15-L20

Added lines #L15 - L20 were not covered by tests

$payload = [];

Check warning on line 22 in src/Serializer/EnvelopItems/MetricsItem.php

View check run for this annotation

Codecov / codecov/patch

src/Serializer/EnvelopItems/MetricsItem.php#L22

Added line #L22 was not covered by tests

$metric = $event->getMetric();
if ($event->getMetric() !== null) {
$payload[] = $metric;
}

Check warning on line 27 in src/Serializer/EnvelopItems/MetricsItem.php

View check run for this annotation

Codecov / codecov/patch

src/Serializer/EnvelopItems/MetricsItem.php#L24-L27

Added lines #L24 - L27 were not covered by tests

return sprintf("%s\n%s", JSON::encode($header), JSON::encode($payload));
}

Check warning on line 30 in src/Serializer/EnvelopItems/MetricsItem.php

View check run for this annotation

Codecov / codecov/patch

src/Serializer/EnvelopItems/MetricsItem.php#L29-L30

Added lines #L29 - L30 were not covered by tests
}
4 changes: 4 additions & 0 deletions src/Serializer/PayloadSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Sentry\Options;
use Sentry\Serializer\EnvelopItems\CheckInItem;
use Sentry\Serializer\EnvelopItems\EventItem;
use Sentry\Serializer\EnvelopItems\MetricsItem;
use Sentry\Serializer\EnvelopItems\ProfileItem;
use Sentry\Serializer\EnvelopItems\TransactionItem;
use Sentry\Tracing\DynamicSamplingContext;
Expand Down Expand Up @@ -77,6 +78,9 @@ public function serialize(Event $event): string
case EventType::checkIn():
$items = CheckInItem::toEnvelopeItem($event);
break;
case EventType::metric():
$items = MetricsItem::toEnvelopeItem($event);
break;

Check warning on line 83 in src/Serializer/PayloadSerializer.php

View check run for this annotation

Codecov / codecov/patch

src/Serializer/PayloadSerializer.php#L81-L83

Added lines #L81 - L83 were not covered by tests
}

return sprintf("%s\n%s", JSON::encode($envelopeHeader), $items);
Expand Down
78 changes: 78 additions & 0 deletions src/State/Hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,84 @@ public function getTransaction(): ?Transaction
return $this->getScope()->getTransaction();
}

/**
* @param int|float $value
* @param string[] $tags
*/
public function metricsIncr(string $name, $value, array $tags): ?EventId

Check warning on line 326 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L326

Added line #L326 was not covered by tests
{
$client = $this->getClient();

Check warning on line 328 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L328

Added line #L328 was not covered by tests

if ($client === null) {
return null;

Check warning on line 331 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L330-L331

Added lines #L330 - L331 were not covered by tests
}

$event = Event::createMetric();
$metric = [
'timestamp' => time(),
'width' => 0,
'name' => 'c:custom/' . $name . '@none',
'type' => 'c',
'value' => $value,
'tags' => $tags,
];
$event->setMetric($metric);

Check warning on line 343 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L334-L343

Added lines #L334 - L343 were not covered by tests

return $this->captureEvent($event);

Check warning on line 345 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L345

Added line #L345 was not covered by tests
}

/**
* @param int|float $value
* @param string[] $tags
*/
public function metricsDistribution(string $name, $value, array $tags, ?string $unit = null): ?EventId

Check warning on line 352 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L352

Added line #L352 was not covered by tests
{
$client = $this->getClient();

Check warning on line 354 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L354

Added line #L354 was not covered by tests

if ($client === null) {
return null;

Check warning on line 357 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L356-L357

Added lines #L356 - L357 were not covered by tests
}

$event = Event::createMetric();
$metric = [
'timestamp' => time(),
'width' => 0,
'name' => 'd:custom/' . $name . '@' . ($unit ?? 'none'),
'type' => 'd',
'value' => $value,
'tags' => $tags,
];
$event->setMetric($metric);

Check warning on line 369 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L360-L369

Added lines #L360 - L369 were not covered by tests

return $this->captureEvent($event);

Check warning on line 371 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L371

Added line #L371 was not covered by tests
}

/**
* @param int|float $value
* @param string[] $tags
*/
public function metricsSet(string $name, $value, array $tags): ?EventId

Check warning on line 378 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L378

Added line #L378 was not covered by tests
{
$client = $this->getClient();

Check warning on line 380 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L380

Added line #L380 was not covered by tests

if ($client === null) {
return null;

Check warning on line 383 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L382-L383

Added lines #L382 - L383 were not covered by tests
}

$event = Event::createMetric();
$metric = [
'timestamp' => time(),
'width' => 0,
'name' => 's:custom/' . $name . '@none',
'type' => 's',
'value' => $value,
'tags' => $tags,
];
$event->setMetric($metric);

Check warning on line 395 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L386-L395

Added lines #L386 - L395 were not covered by tests

return $this->captureEvent($event);

Check warning on line 397 in src/State/Hub.php

View check run for this annotation

Codecov / codecov/patch

src/State/Hub.php#L397

Added line #L397 was not covered by tests
}

/**
* {@inheritdoc}
*/
Expand Down
9 changes: 9 additions & 0 deletions src/State/HubInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;

/**
* This interface represent the class which is responsible for maintaining a
* stack of pairs of clients and scopes. It is the main entry point to talk
* with the Sentry client.
*
* @method EventId|null metricsIncr(string $name, $value, array $tags)
* @method EventId|null metricsDistribution(string $name, $value, array $tags, ?string $unit = null)
* @method EventId|null metricsSet(string $name, $value, array $tags)
*/
interface HubInterface
{
/**
Expand Down
27 changes: 27 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,30 @@ function continueTrace(string $sentryTrace, string $baggage): TransactionContext

return TransactionContext::fromHeaders($sentryTrace, $baggage);
}

/**
* @param int|float $value
* @param string[] $tags
*/
function metricsIncr(string $key, $value, array $tags): ?EventId
{
return SentrySdk::getCurrentHub()->metricsIncr($key, $value, $tags);

Check warning on line 278 in src/functions.php

View check run for this annotation

Codecov / codecov/patch

src/functions.php#L278

Added line #L278 was not covered by tests
}

/**
* @param int|float $value
* @param string[] $tags
*/
function metricsDistribution(string $key, $value, array $tags, ?string $unit = null): ?EventId
{
return SentrySdk::getCurrentHub()->metricsDistribution($key, $value, $tags, $unit);

Check warning on line 287 in src/functions.php

View check run for this annotation

Codecov / codecov/patch

src/functions.php#L287

Added line #L287 was not covered by tests
}

/**
* @param int|float $value
* @param string[] $tags
*/
function metricsSet(string $key, $value, array $tags): ?EventId
{
return SentrySdk::getCurrentHub()->metricsSet($key, $value, $tags);

Check warning on line 296 in src/functions.php

View check run for this annotation

Codecov / codecov/patch

src/functions.php#L296

Added line #L296 was not covered by tests
}

0 comments on commit 62bb5b7

Please sign in to comment.