-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(protobuf) Add support for protobuf message topics (#344)
For taskworkers (and likely other topics) in the future, we would like to put protobuf serialized messages into the topic instead of json/msgpack. Using protobufs will let us have one message schema for both kafka, rpc and application logic. In order to get access to the protobuf generated code, I've needed to expand the dependencies of this library. I thought this was the simplest to build and maintain solution. Our protos package isn't compatible with python 3.9. 3.9 went end of life mid 2022 and I don't think we have any applications still running on 3.9
- Loading branch information
Showing
17 changed files
with
201 additions
and
108 deletions.
There are no files selected for viewing
This file contains 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
Validating CODEOWNERS rules …
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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,2 @@ | ||
|
||
abc123teststests.do_things"{"args":[],"kwargs":{}}2���� |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
black==24.3.0 | ||
isort==5.13.2 | ||
flake8==5.0.4 | ||
mypy==0.971 | ||
mypy==1.13.0 | ||
pytest>=7.2.0 | ||
pytest-xdist==3.2.0 | ||
fastjsonschema==2.16.3 | ||
jsonschema==4.17.3 | ||
types-protobuf |
This file contains 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 |
---|---|---|
|
@@ -3,3 +3,4 @@ PyYAML>=5.4,<7.0 | |
typing-extensions>=4.0.0 | ||
fastjsonschema>=2.16.2 | ||
msgpack>=1.0.4 | ||
sentry-protos>=0.1.30 |
This file contains 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,44 @@ | ||
from typing import TypeVar, cast | ||
|
||
from google.protobuf.message import Message as ProtoMessage | ||
from sentry_kafka_schemas.codecs import Codec | ||
|
||
T = TypeVar("T", bound=ProtoMessage) | ||
|
||
|
||
class ProtobufCodec(Codec[T]): | ||
""" | ||
This codec assumes the payload is a protobuf payload. | ||
`resource` should be a module path to the message type | ||
in a protobuf generated module. For us this will most likely | ||
be message in `sentry-protos`. | ||
""" | ||
|
||
def __init__(self, resource: str) -> None: | ||
self._resource = resource | ||
self._message_cls = self._import_resource() | ||
|
||
def _import_resource(self) -> type[T]: | ||
module_name, class_name = self._resource.rsplit(".", 1) | ||
|
||
module = __import__(module_name, {}, {}, [class_name]) | ||
class_type = getattr(module, class_name) | ||
return cast(type[T], class_type) | ||
|
||
def encode(self, data: T, validate: bool = True) -> bytes: | ||
# There isn't any validation logic as protobuf | ||
# does most of the type validation as messages are constructed. | ||
return data.SerializeToString() | ||
|
||
def decode(self, raw_data: bytes, validate: bool = True) -> T: | ||
# There isn't any validation logic as protobuf | ||
# does validation implicitly when deserializing. | ||
instance = self._message_cls() | ||
instance.ParseFromString(raw_data) | ||
return instance | ||
|
||
def validate(self, data: T) -> None: | ||
# Protobuf automatically validates instances | ||
# as they are built. | ||
return None |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.