-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
151 additions
and
4 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Empty file.
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,14 @@ | ||
import json | ||
|
||
from ..sink_base import SinkBase | ||
from kafka import KafkaProducer | ||
|
||
|
||
class KafkaSink(SinkBase): | ||
|
||
def __init__(self, producer: KafkaProducer, topic: str): | ||
self.producer = producer | ||
self.topic = topic | ||
|
||
def write(self, data: dict): | ||
self.producer.send(self.topic, value=json.dumps(data).encode("utf-8")) |
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,6 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class KafkaSinkConfig(BaseModel): | ||
kafka_url: str | ||
topic: str |
23 changes: 23 additions & 0 deletions
23
src/robusta/integrations/sinks/kafka/kafka_sink_manager.py
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,23 @@ | ||
import threading | ||
from collections import defaultdict | ||
|
||
from kafka import KafkaProducer | ||
|
||
from .kafka_sink import KafkaSink | ||
from ..sink_base import SinkBase | ||
|
||
|
||
class KafkaSinkManager: | ||
|
||
manager_lock = threading.Lock() | ||
producers_map = defaultdict(None) | ||
|
||
@staticmethod | ||
def get_kafka_sink(kafka_url : str, topic: str) -> SinkBase: | ||
with KafkaSinkManager.manager_lock: | ||
producer = KafkaSinkManager.producers_map.get(kafka_url) | ||
if producer is not None: | ||
return KafkaSink(producer, topic) | ||
producer = KafkaProducer(bootstrap_servers=kafka_url) | ||
KafkaSinkManager.producers_map[kafka_url] = producer | ||
return KafkaSink(producer, topic) |
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,5 @@ | ||
|
||
class SinkBase: | ||
|
||
def write(self, data: dict): | ||
pass |
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,8 @@ | ||
from typing import Dict | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class SinkConfigBase(BaseModel): | ||
sink_type: str | ||
params: Dict |
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,15 @@ | ||
from .kafka.kafka_sink_config import KafkaSinkConfig | ||
from .kafka.kafka_sink_manager import KafkaSinkManager | ||
from .sink_config import SinkConfigBase | ||
from .sink_base import SinkBase | ||
|
||
|
||
class SinkFactory: | ||
|
||
@staticmethod | ||
def get_sink(sink_config: SinkConfigBase) -> SinkBase: | ||
if sink_config.sink_type == "kafka": | ||
kafka_sink_config = KafkaSinkConfig(**sink_config.params) | ||
return KafkaSinkManager.get_kafka_sink(kafka_sink_config.kafka_url, kafka_sink_config.topic) | ||
|
||
raise Exception(f"Sink not supported {sink_config.sink_type}") |