Skip to content

Add Barstool::context() for storing custom context on recordings - #13

Merged
craigpotter merged 1 commit into
mainfrom
feature/context-support
Jul 17, 2026
Merged

Add Barstool::context() for storing custom context on recordings#13
craigpotter merged 1 commit into
mainfrom
feature/context-support

Conversation

@craigpotter

Copy link
Copy Markdown
Collaborator

Overview of change

Adds the ability to attach custom context to Barstool recordings, stored in a new nullable JSON context column:

use Saloon\Barstool\Barstool;

Barstool::context([
    'user_id' => auth()->id(),
    'tenant_id' => $tenant->id,
]);

// Or a single key:
Barstool::addContext('job', 'user-sync');

Every request recorded after this point stores the context on its row. getContext() and flushContext() are also available.

Implementation notes

  • Context is stored as hidden data on Laravel's Context (Context::addHidden) under a namespaced key. This means it is carried into queued jobs automatically (set context in a controller, and API calls made inside a dispatched job still record it), it is reset between requests/workers by the framework, and it never leaks into the application's log context.
  • The persist payload only includes the context key when context has actually been set. Existing installs can upgrade the package without running the new migration and nothing breaks — the column is only referenced once you opt in to the feature. This makes it a safe minor release.
  • New add_context_to_barstools_table migration for existing installs (guarded with hasColumn, so it is a no-op on fresh installs where the create migration already includes the column).

Related

Testing

  • Six new Pest tests covering: context recorded on the row, merge/overwrite/flush semantics, null column and absent payload key when unset, context in queued payloads, and the UUID read-back correlation pattern.
  • composer test (25 passed), composer analyse (PHPStan level 8, clean), composer format (clean).

Allows attaching arbitrary key/value context (user IDs, tenant IDs, etc.)
to recorded requests, stored in a new nullable JSON `context` column.
Context is held as hidden data on Laravel's Context so it carries into
queued jobs and resets between requests/workers automatically.

The persist payload only includes the context key when context has been
set, so upgrading without running the new migration is safe until the
feature is used.

Closes #7
@craigpotter
craigpotter merged commit 2344fa0 into main Jul 17, 2026
8 checks passed
@craigpotter
craigpotter deleted the feature/context-support branch July 17, 2026 22:18
craigpotter added a commit that referenced this pull request Jul 17, 2026
# Overview of change

Documentation follow-up from #13/#15: the README previously documented
queue support and the newer features but almost none of the config
options. This PR:

- Groups all configuration docs under a single **Configuration** section
with subsections: enabling & disabling, choosing what gets recorded
(`only`/`ignore`), keeping only failed responses, redacting sensitive
request headers, excluding response bodies, response body limits,
database connection, pruning, and queue support
- Adds a short **Requirements** section (PHP 8.3+, Laravel 12+, Saloon
v4)
- Documents the placeholder values users see in the `barstools` table
(`REDACTED`, `<Streamed Body>`, `<Multipart Body>`, `<Unsupported
Barstool Response Content>`) and when each appears
- Links the pruning setup from Installation instead of duplicating it
there
- Minor copy fixes, keeping the existing tone

One non-README change: the `enabled` config comment mentioned a UI that
doesn't exist, so that mention is removed.

# Testing

Docs only (plus a config comment) — full suite still green locally (31
passed, 154 assertions), Pint clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: is it possible to store some context alongside the request data ( eg. current auth user, ... )

1 participant