Add ability to supply X-Barstool-UUID in a request header - #6
Conversation
Also wrote a happy-path test for this
craigpotter
left a comment
There was a problem hiding this comment.
Hey
Thanks for this.
One small suggestion please.
| $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); | ||
| } |
There was a problem hiding this comment.
| $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); |
|
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 The good news is your use case works today without any changes — the generated UUID is readable straight off the sent request: Thanks for using Barstool and for pushing on this — it directly shaped both additions. 🍻 |
# 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).
Overview of change
This PR adds the ability to send your own custom
X-Barstool-UUIDin the header of a Saloon request and to have that be the UUID stored against the row in thebarstoolstable.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-UUIDheader 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 thebarstoolstable.Example Implementation