-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[kafka_actions] Add plugin architecture for formats and compression codecs #23650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
piochelepiotr
wants to merge
3
commits into
master
Choose a base branch
from
piotr.wolski/kafka_actions-plugin-architecture
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add a plugin architecture for message format handlers and payload-compression codecs. Format handlers can be registered via the `datadog_kafka_actions.formats` entry-point group, and compression codecs via `datadog_kafka_actions.compressions`. Built-in formats (json, string, raw, bson, avro, protobuf) are now first-class plugins. New `value_compression` and `key_compression` config keys decompress payloads before deserialization. No compression codecs ship in core — install a plugin wheel to add them. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
kafka_actions/datadog_checks/kafka_actions/compression/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # (C) Datadog, Inc. 2026-present | ||
| # All rights reserved | ||
| # Licensed under a 3-clause BSD style license (see LICENSE) | ||
| """Compression codec registry for kafka_actions. | ||
|
|
||
| Some producers compress message payloads at the application layer (before | ||
| handing bytes to the Kafka producer) using a variety of algorithms, separate | ||
| from the broker-negotiated ``compression.type`` setting. This module exposes | ||
| a pluggable codec interface so consumers can decompress those payloads | ||
| before deserialization. | ||
|
|
||
| No codecs ship in the core wheel — install a plugin wheel that registers | ||
| codecs on the ``datadog_kafka_actions.compressions`` entry-point group, or | ||
| register them directly via :func:`register_codec` in tests. | ||
| """ | ||
|
|
||
| from .base import CompressionCodec | ||
| from .registry import get_codec, list_codecs, register_codec | ||
|
|
||
| __all__ = ['CompressionCodec', 'get_codec', 'list_codecs', 'register_codec'] |
19 changes: 19 additions & 0 deletions
19
kafka_actions/datadog_checks/kafka_actions/compression/base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # (C) Datadog, Inc. 2026-present | ||
| # All rights reserved | ||
| # Licensed under a 3-clause BSD style license (see LICENSE) | ||
| """Base class for app-level payload compression codecs.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from abc import ABC, abstractmethod | ||
|
|
||
|
|
||
| class CompressionCodec(ABC): | ||
| """Plug-in interface for app-level payload decompression.""" | ||
|
|
||
| name: str = '' | ||
|
|
||
| @abstractmethod | ||
| def decompress(self, data: bytes) -> bytes: | ||
| """Return the uncompressed payload bytes.""" | ||
| raise NotImplementedError |
64 changes: 64 additions & 0 deletions
64
kafka_actions/datadog_checks/kafka_actions/compression/registry.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # (C) Datadog, Inc. 2026-present | ||
| # All rights reserved | ||
| # Licensed under a 3-clause BSD style license (see LICENSE) | ||
| """Lazy registry of compression codecs.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from importlib.metadata import entry_points | ||
| from threading import Lock | ||
|
|
||
| from .base import CompressionCodec | ||
|
|
||
| _LOG = logging.getLogger(__name__) | ||
| _ENTRY_POINT_GROUP = 'datadog_kafka_actions.compressions' | ||
|
|
||
| _lock = Lock() | ||
| _codecs: dict[str, CompressionCodec] = {} | ||
| _loaded = False | ||
|
|
||
|
|
||
| def register_codec(codec: CompressionCodec) -> None: | ||
| if not codec.name: | ||
| raise ValueError(f"CompressionCodec {type(codec).__name__} has no name set") | ||
| with _lock: | ||
| _codecs[codec.name] = codec | ||
|
|
||
|
|
||
| def _load_entry_points() -> None: | ||
| global _loaded | ||
| if _loaded: | ||
| return | ||
| with _lock: | ||
| if _loaded: | ||
| return | ||
| try: | ||
| eps = entry_points(group=_ENTRY_POINT_GROUP) | ||
| except TypeError: # pragma: no cover | ||
| eps = entry_points().get(_ENTRY_POINT_GROUP, []) | ||
| for ep in eps: | ||
| if ep.name in _codecs: | ||
| continue | ||
| try: | ||
| cls = ep.load() | ||
| instance = cls() if isinstance(cls, type) else cls | ||
| if not isinstance(instance, CompressionCodec): | ||
| _LOG.warning("Entry point %s did not produce a CompressionCodec", ep.name) | ||
| continue | ||
| if not instance.name: | ||
| instance.name = ep.name | ||
| _codecs[instance.name] = instance | ||
| except Exception as e: | ||
| _LOG.warning("Failed to load compression codec '%s': %s", ep.name, e) | ||
| _loaded = True | ||
|
|
||
|
|
||
| def get_codec(name: str) -> CompressionCodec | None: | ||
| _load_entry_points() | ||
| return _codecs.get(name) | ||
|
|
||
|
|
||
| def list_codecs() -> list[str]: | ||
| _load_entry_points() | ||
| return sorted(_codecs) |
18 changes: 18 additions & 0 deletions
18
kafka_actions/datadog_checks/kafka_actions/formats/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # (C) Datadog, Inc. 2026-present | ||
| # All rights reserved | ||
| # Licensed under a 3-clause BSD style license (see LICENSE) | ||
| """Format handler registry for kafka_actions. | ||
|
|
||
| External wheels can register additional handlers by exposing them on the | ||
| ``datadog_kafka_actions.formats`` entry-point group: | ||
|
|
||
| [project.entry-points."datadog_kafka_actions.formats"] | ||
| myformat = "my_pkg.handler:MyHandler" | ||
|
|
||
| Handlers must subclass :class:`FormatHandler` from ``base``. | ||
| """ | ||
|
|
||
| from .base import FormatHandler | ||
| from .registry import get_handler, list_handlers, register_handler | ||
|
|
||
| __all__ = ['FormatHandler', 'get_handler', 'list_handlers', 'register_handler'] |
42 changes: 42 additions & 0 deletions
42
kafka_actions/datadog_checks/kafka_actions/formats/base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # (C) Datadog, Inc. 2026-present | ||
| # All rights reserved | ||
| # Licensed under a 3-clause BSD style license (see LICENSE) | ||
| """Base class for kafka_actions format handlers.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from typing import Any | ||
|
|
||
|
|
||
| class FormatHandler(ABC): | ||
| """Plug-in interface for message-body deserialization. | ||
|
|
||
| Subclasses are instantiated once and reused across messages, so they | ||
| should be stateless or maintain only thread-safe caches. | ||
| """ | ||
|
|
||
| name: str = '' | ||
|
|
||
| def build_schema(self, schema_str: str) -> Any: | ||
| """Build a schema object from an inline (config-supplied) schema string. | ||
|
|
||
| Override for formats that need a parsed schema (e.g. Avro, Protobuf). | ||
| Schemaless formats (json, msgpack, raw) can leave the default. | ||
| """ | ||
| return None | ||
|
|
||
| def build_schema_from_registry(self, schema_str: str, dep_schemas: list) -> Any: | ||
| """Build a schema object from registry-supplied bytes. | ||
|
|
||
| ``dep_schemas`` is a list of ``(name, base64_bytes)`` tuples for | ||
| dependencies (e.g. imported .proto files). | ||
|
|
||
| Defaults to :meth:`build_schema` for formats that don't distinguish. | ||
| """ | ||
| return self.build_schema(schema_str) | ||
|
|
||
| @abstractmethod | ||
| def deserialize(self, message: bytes, schema: Any, *, log, uses_schema_registry: bool) -> str | None: | ||
| """Decode ``message`` and return a JSON string (or None for empty messages).""" | ||
| raise NotImplementedError |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wire-up in
read_messagesnow readsvalue_compression/key_compression, but this commit does not add those fields to the integration configuration contract (kafka_actions/assets/configuration/spec.yaml) or generated model (kafka_actions/datadog_checks/kafka_actions/config_models/instance.py). In Remote Configuration flows, unsupported keys are rejected or dropped before reaching the check, so the new compression feature is effectively not configurable in production even though deserialization now expects it.Useful? React with 👍 / 👎.