# getsentry/sentry-laravel architecture

> Architecture diagram of the GitHub repository getsentry/sentry-laravel, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/getsentry/sentry-laravel
- Repository: https://github.com/getsentry/sentry-laravel

## Overview

##### Project Type and Purpose
- This is a Laravel integration package that enables seamless error/event capturing and monitoring with Sentry.
- Its purpose is to integrate with the Laravel framework to capture exceptions, log errors, and trace transactions through various integrations (HTTP, queue, cache, storage, etc.) and report them to Sentry’s external monitoring service.

##### Analyzing the File Structure
- Core integration files (e.g., BaseServiceProvider, ServiceProvider)
- Features (e.g., CacheIntegration, HttpClientIntegration, QueueIntegration, etc.)
- HTTP-related components including middlewares (FlushEventsMiddleware, SetRequestMiddleware, etc.)
- Tracing components (under the Tracing directory) that handle detailed tracing of HTTP requests and routing.
- Test files in the “test” directory mirror many of these components, ensuring that each integration (logging, event handling, route integrations, etc.) is properly validated.
- There are several configuration and script files for builds, releases, and code quality (e.g., phpunit.xml, phpstan.neon, scripts) that support maintaining the package.

##### Insights from the README
- The README describes how to install and set up the package within a Laravel application, including modifications to bootstrap files and route declarations.
- It shows that the package hooks into “routes” defined in the Laravel app (using routes such as web, commands, and health endpoints) to capture exceptions from the controller/routes layer.
- The instructions also mention setting up the SDK via a published config file (config/sentry.php) which then drives the behavior of the SDK at runtime.

##### Main Components and Their Relationships
- Laravel Application Core:
- The “routes” defined by the user play a crucial role since the SDK integrates into the Laravel lifecycle. Routes (web endpoints, command routes, etc.) eventually trigger controllers and middlewares.
- Service Providers:
- The ServiceProvider classes (e.g., BaseServiceProvider.php, ServiceProvider.php, and Tracing/ServiceProvider.php) are registered during the Laravel bootstrap process. They hook into the framework to register integrations, event listeners, and middlewares.
- HTTP Middleware & Request Handling:
- Middleware components like FlushEventsMiddleware, SetRequestIpMiddleware, and SetRequestMiddleware intercept incoming requests – including requests routed via defined Laravel routes—to capture context and exceptions.
- Feature Modules:
- The Features directory contains modular components (e.g., CacheIntegration, NotificationsIntegration) that extend the SDK’s capabilities based on application usage patterns. They may hook into different parts of the Laravel application (including routes via HTTP controllers) to capture and dispatch events.
- Integration & Custom Commands:
- Console commands found under src/Sentry/Laravel/Console (like TestCommand, PublishCommand) enhance the developer experience by allowing testing and publishing of integration settings.
- External Interaction:
- Once exceptions or events are captured, the SDK sends the data to Sentry servers, effectively linking your Laravel route-triggered requests (and other processes) with the Sentry monitoring platform.

## Diagram (Mermaid)

```mermaid
flowchart TD
  %% Laravel Application Layer
  subgraph "Laravel Application Layer"
    A1["Application Routes"]:::laravel
    subgraph "HTTP Middleware"
      M1["FlushEventsMiddleware"]:::laravel
      M2["SetRequestMiddleware"]:::laravel
      M3["SetRequestIpMiddleware"]:::laravel
    end
    A2["Laravel Controllers"]:::laravel
  end

  %% Sentry SDK Integration Layer
  subgraph "Sentry SDK Integration Layer"
    subgraph "Service Providers"
      SP1["BaseServiceProvider"]:::sentry
      SP2["ServiceProvider"]:::sentry
      SP3["Tracing ServiceProvider"]:::sentry
    end
    subgraph "Feature Modules"
      FM1["CacheIntegration"]:::sentry
      FM2["HttpClientIntegration"]:::sentry
      FM3["QueueIntegration"]:::sentry
      FM4["NotificationsIntegration"]:::sentry
    end
    subgraph "Console Commands"
      CC1["AboutCommandIntegration"]:::sentry
      CC2["PublishCommand"]:::sentry
      CC3["TestCommand"]:::sentry
    end
    subgraph "Event Handling & Exception Reporting"
      EH1["EventHandler"]:::sentry
      EH2["ExceptionContextIntegration"]:::sentry
      EH3["LaravelContextIntegration"]:::sentry
    end
    subgraph "Tracing Components"
      T1["Tracing Middleware"]:::sentry
      T2["TracingRoutingDispatcher"]:::sentry
    end
    SC["SDK Configuration"]:::sentry
  end

  %% External Sentry Server
  ES["External Sentry Server"]:::external

  %% Connections in Laravel Layer
  A1 --> M1
  M1 --> M2
  M2 --> M3
  M3 --> A2

  %% Connections from Laravel Controllers to Service Providers
  A2 --> SP1
  A2 --> SP2
  A2 --> SP3

  %% SDK Configuration flows into Service Providers
  SC --> SP1
  SC --> SP2
  SC --> SP3

  %% Service Providers forward to Event Handling
  SP1 --> EH1
  SP2 --> EH1
  SP3 --> EH1

  %% Feature Modules contribute to Event Handling
  FM1 --> EH1
  FM2 --> EH1
  FM3 --> EH1
  FM4 --> EH1

  %% Console Commands contribute to Event Handling
  CC1 --> EH1
  CC2 --> EH1
  CC3 --> EH1

  %% Event Handling processes and reports errors
  EH1 --> EH2
  EH1 --> EH3
  EH2 --> ES
  EH3 --> ES

  %% Tracing pathway from Service Providers to External Sentry
  SP3 --> T1
  T1 --> T2
  T2 --> ES

  %% Class definitions for colours
  classDef laravel fill:#cce5ff,stroke:#004085,stroke-width:2px;
  classDef sentry fill:#d4edda,stroke:#155724,stroke-width:2px;
  classDef external fill:#fff3cd,stroke:#856404,stroke-width:2px;

  %% Click Events for Service Providers
  click SP1 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/BaseServiceProvider.php"
  click SP2 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/ServiceProvider.php"
  click SP3 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Tracing/ServiceProvider.php"

  %% Click Events for HTTP Middleware
  click M1 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Http/FlushEventsMiddleware.php"
  click M2 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Http/SetRequestMiddleware.php"
  click M3 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Http/SetRequestIpMiddleware.php"

  %% Click Events for Feature Modules
  click FM1 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Features/CacheIntegration.php"
  click FM2 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Features/HttpClientIntegration.php"
  click FM3 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Features/QueueIntegration.php"
  click FM4 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Features/NotificationsIntegration.php"

  %% Click Events for Console Commands
  click CC1 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Console/AboutCommandIntegration.php"
  click CC2 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Console/PublishCommand.php"
  click CC3 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Console/TestCommand.php"

  %% Click Events for Event Handling & Exception Reporting
  click EH1 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/EventHandler.php"
  click EH2 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Integration/ExceptionContextIntegration.php"
  click EH3 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Integration/LaravelContextIntegration.php"

  %% Click Events for Tracing Components
  click T1 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Tracing/Middleware.php"
  click T2 "https://github.com/getsentry/sentry-laravel/blob/master/src/Sentry/Laravel/Tracing/Routing/TracingRoutingDispatcher.php"

  %% Click Event for SDK Configuration
  click SC "https://github.com/getsentry/sentry-laravel/blob/master/config/sentry.php"
```
