Observability and operations

This section details the one-off Sentry report export command, which retrieves recent production issues via the Sentry API and writes them to a CSV. It aids in incident review and triage by surfacing key metrics (ID, link, title, culprit, last seen, count).

Prerequisites

  • Environment Variable
    Ensure SENTRY_API_KEY is set in your .env.
  • Configuration
    The API key is loaded from config/services.php:
'sentry' => [
    'api_key' => env('SENTRY_API_KEY'),
],

Command Overview

Property Detail
Signature sentry:generate-report
Description Export recent production issues to sentry-report.csv
Namespace App\Console\Commands\oneoffs
Location app/Console/Commands/oneoffs/GenerateSentryReport.php

Usage

Run from the project root:

php artisan sentry:generate-report

Workflow

flowchart TD
    A[Start command<br>`handle()`] --> B{API key set?}
    B -- No --> C[Error: "SENTRY_API_KEY is not set"]
    B -- Yes --> D[Determine date range<br>(last 30 days)]
    D --> E[getEvents(start, end)]
    E --> F[Loop pages:<br>makeRequest → parseResponse → collect]
    F --> G[Format & sort events]
    G --> H[Open `sentry-report.csv` for writing]
    H --> I[Write headers & rows]
    I --> J[Close file]
    J --> K[Info: success messages]

Key Methods

  • handle()

    • Loads the API key.
    • Computes startDate and endDate (last 30 days).
    • Calls getEvents(), transforms and sorts the results.
    • Writes sentry-report.csv with headers:
      ID, Link, Title, Culprit, Last Seen, Count.
  • getEvents(string $startDate, string $endDate)

    • Builds the Sentry API URL for production issues.
    • Paginates via the Link header until no next link remains.
    • Merges all pages into a single Collection.
  • makeRequest(string $url)

    • Sends an HTTP GET with Authorization: Bearer <apiKey>.
    • Uses the Http facade for simplicity and retry behavior.
  • parseResponse($response)

    • Converts JSON to a collection, filtering out issues not seen within the last 30 days.
  • parseLinkHeader(array $headers)

    • Extracts the next page URL when rel="next" and results="true".
    • Returns null when pagination ends.

CSV Output

Column Description
ID Sentry issue identifier
Link Direct URL to the issue in Sentry
Title Issue title
Culprit Code path most responsible for the error
Last Seen Date of the most recent occurrence (MM/DD/YYYY)
Count Total times the issue occurred

The file is saved as:

sentry-report.csv

in the project root upon successful execution.

Best Practices 💡

{
    "title": "Secure API Key",
    "content": "Rotate and scope your SENTRY_API_KEY. Avoid committing it to VCS."
}
  • Regularly run this export during on-call reviews.
  • Archive or attach the CSV to incident tickets.
  • Combine with Slack or email reminders for automated workflows.

By integrating this command into your operational toolkit, teams gain a consistent, up-to-date snapshot of error trends, improving response time and root-cause analysis.