diff --git a/README.md b/README.md index 8367cec..b9a8eb6 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,11 +64,11 @@ 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' + 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 @@ -76,27 +77,40 @@ If you are replacing or mocking HTTP clients in your test environment, for examp ```yaml when@test: - services: - baggage_aware_payment_http_client: - class: Macpaw\SchemaContextBundle\HttpClient\BaggageAwareHttpClient + 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`: -```yaml +```yaml framework: - messenger: - buses: - messenger.bus.default: - middleware: - - Macpaw\SchemaContextBundle\Messenger\Middleware\BaggageSchemaMiddleware + messenger: + buses: + messenger.bus.default: + middleware: + - Macpaw\SchemaContextBundle\Messenger\Middleware\BaggageSchemaMiddleware +``` + +## 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; + } +}