Skip to content

Conversation

@mwazovzky
Copy link

Add support for merging metadata from exceptions that implement Junges\Kafka\Contracts\ContextAware interface into the headers of messages sent to the Dead Letter Queue (DLQ). Context keys and values are normalized allowing only string keys and values. Existing message headers are preserved unless explicitly overwritten by the exception context.

Why
Sometimes additional failure metadata (id, correlation keys, retry counts, ...) added to DLQ headers may help downstream consumers and observability tools diagnose and route failed messages.

What changed

  • src/Consumers/Consumer.php: include ContextAware::getContext() when building DLQ headers and normalize keys/values.
  • tests/Consumers/ConsumerTest.php: add tests asserting that context metadata from ContextAware exceptions is appended to DLQ headers.
  • docs/consuming-messages/configuring-consumer-options.md: document the new behavior, include example exception and resulting DLQ headers.
  • No breaking changes introduced into public API.

Behavior details

  • Preserves original message headers as before.
  • Adds kafka_throwable_message, kafka_throwable_code, and kafka_throwable_class_name keys as before.
  • Merges normalized context into the DLQ headers.
  • Skips non-string values and empty keys to ensure header safety.

Testing and Linting

  • Run Unit tests added to tests/Consumers ./vendor/bin/phpunit --filter ConsumerTest
  • Verify PSR compliance: ./vendor/bin/pint

Add support for merging metadata from exceptions that implement
Junges\Kafka\Contracts\ContextAware interface into the headers of messages sent to the
Dead Letter Queue (DLQ). Context keys and values are normalized allowing only
string keys and values. Existing message headers are preserved unless explicitly
overwritten by the exception context.

Why
Sometimes additional failure metadata (id, correlation keys, retry counts, ...) added to DLQ headers
may help downstream consumers and observability tools diagnose and route failed messages.

What changed
- src/Consumers/Consumer.php:
  include ContextAware::getContext() when building DLQ headers and normalize keys/values.
- tests/Consumers/ConsumerTest.php:
  add  tests asserting that context metadata from ContextAware exceptions is appended to DLQ headers.
- docs/consuming-messages/configuring-consumer-options.md:
  document the new behavior, include example exception and resulting DLQ headers.
- No breaking changes introduced into public API.

Behavior details
- Preserves original message headers as before.
- Adds `kafka_throwable_message`, `kafka_throwable_code`, and `kafka_throwable_class_name` keys as before.
- Merges normalized context into the DLQ headers.
- Skips non-string values and empty keys to ensure header safety.

Testing and Linting
-------
- Run Unit tests added to tests/Consumers
  ./vendor/bin/phpunit --filter ConsumerTest
- Verify PSR compliance:
  ./vendor/bin/pint
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.

1 participant