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
3 changes: 3 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

return (new PhpCsFixer\Config())
->setRules([
'@PHP71Migration' => true,
Expand Down Expand Up @@ -35,6 +37,7 @@
'elements' => ['arrays'],
],
'no_whitespace_before_comma_in_array' => false, // Should be dropped when we drop support for PHP 7.x
'stringable_for_to_string' => false,
])
->setRiskyAllowed(true)
->setFinder(
Expand Down
8 changes: 8 additions & 0 deletions src/Metrics/MetricsAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public function add(
$hub = SentrySdk::getCurrentHub();
$client = $hub->getClient();

if (!\is_int($value) && !\is_float($value)) {
if ($client !== null) {
$client->getOptions()->getLoggerOrNullLogger()->debug('Metrics value is neither int nor float. Metric will be discarded');
}

return;
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/Metrics/Types/CounterMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(
) {
parent::__construct($name, $traceId, $spanId, $timestamp, $attributes, $unit);

$this->value = (float) $value;
$this->value = $value;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Metrics/Types/DistributionMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(
) {
parent::__construct($name, $traceId, $spanId, $timestamp, $attributes, $unit);

$this->value = (float) $value;
$this->value = $value;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Metrics/Types/GaugeMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(
) {
parent::__construct($name, $traceId, $spanId, $timestamp, $attributes, $unit);

$this->value = (float) $value;
$this->value = $value;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Metrics/Types/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
use Sentry\Tracing\TraceId;
use Sentry\Unit;

/**
* @internal
*/
abstract class Metric
{
/**
Expand Down
39 changes: 39 additions & 0 deletions tests/Metrics/TraceMetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,43 @@ public function testBeforeSendMetricAltersContent()
$metric = $event->getMetrics()[0];
$this->assertEquals(99999, $metric->getValue());
}

public function testIntType()
{
trace_metrics()->count('test-count', 2, ['foo' => 'bar']);
trace_metrics()->flush();

$this->assertCount(1, StubTransport::$events);
$event = StubTransport::$events[0];

$this->assertCount(1, $event->getMetrics());
$metric = $event->getMetrics()[0];

$this->assertEquals('test-count', $metric->getName());
$this->assertEquals(2, $metric->getValue());
}

public function testFloatType(): void
{
trace_metrics()->gauge('test-gauge', 10.50, ['foo' => 'bar']);
trace_metrics()->flush();

$this->assertCount(1, StubTransport::$events);
$event = StubTransport::$events[0];

$this->assertCount(1, $event->getMetrics());
$metric = $event->getMetrics()[0];

$this->assertEquals('test-gauge', $metric->getName());
$this->assertEquals(10.50, $metric->getValue());
}

public function testInvalidTypeIsDiscarded(): void
{
// @phpstan-ignore-next-line
trace_metrics()->count('test-count', 'test-value');
trace_metrics()->flush();

$this->assertEmpty(StubTransport::$events);
}
}