Skip to content
Open
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\AbstractType;
use Sentry\Profiling\Profile;
use Sentry\Tracing\Span;

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

/**
* @var AbstractType[]
*/
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 AbstractType[]
*/
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 AbstractType[] $metrics
*/
public function setMetrics(array $metrics): self
{
$this->metrics = $metrics;

return $this;
}

Expand Down
15 changes: 11 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,16 @@ public static function cases(): array
];
}

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

public function __toString(): string
{
return $this->value;
Expand Down
84 changes: 59 additions & 25 deletions src/Metrics/Metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace Sentry\Metrics;

use Sentry\EventId;
use Sentry\Metrics\Types\CounterType;
use Sentry\Metrics\Types\DistributionType;
use Sentry\Metrics\Types\GaugeType;
use Sentry\Tracing\SpanContext;

use function Sentry\trace;
Expand All @@ -16,6 +19,16 @@ class Metrics
*/
private static $instance;

/**
* @var MetricsAggregator
*/
private $aggregator;

private function __construct()
{
$this->aggregator = new MetricsAggregator();
}

public static function getInstance(): self
{
if (self::$instance === null) {
Expand All @@ -28,58 +41,82 @@ 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 Metrics::count() instead. To be removed in 5.x.
*/
public function increment(
string $key,
float $value,
?MetricsUnit $unit = null,
?Unit $unit = null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
?Unit $unit = null,
?MetricsUnit $unit = null,

array $tags = [],
?int $timestamp = null,
int $stackLevel = 0
): void {
}

/**
* @param array<string, string> $tags
*
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
* @param array<string, int|float|string|bool> $attributes
*/
public function count(
string $name,
float $value,
array $attributes = [],
?Unit $unit = null
): void {
$this->aggregator->add(
CounterType::TYPE,
$name,
$value,
$attributes,
$unit
);
}

/**
* @param array<string, int|float|string|bool> $attributes
*/
public function distribution(
string $key,
string $name,
float $value,
?MetricsUnit $unit = null,
array $tags = [],
?int $timestamp = null,
int $stackLevel = 0
array $attributes = [],
?Unit $unit = null
): void {
$this->aggregator->add(
DistributionType::TYPE,
$name,
$value,
$attributes,
$unit
);
}

/**
* @param array<string, string> $tags
*
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
* @param array<string, int|float|string|bool> $attributes
*/
public function gauge(
string $key,
string $name,
float $value,
?MetricsUnit $unit = null,
array $tags = [],
?int $timestamp = null,
int $stackLevel = 0
array $attributes = [],
?Unit $unit = null
): void {
$this->aggregator->add(
GaugeType::TYPE,
$name,
$value,
$attributes,
$unit
);
}

/**
* @param int|string $value
* @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 To be removed in 5.x.
*/
public function set(
string $key,
$value,
?MetricsUnit $unit = null,
?Unit $unit = null,
array $tags = [],
?int $timestamp = null,
int $stackLevel = 0
Expand All @@ -94,7 +131,7 @@ public function set(
*
* @return T
*
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
* @deprecated To be removed in 5.x.
*/
public function timing(
string $key,
Expand All @@ -113,11 +150,8 @@ function () use ($callback) {
);
}

/**
* @deprecated Metrics are no longer supported. Metrics API is a no-op and will be removed in 5.x.
*/
public function flush(): ?EventId
{
return null;
return $this->aggregator->flush();
}
}
109 changes: 109 additions & 0 deletions src/Metrics/MetricsAggregator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

namespace Sentry\Metrics;

use Sentry\Client;
use Sentry\Event;
use Sentry\EventId;
use Sentry\Metrics\Types\AbstractType;
use Sentry\Metrics\Types\CounterType;
use Sentry\Metrics\Types\DistributionType;
use Sentry\Metrics\Types\GaugeType;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Sentry\Util\RingBuffer;

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

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

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

private const METRIC_TYPES = [
CounterType::TYPE => CounterType::class,
DistributionType::TYPE => DistributionType::class,
GaugeType::TYPE => GaugeType::class,
];

/**
* @param int|float|string $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();

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

$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 AbstractType $metric */
/** @phpstan-ignore-next-line SetType accepts int|float|string, others only int|float */
$metric = new $metricTypeClass($name, $value, $traceId, $spanId, $attributes, microtime(true), $unit);

$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