|
2 | 2 |
|
3 | 3 | namespace Naoray\LaravelGithubMonolog; |
4 | 4 |
|
| 5 | +use Illuminate\Contracts\Config\Repository; |
| 6 | +use Illuminate\Support\Facades\Context; |
5 | 7 | use Illuminate\Support\Facades\Event; |
6 | 8 | use Illuminate\Support\ServiceProvider; |
7 | 9 | use Naoray\LaravelGithubMonolog\Tracing\EventHandler; |
| 10 | +use Naoray\LaravelGithubMonolog\Tracing\LivewireDataCollector; |
| 11 | +use Naoray\LaravelGithubMonolog\Tracing\UserDataCollector; |
8 | 12 |
|
9 | 13 | class GithubMonologServiceProvider extends ServiceProvider |
10 | 14 | { |
| 15 | + public function register(): void |
| 16 | + { |
| 17 | + $this->mergeConfigFrom(__DIR__.'/../config/github-monolog.php', 'github-monolog'); |
| 18 | + $this->registerLogger(); |
| 19 | + $this->registerTracingDefaults(); |
| 20 | + $this->registerContextDehydration(); |
| 21 | + } |
| 22 | + |
11 | 23 | public function boot(): void |
12 | 24 | { |
13 | | - $config = config('logging.channels.github.tracing'); |
| 25 | + $this->registerTracingSubscribers(); |
| 26 | + $this->registerLivewireHooks(); |
| 27 | + $this->registerPublishables(); |
| 28 | + $this->registerOctaneListeners(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Register the github logging channel if enabled. |
| 33 | + * |
| 34 | + * This follows Nightwatch's pattern of auto-registering the logging channel |
| 35 | + * without requiring explicit configuration. |
| 36 | + */ |
| 37 | + protected function registerLogger(): void |
| 38 | + { |
| 39 | + /** @var Repository $config */ |
| 40 | + $config = $this->app->make(Repository::class); |
| 41 | + |
| 42 | + // Check if github-monolog is enabled (defaults to true for zero-config) |
| 43 | + if (! $config->get('github-monolog.enabled', true)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // Auto-register the github logging channel if not already configured |
| 48 | + if (! $config->has('logging.channels.github')) { |
| 49 | + $config->set('logging.channels.github', [ |
| 50 | + 'driver' => 'custom', |
| 51 | + 'via' => GithubIssueHandlerFactory::class, |
| 52 | + 'repo' => $config->get('github-monolog.repo'), |
| 53 | + 'token' => $config->get('github-monolog.token'), |
| 54 | + 'labels' => $config->get('github-monolog.labels', []), |
| 55 | + 'level' => $config->get('github-monolog.level', 'error'), |
| 56 | + 'signature_generator' => $config->get('github-monolog.signature_generator'), |
| 57 | + 'deduplication' => $config->get('github-monolog.deduplication', []), |
| 58 | + 'buffer' => $config->get('github-monolog.buffer', []), |
| 59 | + 'tracing' => $config->get('github-monolog.tracing', []), |
| 60 | + ]); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Register zero-config defaults for tracing. |
| 66 | + * |
| 67 | + * This follows Nightwatch's pattern of setting sensible defaults |
| 68 | + * without requiring explicit configuration. |
| 69 | + */ |
| 70 | + protected function registerTracingDefaults(): void |
| 71 | + { |
| 72 | + /** @var Repository $config */ |
| 73 | + $config = $this->app->make(Repository::class); |
14 | 74 |
|
15 | | - if (isset($config['enabled']) && $config['enabled']) { |
| 75 | + // Skip if github-monolog is disabled |
| 76 | + if (! $config->get('github-monolog.enabled', true)) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // Skip if github channel doesn't exist (user may have disabled auto-registration) |
| 81 | + if (! $config->has('logging.channels.github')) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + // Merge tracing config from github-monolog.tracing into logging.channels.github.tracing |
| 86 | + // This ensures users can configure tracing in either location |
| 87 | + $packageTracingConfig = $config->get('github-monolog.tracing', []); |
| 88 | + $channelTracingConfig = $config->get('logging.channels.github.tracing', []); |
| 89 | + |
| 90 | + // Package config takes precedence over channel config |
| 91 | + $mergedConfig = array_merge($channelTracingConfig, $packageTracingConfig); |
| 92 | + |
| 93 | + $config->set('logging.channels.github.tracing', $mergedConfig); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Register tracing event subscribers. |
| 98 | + */ |
| 99 | + protected function registerTracingSubscribers(): void |
| 100 | + { |
| 101 | + // Check if package is enabled |
| 102 | + if (! config('github-monolog.enabled', true)) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + // Check tracing config from either location (package config takes precedence) |
| 107 | + $tracingEnabled = config('github-monolog.tracing.enabled') |
| 108 | + ?? config('logging.channels.github.tracing.enabled', true); |
| 109 | + |
| 110 | + if ($tracingEnabled) { |
16 | 111 | Event::subscribe(EventHandler::class); |
17 | 112 | } |
| 113 | + } |
18 | 114 |
|
| 115 | + /** |
| 116 | + * Register Livewire hooks if Livewire is available. |
| 117 | + * |
| 118 | + * This follows Nightwatch's pattern of registering Livewire hooks |
| 119 | + * directly in the service provider. |
| 120 | + */ |
| 121 | + protected function registerLivewireHooks(): void |
| 122 | + { |
| 123 | + // Check if package is enabled |
| 124 | + if (! config('github-monolog.enabled', true)) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + // Check if Livewire tracing is enabled (package config takes precedence) |
| 129 | + $livewireEnabled = config('github-monolog.tracing.livewire') |
| 130 | + ?? config('logging.channels.github.tracing.livewire', true); |
| 131 | + |
| 132 | + if (! $livewireEnabled) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + $this->app->booted(static function ($app) { |
| 137 | + // Check if Livewire is available |
| 138 | + if (! class_exists('Livewire\Livewire')) { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + if (! $app->bound('Livewire\LivewireManager')) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + $collector = new LivewireDataCollector; |
| 147 | + |
| 148 | + if (! $collector->isEnabled()) { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + // Use call_user_func to avoid static analysis issues with optional dependencies |
| 153 | + // Livewire 2: component.hydrate.subsequent |
| 154 | + call_user_func(['Livewire\Livewire', 'listen'], 'component.hydrate.subsequent', fn ($component) => rescue( |
| 155 | + fn () => $collector->componentHydrateSubsequent($component) |
| 156 | + )); |
| 157 | + |
| 158 | + // Livewire 3: hydrate |
| 159 | + call_user_func(['Livewire\Livewire', 'listen'], 'hydrate', fn ($component) => rescue( |
| 160 | + fn () => $collector->hydrate($component) |
| 161 | + )); |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Register publishable assets. |
| 167 | + */ |
| 168 | + protected function registerPublishables(): void |
| 169 | + { |
19 | 170 | if ($this->app->runningInConsole()) { |
| 171 | + $this->publishes([ |
| 172 | + __DIR__.'/../config/github-monolog.php' => config_path('github-monolog.php'), |
| 173 | + ], 'github-monolog-config'); |
| 174 | + |
20 | 175 | $this->publishes([ |
21 | 176 | __DIR__.'/../resources/views' => resource_path('views/vendor/github-monolog'), |
22 | 177 | ], 'github-monolog-views'); |
23 | 178 | } |
24 | 179 | } |
| 180 | + |
| 181 | + /** |
| 182 | + * Register context dehydration callback to prevent large tracing data from being serialized into job payloads. |
| 183 | + */ |
| 184 | + protected function registerContextDehydration(): void |
| 185 | + { |
| 186 | + if (! method_exists(Context::class, 'dehydrating')) { |
| 187 | + return; |
| 188 | + } |
| 189 | + |
| 190 | + Context::dehydrating(function ($context) { |
| 191 | + foreach (['queries', 'outgoing_requests', 'session', 'request'] as $key) { |
| 192 | + if ($context->has($key)) { |
| 193 | + $context->forget($key); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + foreach (array_keys($context->all()) as $key) { |
| 198 | + if (str_starts_with($key, 'outgoing_request.')) { |
| 199 | + $context->forget($key); |
| 200 | + } |
| 201 | + } |
| 202 | + }); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Register Octane listeners for proper state management. |
| 207 | + */ |
| 208 | + protected function registerOctaneListeners(): void |
| 209 | + { |
| 210 | + if (! class_exists(\Laravel\Octane\Events\RequestReceived::class)) { |
| 211 | + return; |
| 212 | + } |
| 213 | + |
| 214 | + // Flush user data between requests in Octane |
| 215 | + Event::listen(\Laravel\Octane\Events\RequestReceived::class, function () { |
| 216 | + UserDataCollector::flush(); |
| 217 | + }); |
| 218 | + } |
25 | 219 | } |
0 commit comments