Skip to content

Commit b7cde74

Browse files
Merge pull request #16 from MacPaw/main
Update from Main
2 parents 42b4687 + f9d5793 commit b7cde74

2 files changed

Lines changed: 68 additions & 19 deletions

File tree

README.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The **SchemaContextBundle** provides a lightweight way to manage dynamic schema
1111
- Injects schema and baggage info into Messenger messages via a middleware.
1212
- Rehydrates schema and baggage on message consumption via a middleware.
1313
- Provide decorator for Http clients to propagate baggage header
14+
- Optional: Adds baggage context to Monolog log records via a processor
1415

1516
---
1617

@@ -34,10 +35,10 @@ Add this config to `config/packages/schema_context.yaml`:
3435

3536
```yaml
3637
schema_context:
37-
app_name: '%env(APP_NAME)%' # Application name
38-
header_name: 'X-Tenant' # Request header to extract schema name
39-
default_schema: 'public' # Default schema to fallback to
40-
allowed_app_names: ['develop', 'staging', 'test'] # App names where schema context is allowed to change
38+
app_name: '%env(APP_NAME)%' # Application name
39+
header_name: 'X-Tenant' # Request header to extract schema name
40+
default_schema: 'public' # Default schema to fallback to
41+
allowed_app_names: ['develop', 'staging', 'test'] # App names where schema context is allowed to change
4142
```
4243
### 2. Set Environment Parameters
4344
If you're using .env, define the app name:
@@ -63,11 +64,11 @@ public function index(BaggageSchemaResolver $schemaResolver)
6364
Decorate your http client in your service configuration:
6465
```yaml
6566
services:
66-
baggage_aware_payment_http_client:
67-
class: Macpaw\SchemaContextBundle\HttpClient\BaggageAwareHttpClient
68-
decorates: payment_http_client #http client to decorate
69-
arguments:
70-
- '@baggage_aware_payment_http_client.inner'
67+
baggage_aware_payment_http_client:
68+
class: Macpaw\SchemaContextBundle\HttpClient\BaggageAwareHttpClient
69+
decorates: payment_http_client #http client to decorate
70+
arguments:
71+
- '@baggage_aware_payment_http_client.inner'
7172
```
7273
7374
### A Note on Testing
@@ -76,27 +77,40 @@ If you are replacing or mocking HTTP clients in your test environment, for examp
7677

7778
```yaml
7879
when@test:
79-
services:
80-
baggage_aware_payment_http_client:
81-
class: Macpaw\SchemaContextBundle\HttpClient\BaggageAwareHttpClient
80+
services:
81+
baggage_aware_payment_http_client:
82+
class: Macpaw\SchemaContextBundle\HttpClient\BaggageAwareHttpClient
8283
```
8384

8485
## Messenger Integration
8586
The bundle provides a middleware that automatically:
8687

8788
* Adds a BaggageSchemaStamp to dispatched messages
88-
8989
* Restores the schema and baggage context on message handling
9090

9191
Enable the middleware in your `messenger.yaml`:
9292

93-
```yaml
93+
```yaml
9494
framework:
95-
messenger:
96-
buses:
97-
messenger.bus.default:
98-
middleware:
99-
- Macpaw\SchemaContextBundle\Messenger\Middleware\BaggageSchemaMiddleware
95+
messenger:
96+
buses:
97+
messenger.bus.default:
98+
middleware:
99+
- Macpaw\SchemaContextBundle\Messenger\Middleware\BaggageSchemaMiddleware
100+
```
101+
102+
## Optional: Monolog Integration
103+
The bundle provides an optional processor that automatically adds baggage context to your log records:
104+
105+
* Adds baggage information to the `extra` field of log records
106+
107+
To enable the processor, add it to your service configuration:
108+
109+
```yaml
110+
services:
111+
Macpaw\SchemaContextBundle\Monolog\BaggageProcessor:
112+
tags:
113+
- { name: monolog.processor }
100114
```
101115

102116
## Testing

src/Monolog/BaggageProcessor.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Macpaw\SchemaContextBundle\Monolog;
6+
7+
use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver;
8+
9+
class BaggageProcessor
10+
{
11+
public function __construct(
12+
private readonly BaggageSchemaResolver $baggageSchemaResolver,
13+
) {
14+
}
15+
16+
/**
17+
* @param array<string, mixed> $record
18+
*
19+
* @return array<string, mixed>
20+
*/
21+
public function __invoke(array $record): array
22+
{
23+
$baggage = $this->baggageSchemaResolver->getBaggage();
24+
25+
if (is_array($baggage) && count($baggage) > 0) {
26+
if (!isset($record['extra']) || !is_array($record['extra'])) {
27+
$record['extra'] = [];
28+
}
29+
30+
$record['extra']['baggage'] = $baggage;
31+
}
32+
33+
return $record;
34+
}
35+
}

0 commit comments

Comments
 (0)