Skip to content

Conversation

@dwjordan
Copy link
Contributor

Summary

When the Nightwatch middleware is added to a connector that is reused across multiple requests, the middleware is registered repeatedly on subsequent requests. This results in duplicate middleware execution and inflated Nightwatch metrics.

Problem

The Nightwatch plugin registers its middleware via addMiddleware on the Guzzle handlerStack, which does not de-duplicate entries. When a Connector instance is reused, the Nightwatch middleware is appended again on each request, causing the middleware to run multiple times per request lifecycle.

Because Nightwatch is intended to observe outgoing requests once per execution, this leads to significant oversampling.

Fix

Before registering the Nightwatch middleware, remove any existing middleware tagged as nightwatch from the handler stack. This ensures the middleware is only applied once, even when connectors are reused.

Tests

  • Added a regression test that reproduces the middleware duplication when reusing a connector.
  • The test fails on main and passes with this change.
  • Verified that the Nightwatch middleware executes only once per request across multiple uses of the same connector instance.

@JonPurvis
Copy link
Collaborator

Thanks @dwjordan, this makes sense.

Please could you run ./vendor/bin/php-cs-fixer fix --allow-risky=yes and push the change(s) up? That action only runs if Sam does it!

@JonPurvis
Copy link
Collaborator

Speedy! 🚀

Thanks! I've got a call with Sam later tonight so we'll go through this PR

@dwjordan
Copy link
Contributor Author

Sounds good, thanks!

@Sammyjo20
Copy link
Member

Fantastic PR thanks @dwjordan and @JonPurvis for pairing! 🍐

@Sammyjo20 Sammyjo20 merged commit 017ecb8 into saloonphp:v3 Dec 18, 2025
40 of 66 checks passed
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.

3 participants