From ea26fb37ec2f5b0e3ec0213287a825d320e62c2c Mon Sep 17 00:00:00 2001 From: Aleksey Tupichenkov Date: Tue, 30 Sep 2025 11:11:29 +0300 Subject: [PATCH 1/2] feat: add optional monolog BaggageProcessor --- README.md | 54 ++++++++++++++++++-------------- src/Monolog/BaggageProcessor.php | 35 +++++++++++++++++++++ 2 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 src/Monolog/BaggageProcessor.php diff --git a/README.md b/README.md index 8367cec..34fa1d5 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ The **SchemaContextBundle** provides a lightweight way to manage dynamic schema - Injects schema and baggage info into Messenger messages via a middleware. - Rehydrates schema and baggage on message consumption via a middleware. - Provide decorator for Http clients to propagate baggage header +- Optional: Adds baggage context to Monolog log records via a processor --- @@ -34,10 +35,10 @@ Add this config to `config/packages/schema_context.yaml`: ```yaml schema_context: - app_name: '%env(APP_NAME)%' # Application name - header_name: 'X-Tenant' # Request header to extract schema name - default_schema: 'public' # Default schema to fallback to - allowed_app_names: ['develop', 'staging', 'test'] # App names where schema context is allowed to change + app_name: '%env(APP_NAME)%' # Application name + header_name: 'X-Tenant' # Request header to extract schema name + default_schema: 'public' # Default schema to fallback to + allowed_app_names: ['develop', 'staging', 'test'] # App names where schema context is allowed to change ``` ### 2. Set Environment Parameters If you're using .env, define the app name: @@ -63,22 +64,13 @@ public function index(BaggageSchemaResolver $schemaResolver) Decorate your http client in your service configuration: ```yaml services: - baggage_aware_payment_http_client: - class: Macpaw\SchemaContextBundle\HttpClient\BaggageAwareHttpClient - decorates: payment_http_client #http client to decorate - arguments: - - '@baggage_aware_payment_http_client.inner' -``` - -### A Note on Testing - -If you are replacing or mocking HTTP clients in your test environment, for example, using a library like [`macpaw/extended-mock-http-client`](https://github.com/MacPaw/extended_mock_http_client), you need to disable the `BaggageAwareHttpClient` decoration. - -```yaml -when@test: - services: baggage_aware_payment_http_client: class: Macpaw\SchemaContextBundle\HttpClient\BaggageAwareHttpClient + decorates: payment_http_client #http client to decorate + arguments: + - '@baggage_aware_payment_http_client.inner' + - '@Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver' + - '@Macpaw\SchemaContextBundle\Service\BaggageCodec' ``` ## Messenger Integration @@ -90,13 +82,27 @@ The bundle provides a middleware that automatically: Enable the middleware in your `messenger.yaml`: -```yaml +```yaml framework: - messenger: - buses: - messenger.bus.default: - middleware: - - Macpaw\SchemaContextBundle\Messenger\Middleware\BaggageSchemaMiddleware + messenger: + buses: + messenger.bus.default: + middleware: + - Macpaw\SchemaContextBundle\Messenger\Middleware\BaggageMiddleware +``` + +## Optional: Monolog Integration +The bundle provides an optional processor that automatically adds baggage context to your log records: + +* Adds baggage information to the `extra` field of log records + +To enable the processor, add it to your service configuration: + +```yaml +services: + Macpaw\SchemaContextBundle\Monolog\BaggageProcessor: + tags: + - { name: monolog.processor } ``` ## Testing diff --git a/src/Monolog/BaggageProcessor.php b/src/Monolog/BaggageProcessor.php new file mode 100644 index 0000000..04c518a --- /dev/null +++ b/src/Monolog/BaggageProcessor.php @@ -0,0 +1,35 @@ + $record + * + * @return array + */ + public function __invoke(array $record): array + { + $baggage = $this->baggageSchemaResolver->getBaggage(); + + if (is_array($baggage) && count($baggage) > 0) { + if (!isset($record['extra']) || !is_array($record['extra'])) { + $record['extra'] = []; + } + + $record['extra']['baggage'] = $baggage; + } + + return $record; + } +} From ffa0c39d82f137a337b0f2b2b69a8f1b808b917e Mon Sep 17 00:00:00 2001 From: Aleksey Tupichenkov Date: Tue, 30 Sep 2025 11:18:23 +0300 Subject: [PATCH 2/2] chore: update readme --- README.md | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 34fa1d5..b9a8eb6 100644 --- a/README.md +++ b/README.md @@ -65,19 +65,27 @@ Decorate your http client in your service configuration: ```yaml services: baggage_aware_payment_http_client: - class: Macpaw\SchemaContextBundle\HttpClient\BaggageAwareHttpClient - decorates: payment_http_client #http client to decorate - arguments: - - '@baggage_aware_payment_http_client.inner' - - '@Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver' - - '@Macpaw\SchemaContextBundle\Service\BaggageCodec' + class: Macpaw\SchemaContextBundle\HttpClient\BaggageAwareHttpClient + decorates: payment_http_client #http client to decorate + arguments: + - '@baggage_aware_payment_http_client.inner' +``` + +### A Note on Testing + +If you are replacing or mocking HTTP clients in your test environment, for example, using a library like [`macpaw/extended-mock-http-client`](https://github.com/MacPaw/extended_mock_http_client), you need to disable the `BaggageAwareHttpClient` decoration. + +```yaml +when@test: + services: + baggage_aware_payment_http_client: + class: Macpaw\SchemaContextBundle\HttpClient\BaggageAwareHttpClient ``` ## Messenger Integration The bundle provides a middleware that automatically: * Adds a BaggageSchemaStamp to dispatched messages - * Restores the schema and baggage context on message handling Enable the middleware in your `messenger.yaml`: @@ -88,7 +96,7 @@ framework: buses: messenger.bus.default: middleware: - - Macpaw\SchemaContextBundle\Messenger\Middleware\BaggageMiddleware + - Macpaw\SchemaContextBundle\Messenger\Middleware\BaggageSchemaMiddleware ``` ## Optional: Monolog Integration