amarc-sudo/sentry-enhanced-tracing
Installation:
composer require amarc-sudo/sentry-enhanced-tracing lexik/jwt-authentication-bundle
Add to config/bundles.php:
AmarcSudo\SentryEnhancedTracing\SentryEnhancedTracingBundle::class => ['all' => true],
Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle::class => ['all' => true],
Configure Sentry in config/packages/sentry.yaml:
sentry:
dsn: '%env(SENTRY_DSN)%'
options:
traces_sample_rate: 1.0
profiles_sample_rate: 1.0
First Use Case: Call any API endpoint or trigger a Symfony event. Check Sentry for:
kernel.request, kernel.controller)Automatic Span Capture:
// No manual code needed—spans auto-capture via listeners.
$user = $em->getRepository(User::class)->find($id); // Auto-traced DB query
Event Phase Tracking:
kernel.request, kernel.controller, etc.).config/packages/sentry_enhanced_tracing.yaml:
sentry_enhanced_tracing:
tracked_events:
- 'kernel.request'
- 'kernel.controller'
User Context Enrichment:
EnhancedUserInterface for richer user data:
class User implements EnhancedUserInterface {
public function getEnhancedFirstname(): ?string { return $this->firstname; }
// ... other methods
}
lexik_jwt_authentication.on_jwt_authenticated.Messenger Queue Monitoring:
queue.publish spans.queue.process transactions.SentryTraceStamp:
# config/services.yaml
parameters:
sentry_enhanced_tracing.messenger.propagate_user: true
Performance Thresholds:
fast/slow based on custom thresholds:
sentry_enhanced_tracing:
performance_thresholds:
kernel.controller: 0.1 # 100ms threshold
Sentry\Tracing\Span methods like setData() to add custom metadata:
$span->setData(['custom_key', 'value']);
traces_sample_rate in production (e.g., 0.1).# config/packages/sentry_enhanced_tracing.yaml
sentry_enhanced_tracing:
excluded_spans:
- 'db.query:SELECT * FROM logs'
Missing Spans:
traces_sample_rate set to 0 or Sentry DSN misconfigured.sentry.yaml and check Sentry dashboard for transactions.Performance Overhead:
traces_sample_rate (e.g., 1.0) in production.0.1) and monitor with Sentry\PerformanceMonitor.Broken Hierarchy:
Sentry\State\HubInterface.JWT User Context Missing:
LexikJWTAuthenticationBundle not installed or misconfigured.Messenger Traces Not Linked:
SentryTraceStamp not propagated between producer/consumer.propagate_user is true and SentryTraceStamp is attached to messages.Log Listener Execution:
Add debug logs to SentryListenerPhasesTracer to verify event phases:
$this->logger->debug('Tracking event phase: ' . $eventName);
Check Span Hierarchy: Use Sentry’s "Transaction Details" to inspect nested spans. Look for:
kernel.controller) with child spans (e.g., db.query).Override Default Behavior: Extend the bundle’s services to customize span creation:
# config/services.yaml
services:
AmarcSudo\SentryEnhancedTracing\Listener\SentryListenerPhasesTracer:
arguments:
$customSpanFactory: '@app.custom.span_factory'
Custom Span Factories: Create a service to modify span creation logic:
class CustomSpanFactory implements SpanFactoryInterface {
public function createSpan(string $name, ?Span $parent = null): Span {
$span = parent::createSpan($name, $parent);
$span->setData(['custom_metadata', 'value']);
return $span;
}
}
Event Phase Extensions:
Add new event phases by extending SentryListenerPhasesTracer:
class CustomPhaseTracer extends SentryListenerPhasesTracer {
protected function getTrackedEvents(): array {
return array_merge(parent::getTrackedEvents(), ['custom.event']);
}
}
Messenger Integration:
Extend SentryMessengerListener to add custom metadata:
class CustomMessengerListener extends SentryMessengerListener {
public function handle(DispatcherInterface $dispatcher, callable $next, Envelope $envelope): Envelope {
$envelope = parent::handle($dispatcher, $next, $envelope);
$this->addCustomMetadata($envelope);
return $envelope;
}
}
tracked_events) may be needed for edge cases.99999 by default).capture_email in user_context if handling sensitive data:
sentry_enhanced_tracing:
user_context:
capture_email: false
if (!$this->isTracingEnabled()) {
return;
}
max_breadcrumbs in Sentry config to reduce payload size:
sentry:
options:
max_breadcrumbs: 50
How can I help you explore Laravel packages today?