Skip to content

Commit

Permalink
Add log driver for heartbeats
Browse files Browse the repository at this point in the history
  • Loading branch information
ttrig committed Jul 30, 2024
1 parent b376e9f commit 9fe9125
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Log driver for heartbeats.

## [0.6.0] - 2024-05-28
### Changed
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ heartbeat('foo bar'); // POST http://heartbeat.localhost/foo-bar/1
heartbeat('foo baz', 5); // POST http://heartbeat.localhost/foo-baz/5
```

### Log driver

To prevent HTTP requests in local environment, set `butler.health.heartbeat.driver` to "log".

### Fake

Instead of faking the laravel [Http client](https://laravel.com/docs/master/http-client) in your tests you can fake heartbeats, see example below.
Expand Down
1 change: 1 addition & 0 deletions config/butler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'checks' => [],

'heartbeat' => [
'driver' => env('BUTLER_HEALTH_HEARTBEAT_DRIVER', 'http'),
'url' => env('BUTLER_HEALTH_HEARTBEAT_URL'),
'token' => env('BUTLER_HEALTH_HEARTBEAT_TOKEN'),
'report' => env('BUTLER_HEALTH_HEARTBEAT_REPORT'),
Expand Down
31 changes: 20 additions & 11 deletions src/Heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Traits\Macroable;
use PHPUnit\Framework\Assert as PHPUnit;

Expand All @@ -30,18 +31,26 @@ public function send(string $slug, int $minutes = 1): void

if ($this->recording) {
$this->recorded[] = $url;
} else {
$config = config('butler.health.heartbeat');

Http::withToken($config['token'] ?? null)
->timeout($config['timeout'] ?? 5)
->acceptJson()
->post($url)
->onError(fn ($response) => report_if(
$config['report'] ?? false,
$response->toException(),
));

return;
}

$config = config('butler.health.heartbeat');

if ($config['driver'] ?? null === 'log') {
Log::info('heartbeat', ['url' => $url]);

return;
}

Http::withToken($config['token'] ?? null)
->timeout($config['timeout'] ?? 5)
->acceptJson()
->post($url)
->onError(fn ($response) => report_if(
$config['report'] ?? false,
$response->toException(),
));
}

public function assertSent(string $slug, int $minutes = 1): void
Expand Down
12 changes: 12 additions & 0 deletions tests/HeartbeatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use PHPUnit\Framework\ExpectationFailedException;

class HeartbeatTest extends AbstractTestCase
Expand Down Expand Up @@ -120,4 +121,15 @@ public function test_recorded()
$this->assertInstanceOf(Collection::class, $recorded);
$this->assertEquals(1, $recorded->count());
}

public function test_log_driver()
{
config(['butler.health.heartbeat.driver' => 'log']);

Log::expects('info')->with('heartbeat', ['url' => 'http://localhost/foobar/2']);

Heartbeat::send('foobar', 2);

Http::assertNothingSent();
}
}

0 comments on commit 9fe9125

Please sign in to comment.