Skip to content

Commit

Permalink
Refactor code & add gauges
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric committed Nov 8, 2023
1 parent 39e6638 commit 03bfef7
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 106 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
},
"autoload": {
"files": [
"src/functions.php"
"src/functions.php",
"src/functions_experimental.php"
],
"psr-4": {
"Sentry\\": "src/"
Expand Down
134 changes: 134 additions & 0 deletions src/Metrics/Metrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

declare(strict_types=1);

namespace Sentry\Metrics;

use Sentry\Event;
use Sentry\EventId;
use Sentry\SentrySdk;

class Metrics
{
/**
* @var self|null
*/
private static $instance;

private function __construct()
{
}

Check warning on line 20 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L18-L20

Added lines #L18 - L20 were not covered by tests

public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new self();
}

Check warning on line 26 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L22-L26

Added lines #L22 - L26 were not covered by tests

return self::$instance;
}

Check warning on line 29 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L28-L29

Added lines #L28 - L29 were not covered by tests

/**
* @param int|float $value
* @param string[] $tags
*/
public function incr(string $name, $value, array $tags): ?EventId
{
$client = SentrySdk::getCurrentHub()->getClient();

Check warning on line 37 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L35-L37

Added lines #L35 - L37 were not covered by tests

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

Check warning on line 41 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L39-L41

Added lines #L39 - L41 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 52 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L43-L52

Added lines #L43 - L52 were not covered by tests

return $client->captureEvent($event);
}

Check warning on line 55 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L54-L55

Added lines #L54 - L55 were not covered by tests

/**
* @param int|float $value
* @param string[] $tags
*/
public function distribution(string $name, $value, array $tags, ?string $unit = null): ?EventId
{
$client = SentrySdk::getCurrentHub()->getClient();

Check warning on line 63 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L61-L63

Added lines #L61 - L63 were not covered by tests

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

Check warning on line 67 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L65-L67

Added lines #L65 - L67 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 78 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L69-L78

Added lines #L69 - L78 were not covered by tests

return $client->captureEvent($event);
}

Check warning on line 81 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L80-L81

Added lines #L80 - L81 were not covered by tests

/**
* @param int|float $value
* @param string[] $tags
*/
public function set(string $name, $value, array $tags): ?EventId
{
$client = SentrySdk::getCurrentHub()->getClient();

Check warning on line 89 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L87-L89

Added lines #L87 - L89 were not covered by tests

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

Check warning on line 93 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L91-L93

Added lines #L91 - L93 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 104 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L95-L104

Added lines #L95 - L104 were not covered by tests

return $client->captureEvent($event);
}

Check warning on line 107 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L106-L107

Added lines #L106 - L107 were not covered by tests

/**
* @param int|float $value
* @param string[] $tags
*/
public function gauge(string $name, $value, array $tags): ?EventId
{
$client = SentrySdk::getCurrentHub()->getClient();

Check warning on line 115 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L113-L115

Added lines #L113 - L115 were not covered by tests

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

Check warning on line 119 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L117-L119

Added lines #L117 - L119 were not covered by tests

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

Check warning on line 130 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L121-L130

Added lines #L121 - L130 were not covered by tests

return $client->captureEvent($event);
}

Check warning on line 133 in src/Metrics/Metrics.php

View check run for this annotation

Codecov / codecov/patch

src/Metrics/Metrics.php#L132-L133

Added lines #L132 - L133 were not covered by tests
}
78 changes: 0 additions & 78 deletions src/State/Hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,84 +319,6 @@ public function getTransaction(): ?Transaction
return $this->getScope()->getTransaction();
}

/**
* @param int|float $value
* @param string[] $tags
*/
public function metricsIncr(string $name, $value, array $tags): ?EventId
{
$client = $this->getClient();

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

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

return $this->captureEvent($event);
}

/**
* @param int|float $value
* @param string[] $tags
*/
public function metricsDistribution(string $name, $value, array $tags, ?string $unit = null): ?EventId
{
$client = $this->getClient();

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

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

return $this->captureEvent($event);
}

/**
* @param int|float $value
* @param string[] $tags
*/
public function metricsSet(string $name, $value, array $tags): ?EventId
{
$client = $this->getClient();

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

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

return $this->captureEvent($event);
}

/**
* {@inheritdoc}
*/
Expand Down
27 changes: 0 additions & 27 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,30 +268,3 @@ 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);
}

/**
* @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);
}

/**
* @param int|float $value
* @param string[] $tags
*/
function metricsSet(string $key, $value, array $tags): ?EventId
{
return SentrySdk::getCurrentHub()->metricsSet($key, $value, $tags);
}
12 changes: 12 additions & 0 deletions src/functions_experimental.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Sentry;

use Sentry\Metrics\Metrics;

function metrics(): Metrics
{
return Metrics::getInstance();

Check warning on line 11 in src/functions_experimental.php

View check run for this annotation

Codecov / codecov/patch

src/functions_experimental.php#L11

Added line #L11 was not covered by tests
}

0 comments on commit 03bfef7

Please sign in to comment.