Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,11 @@ parameters:
count: 1
path: src/Dsn.php

-
message: "#^Method Sentry\\\\Event\\:\\:getMetrics\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Event.php

-
message: "#^Method Sentry\\\\Event\\:\\:getMetricsSummary\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Event.php

-
message: "#^Method Sentry\\\\Event\\:\\:setMetrics\\(\\) has parameter \\$metrics with no value type specified in iterable type array\\.$#"
count: 1
path: src/Event.php

-
message: "#^Method Sentry\\\\Event\\:\\:setMetricsSummary\\(\\) has parameter \\$metricsSummary with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
17 changes: 11 additions & 6 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Sentry\Context\OsContext;
use Sentry\Context\RuntimeContext;
use Sentry\Logs\Log;
use Sentry\Metrics\Types\Metric;
use Sentry\Profiling\Profile;
use Sentry\Tracing\Span;

Expand Down Expand Up @@ -71,6 +72,11 @@ final class Event
*/
private $logs = [];

/**
* @var Metric[]
*/
private $metrics = [];

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

/**
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
*/
public static function createMetrics(?EventId $eventId = null): self
{
return new self($eventId, EventType::metrics());
Expand Down Expand Up @@ -446,18 +449,20 @@ public function setLogs(array $logs): self
}

/**
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
* @return Metric[]
*/
public function getMetrics(): array
{
return [];
return $this->metrics;
}

/**
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
* @param Metric[] $metrics
*/
public function setMetrics(array $metrics): self
{
$this->metrics = $metrics;

return $this;
}

Expand Down
16 changes: 12 additions & 4 deletions src/EventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ public static function logs(): self
return self::getInstance('log');
}

/**
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
*/
public static function metrics(): self
{
return self::getInstance('metrics');
return self::getInstance('trace_metric');
}

/**
Expand All @@ -71,6 +68,17 @@ public static function cases(): array
];
}

public function requiresEventId(): bool
{
switch ($this) {
case self::metrics():
case self::logs():
return false;
default:
return true;
}
}

public function __toString(): string
{
return $this->value;
Expand Down
20 changes: 13 additions & 7 deletions src/Metrics/Metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@

use Sentry\EventId;
use Sentry\Tracing\SpanContext;
use Sentry\Unit;

use function Sentry\trace;

class_alias(Unit::class, '\Sentry\Metrics\MetricsUnit');

/**
* @deprecated use TraceMetrics instead
*/
class Metrics
{
/**
Expand All @@ -28,12 +34,12 @@ public static function getInstance(): self
/**
* @param array<string, string> $tags
*
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
* @deprecated Use TraceMetrics::count() instead. To be removed in 5.x.
*/
public function increment(
string $key,
float $value,
?MetricsUnit $unit = null,
?Unit $unit = null,
array $tags = [],
?int $timestamp = null,
int $stackLevel = 0
Expand All @@ -43,12 +49,12 @@ public function increment(
/**
* @param array<string, string> $tags
*
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
* @deprecated Use TraceMetrics::distribution() instead. Metrics API is a no-op and will be removed in 5.x.
*/
public function distribution(
string $key,
float $value,
?MetricsUnit $unit = null,
?Unit $unit = null,
array $tags = [],
?int $timestamp = null,
int $stackLevel = 0
Expand All @@ -58,12 +64,12 @@ public function distribution(
/**
* @param array<string, string> $tags
*
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
* @deprecated Use TraceMetrics::gauge() instead. To be removed in 5.x.
*/
public function gauge(
string $key,
float $value,
?MetricsUnit $unit = null,
?Unit $unit = null,
array $tags = [],
?int $timestamp = null,
int $stackLevel = 0
Expand All @@ -79,7 +85,7 @@ public function gauge(
public function set(
string $key,
$value,
?MetricsUnit $unit = null,
?Unit $unit = null,
array $tags = [],
?int $timestamp = null,
int $stackLevel = 0
Expand Down
140 changes: 140 additions & 0 deletions src/Metrics/MetricsAggregator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

declare(strict_types=1);

namespace Sentry\Metrics;

use Sentry\Client;
use Sentry\Event;
use Sentry\EventId;
use Sentry\Metrics\Types\CounterMetric;
use Sentry\Metrics\Types\DistributionMetric;
use Sentry\Metrics\Types\GaugeMetric;
use Sentry\Metrics\Types\Metric;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Sentry\Unit;
use Sentry\Util\RingBuffer;

/**
* @internal
*/
final class MetricsAggregator
{
/**
* @var int
*/
public const METRICS_BUFFER_SIZE = 1000;

/**
* @var RingBuffer<Metric>
*/
private $metrics;

public function __construct()
{
$this->metrics = new RingBuffer(self::METRICS_BUFFER_SIZE);
}

private const METRIC_TYPES = [
CounterMetric::TYPE => CounterMetric::class,
DistributionMetric::TYPE => DistributionMetric::class,
GaugeMetric::TYPE => GaugeMetric::class,
];

/**
* @param int|float $value
* @param array<string, int|float|string|bool> $attributes
*/
public function add(
string $type,
string $name,
$value,
array $attributes,
?Unit $unit
): void {
$hub = SentrySdk::getCurrentHub();
$client = $hub->getClient();

if ($client instanceof Client) {
$options = $client->getOptions();

if ($options->getEnableMetrics() === false) {
return;
}

$defaultAttributes = [
'sentry.sdk.name' => $client->getSdkIdentifier(),
'sentry.sdk.version' => $client->getSdkVersion(),
'sentry.environment' => $options->getEnvironment() ?? Event::DEFAULT_ENVIRONMENT,
'server.address' => $options->getServerName(),
];

if ($options->shouldSendDefaultPii()) {
$hub->configureScope(function (Scope $scope) use (&$defaultAttributes) {
$user = $scope->getUser();
if ($user !== null) {
if ($user->getId() !== null) {
$defaultAttributes['user.id'] = $user->getId();
}
if ($user->getEmail() !== null) {
$defaultAttributes['user.email'] = $user->getEmail();
}
if ($user->getUsername() !== null) {
$defaultAttributes['user.name'] = $user->getUsername();
}
}
});
}

$release = $options->getRelease();
if ($release !== null) {
$defaultAttributes['sentry.release'] = $release;
}

$attributes += $defaultAttributes;
}

$spanId = null;
$traceId = null;

$span = $hub->getSpan();
if ($span !== null) {
$spanId = $span->getSpanId();
$traceId = $span->getTraceId();
} else {
$hub->configureScope(function (Scope $scope) use (&$traceId, &$spanId) {
$propagationContext = $scope->getPropagationContext();
$traceId = $propagationContext->getTraceId();
$spanId = $propagationContext->getSpanId();
});
}

$metricTypeClass = self::METRIC_TYPES[$type];
/** @var Metric $metric */
/** @phpstan-ignore-next-line */
$metric = new $metricTypeClass($name, $value, $traceId, $spanId, $attributes, microtime(true), $unit);

if ($client !== null) {
$beforeSendMetric = $client->getOptions()->getBeforeSendMetricCallback();
$metric = $beforeSendMetric($metric);
if ($metric === null) {
return;
}
}

$this->metrics->push($metric);
}

public function flush(): ?EventId
{
if ($this->metrics->isEmpty()) {
return null;
}

$hub = SentrySdk::getCurrentHub();
$event = Event::createMetrics()->setMetrics($this->metrics->drain());

return $hub->captureEvent($event);
}
}
Loading