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
13 changes: 13 additions & 0 deletions src/Drivers/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ public function formatted(): mixed
return null;
}

protected function flushMetricLogs(): void
{
foreach ($this->metrics as $metric) {
if ($metric->getLogMessage()) {
app('log')->log(
$metric->getLogLevel(),
$metric->getLogMessage(),
array_merge(['event' => $metric->getName()], $metric->getExtra())
);
}
}
}

abstract public function format(Metric $metric);

abstract public function flush(): static;
Expand Down
2 changes: 2 additions & 0 deletions src/Drivers/CloudWatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function flush(): static
return $this;
}

$this->flushMetricLogs();

$this->send($this->getMetrics());

$this->metrics = [];
Expand Down
2 changes: 2 additions & 0 deletions src/Drivers/InfluxDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public function flush(): static
return $this;
}

$this->flushMetricLogs();

$this->send($this->getMetrics());
$this->metrics = [];

Expand Down
2 changes: 2 additions & 0 deletions src/Drivers/LogDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function flush(): static
return $this;
}

$this->flushMetricLogs();

$formatted = array_map([$this, 'format'], $this->getMetrics());

$this->logger->info("Metrics", $formatted);
Expand Down
26 changes: 11 additions & 15 deletions src/Drivers/PostHog.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,20 @@ public function __construct(protected string $distinctPrefix = '')
{
}

/**
* Note we are NOT enqueueing metrics on our own with PostHog. It queues internally
* and batches sends as it sees fit. Our best bet is to let PostHog do its thing.
* @throws Exception
*/
public function add(Metric $metric): static
public function flush(): static
{
PostHogClient::capture($this->format($metric));
if (empty($this->getMetrics())) {
return $this;
}

return $this;
}
$this->flushMetricLogs();

foreach ($this->getMetrics() as $metric) {
PostHogClient::capture($this->format($metric));
}

$this->metrics = [];

/**
* PostHog sends batches automatically on __destruct, this really isn't necessary.
* But we're including it in case you ever want to force send earlier on.
*/
public function flush(): static
{
PostHogClient::flush();

return $this;
Expand Down
6 changes: 6 additions & 0 deletions src/Drivers/PrometheusDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public function format(Metric $metric): Collector

public function flush(): static
{
if (empty($this->metrics)) {
return $this;
}

$this->flushMetricLogs();

$this->metrics = [];
$this->registry->wipeStorage();
return $this;
Expand Down
30 changes: 29 additions & 1 deletion src/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class Metric

protected ?string $description = null;

protected ?string $logMessage = null;

protected string $logLevel = 'info';

/**
* Metric constructor.
*
Expand Down Expand Up @@ -149,7 +153,13 @@ public function addTag($key, $value): static

public function getExtra(): array
{
return value($this->extra);
$extra = value($this->extra);

if ($this->logMessage) {
$extra['message'] = $this->logMessage;
}

return $extra;
}

public function setExtra(array|\Closure $extra): static
Expand All @@ -167,6 +177,24 @@ public function addExtra($key, $value): static
return $this;
}

public function withLog(string $message, string $level = 'info'): static
{
$this->logMessage = $message;
$this->logLevel = $level;

return $this;
}

public function getLogMessage(): ?string
{
return $this->logMessage;
}

public function getLogLevel(): string
{
return $this->logLevel;
}

public function getTimestamp(): mixed
{
return $this->timestamp;
Expand Down
51 changes: 51 additions & 0 deletions tests/MetricTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,57 @@ public function testAddExtraAfterClosureSet()
$this->assertEquals(['foo' => 'bar', 'baz' => 'qux'], $metric->getExtra());
}

public function testWithLogStoresMessageAndLevel()
{
$metric = new Metric("my_metric", 1);
$metric->withLog("Something happened", "warning");

$this->assertEquals("Something happened", $metric->getLogMessage());
$this->assertEquals("warning", $metric->getLogLevel());
}

public function testWithLogDefaultsToInfo()
{
$metric = new Metric("my_metric", 1);
$metric->withLog("Something happened");

$this->assertEquals("info", $metric->getLogLevel());
}

public function testWithLogAddsMessageToExtra()
{
$metric = new Metric("my_metric", 1);
$metric->setExtra(['key' => 'value']);
$metric->withLog("Something happened");

$extra = $metric->getExtra();

$this->assertEquals('value', $extra['key']);
$this->assertEquals('Something happened', $extra['message']);
}

public function testWithLogFlushesLog()
{
$this->setupInfluxDB();

$log = Mockery::mock(\Psr\Log\LoggerInterface::class);
$log->shouldReceive('log')
->once()
->with('info', 'Something happened', Mockery::on(function ($context) {
return $context['event'] === 'my_metric'
&& $context['key'] === 'value'
&& $context['message'] === 'Something happened';
}));

app()->instance('log', $log);

Metrics::create("my_metric")
->withLog("Something happened")
->setExtra(['key' => 'value']);

Metrics::flush();
}

public function testGivenTimestampIsntChanged()
{
$metric = new Metric('my_metric', 1);
Expand Down