Skip to content

Add ability to supply X-Barstool-UUID in a request header - #6

Closed
allmanaj wants to merge 2 commits into
saloonphp:mainfrom
allmanaj:pass-in-x-barstool-uuid
Closed

Add ability to supply X-Barstool-UUID in a request header#6
allmanaj wants to merge 2 commits into
saloonphp:mainfrom
allmanaj:pass-in-x-barstool-uuid

Conversation

@allmanaj

@allmanaj allmanaj commented May 13, 2026

Copy link
Copy Markdown

Overview of change

This PR adds the ability to send your own custom X-Barstool-UUID in the header of a Saloon request and to have that be the UUID stored against the row in the barstools table.

Reasoning

I have started using Barstool at work to provide visibility of our requests since the codebase relies heavily on calls to external APIs. Barstool is ideal for this, but we have an instance where we want to store more specific data about a certain repeated job and how to connects to other models in our codebase. By checking for the existence of the X-Barstool-UUID header before the package generates a new UUID, it allows us to supply a UUID from our codebase which can be stored on the more detailed log model. This allows us to associate our own log with the barstool record without clogging up the barstools table.

Example Implementation

$barstoolId = Str::uuid();
$request = new RequestWhichAcceptsCustomHeaders(headers: ['X-Barstool-UUID' => $barstoolId]);
$response = $request->send();
if($response->failed()) {
    LogModelWithAddedContext::create([
        'user_id' => auth()->id,
        'barstool_id' => $barstoolId,
    ]);
}

@craigpotter craigpotter left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey

Thanks for this.
One small suggestion please.

Comment thread src/Barstool.php
Comment on lines +129 to +135
$uuid = $data->headers()->get('X-Barstool-UUID');

$data->headers()->add('X-Barstool-UUID', $uuid);
if (is_null($uuid)) {
$uuid = Str::uuid()->toString();

$data->headers()->add('X-Barstool-UUID', $uuid);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$uuid = $data->headers()->get('X-Barstool-UUID');
$data->headers()->add('X-Barstool-UUID', $uuid);
if (is_null($uuid)) {
$uuid = Str::uuid()->toString();
$data->headers()->add('X-Barstool-UUID', $uuid);
}
$uuid = $data->headers()->get('X-Barstool-UUID', Str::uuid()->toString());
$data->headers()->add('X-Barstool-UUID', $uuid);

@craigpotter

Copy link
Copy Markdown
Collaborator

Hey @allmanaj — thanks again for this PR and sorry for the long silence on it. Having sat with it a while, I've decided not to merge the header override: letting callers set the UUID makes a user-supplied value the updateOrCreate key (a reused UUID would silently merge unrelated recordings into one row), and it turns the internal X-Barstool-UUID header into public API I'd have to preserve through upcoming refactors.

The good news is your use case works today without any changes — the generated UUID is readable straight off the sent request: $response->getPendingRequest()->headers()->get('X-Barstool-UUID'). #13 adds a test locking that in and documents it in the README ("Correlating Barstool records with your own models"). It also adds Barstool::context() for storing extra data (user IDs, etc.) directly on the recording, which may cover the other half of what you were after.

Thanks for using Barstool and for pushing on this — it directly shaped both additions. 🍻

craigpotter added a commit that referenced this pull request Jul 17, 2026
# Overview of change

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

```php
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

- Closes #7 — context (auth user, tenant, etc.) can now be stored
alongside the request data.
- Complements the discussion on #6 — the README now documents the UUID
read-back pattern for correlating your own models with a Barstool
recording, and there is a test locking that behaviour in.

# 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).
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.

2 participants