Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

---

Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions src/Monolog/BaggageProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Macpaw\SchemaContextBundle\Monolog;

use Macpaw\SchemaContextBundle\Service\BaggageSchemaResolver;

class BaggageProcessor
{
public function __construct(
private readonly BaggageSchemaResolver $baggageSchemaResolver,
) {
}

/**
* @param array<string, mixed> $record
*
* @return array<string, mixed>
*/
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;
}
}
Loading