Skip to content

Commit 9a19a71

Browse files
Capture job attempts in Vapor (#199)
Co-authored-by: Tim MacDonald <[email protected]>
1 parent 74a4b86 commit 9a19a71

10 files changed

+435
-130
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Introduction
44

5-
[Laravel Nightwatch](https://nightwatch.laravel.com) is a hosted application monitoring platform design for Laravel. It delivers unparalleled insights into your application's performance, with the intelligence only a system obsessively optimized for Laravel can deliver.
5+
[Laravel Nightwatch](https://nightwatch.laravel.com) is a hosted application monitoring platform designed for Laravel. It delivers unparalleled insights into your application's performance, with the intelligence only a system obsessively optimized for Laravel can deliver.
66

77
This repository contains the official Laravel Nightwatch package, which gathers metrics within your application and transmits them to Nightwatch. To learn more about Laravel Nightwatch and how to use this package, please consult the [official documentation](https://nightwatch.laravel.com/docs).
88

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"name": "laravel/nightwatch",
33
"description": "The official Laravel Nightwatch package.",
4-
"keywords": ["laravel", "monitoring", "insights"],
4+
"keywords": [
5+
"laravel",
6+
"monitoring",
7+
"insights"
8+
],
59
"homepage": "https://nightwatch.laravel.com",
610
"license": "MIT",
711
"support": {
@@ -29,10 +33,12 @@
2933
},
3034
"require-dev": {
3135
"ext-pdo": "*",
36+
"aws/aws-sdk-php": "^3.349",
3237
"guzzlehttp/guzzle": "^7.0",
3338
"guzzlehttp/psr7": "^2.0",
3439
"laravel/horizon": "^5.4",
3540
"laravel/pint": "1.21.0",
41+
"laravel/vapor-core": "^2.38.2",
3642
"livewire/livewire": "^2.0|^3.0",
3743
"mockery/mockery": "^1.0",
3844
"mongodb/laravel-mongodb": "^4.0|^5.0",

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
<env name="DB_DATABASE" value="tests/database.sqlite" />
2121
<env name="MAIL_MAILER" value="array" />
2222
<env name="QUEUE_CONNECTION" value="database" />
23+
<env name="SQS_PREFIX" value="https://sqs.us-east-1.amazonaws.com/123456789" />
2324
</php>
2425
</phpunit>

src/Compatibility.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use Illuminate\Contracts\Events\Dispatcher;
66
use Illuminate\Contracts\Foundation\Application;
7-
use Illuminate\Log\Context\Repository as Context;
87
use Illuminate\Queue\Events\JobProcessing;
98
use Illuminate\Queue\Queue;
9+
use Illuminate\Support\Facades\Context;
1010
use ReflectionProperty;
1111
use Symfony\Component\Console\Input\ArgvInput;
1212

@@ -17,8 +17,6 @@
1717

1818
final class Compatibility
1919
{
20-
public static Application $app;
21-
2220
public static bool $terminatingEventExists = false;
2321

2422
public static bool $cacheDurationCapturable = false;
@@ -44,7 +42,6 @@ final class Compatibility
4442

4543
public static function boot(Application $app): void
4644
{
47-
self::$app = $app;
4845
$version = $app->version();
4946

5047
/**
@@ -134,10 +131,7 @@ public static function addHiddenContext(string $key, mixed $value): void
134131
return;
135132
}
136133

137-
/** @var Context */
138-
$context = self::$app->make(Context::class);
139-
140-
$context->addHidden($key, $value);
134+
Context::addHidden($key, $value);
141135
}
142136

143137
/**
@@ -150,9 +144,6 @@ public static function getHiddenContext(string $key, mixed $default = null): mix
150144
return self::$context[$key] ?? value($default);
151145
}
152146

153-
/** @var Context */
154-
$context = self::$app->make(Context::class);
155-
156-
return $context->getHidden($key) ?? value($default);
147+
return Context::getHidden($key) ?? value($default);
157148
}
158149
}

src/Concerns/CapturesState.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use function array_shift;
4242
use function array_unshift;
4343
use function debug_backtrace;
44+
use function env;
4445
use function memory_reset_peak_usage;
4546
use function random_int;
4647

@@ -481,6 +482,10 @@ public function prepareForNextJob(): void
481482
*/
482483
public function prepareForJob(Job $job): void
483484
{
485+
if ($this->isVapor()) {
486+
$this->prepareForNextJob();
487+
}
488+
484489
$this->sample(
485490
Compatibility::getHiddenContext('nightwatch_should_sample', true) ? 1.0 : 0.0
486491
);
@@ -611,4 +616,9 @@ public function flush(): void
611616
$this->executionState->flush();
612617
$this->ingest->flush();
613618
}
619+
620+
private function isVapor(): bool
621+
{
622+
return env('VAPOR_SSM_PATH') !== null;
623+
}
614624
}

src/Hooks/CommandStartingListener.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __invoke(CommandStarting $event): void
5151

5252
try {
5353
match ($event->command) {
54-
'queue:work', 'queue:listen', 'horizon:work' => $this->registerJobHooks(),
54+
'queue:work', 'queue:listen', 'horizon:work', 'vapor:work' => $this->registerJobHooks($event),
5555
'schedule:run', 'schedule:work' => $this->registerScheduledTaskHooks(),
5656
default => $this->registerCommandHooks($event),
5757
};
@@ -60,7 +60,7 @@ public function __invoke(CommandStarting $event): void
6060
}
6161
}
6262

63-
private function registerJobHooks(): void
63+
private function registerJobHooks(CommandStarting $event): void
6464
{
6565
$this->nightwatch->configureForJobs();
6666

@@ -86,6 +86,10 @@ private function registerJobHooks(): void
8686
JobReleasedAfterException::class,
8787
JobFailed::class,
8888
], (new JobAttemptListener($this->nightwatch))(...));
89+
90+
if ($event->command === 'vapor:work') {
91+
$this->events->listen(CommandFinished::class, (new VaporWorkCommandFinishedListener($this->nightwatch))(...));
92+
}
8993
}
9094

9195
private function registerScheduledTaskHooks(): void
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Laravel\Nightwatch\Hooks;
4+
5+
use Illuminate\Console\Events\CommandFinished;
6+
use Laravel\Nightwatch\Core;
7+
use Laravel\Nightwatch\State\CommandState;
8+
9+
/**
10+
* @internal
11+
*/
12+
final class VaporWorkCommandFinishedListener
13+
{
14+
/**
15+
* @param Core<CommandState> $nightwatch
16+
*/
17+
public function __construct(
18+
private Core $nightwatch,
19+
) {
20+
//
21+
}
22+
23+
public function __invoke(CommandFinished $event): void
24+
{
25+
$this->nightwatch->digest();
26+
}
27+
}

src/NightwatchServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
use Illuminate\Notifications\Events\NotificationSent;
3333
use Illuminate\Queue\Events\JobQueued;
3434
use Illuminate\Queue\Events\JobQueueing;
35-
use Illuminate\Queue\Queue;
3635
use Illuminate\Routing\Events\PreparingResponse;
3736
use Illuminate\Routing\Events\ResponsePrepared;
3837
use Illuminate\Routing\Events\RouteMatched;

testbench.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
providers:
22
- Laravel\Nightwatch\NightwatchServiceProvider
33
- Laravel\Horizon\HorizonServiceProvider
4+
- Laravel\Vapor\VaporServiceProvider
45
- Livewire\LivewireServiceProvider
56

67
workbench:

0 commit comments

Comments
 (0)