Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 1 | # Copyright 2018-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """ XOSKafkaProducer """ |
| 16 | |
| 17 | import confluent_kafka |
| 18 | |
| 19 | from xosconfig import Config |
| 20 | from multistructlog import create_logger |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 21 | |
Scott Baker | 93c40b4 | 2019-03-21 08:57:26 -0700 | [diff] [blame] | 22 | log = None |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 23 | kafka_producer = None |
| 24 | |
| 25 | |
| 26 | class XOSKafkaProducer: |
| 27 | """ XOSKafkaProducer |
| 28 | Wrapper to share Kafka Producer connection |
| 29 | """ |
| 30 | |
| 31 | @staticmethod |
| 32 | def init(): |
| 33 | |
Scott Baker | 93c40b4 | 2019-03-21 08:57:26 -0700 | [diff] [blame] | 34 | global log |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 35 | global kafka_producer |
| 36 | |
Scott Baker | 93c40b4 | 2019-03-21 08:57:26 -0700 | [diff] [blame] | 37 | if not log: |
| 38 | log = create_logger(Config().get("logging")) |
| 39 | |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 40 | if kafka_producer: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 41 | raise Exception("XOSKafkaProducer already initialized") |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 42 | |
| 43 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 44 | log.info( |
| 45 | "Connecting to Kafka with bootstrap servers: %s" |
| 46 | % Config.get("kafka_bootstrap_servers") |
| 47 | ) |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 48 | |
| 49 | try: |
| 50 | producer_config = { |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 51 | "bootstrap.servers": ",".join(Config.get("kafka_bootstrap_servers")) |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | kafka_producer = confluent_kafka.Producer(**producer_config) |
| 55 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 56 | log.info("Connected to Kafka: %s" % kafka_producer) |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 57 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 58 | except confluent_kafka.KafkaError as e: |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 59 | log.exception("Kafka Error: %s" % e) |
| 60 | |
| 61 | @classmethod |
| 62 | def produce(cls, topic, key, value): |
| 63 | |
| 64 | try: |
| 65 | kafka_producer.produce( |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 66 | topic, value, key, callback=cls._kafka_delivery_callback |
| 67 | ) |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 68 | |
Zack Williams | c2b87bf | 2018-09-21 13:48:40 -0700 | [diff] [blame] | 69 | # see https://github.com/confluentinc/confluent-kafka-python/issues/16 |
| 70 | kafka_producer.poll(0) |
| 71 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 72 | except confluent_kafka.KafkaError as err: |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 73 | log.exception("Kafka Error", err) |
| 74 | |
Zack Williams | c2b87bf | 2018-09-21 13:48:40 -0700 | [diff] [blame] | 75 | def __del__(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 76 | if kafka_producer is not None: |
Zack Williams | c2b87bf | 2018-09-21 13:48:40 -0700 | [diff] [blame] | 77 | kafka_producer.flush() |
| 78 | |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 79 | @staticmethod |
| 80 | def _kafka_delivery_callback(err, msg): |
| 81 | if err: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 82 | log.error("Message failed delivery: %s" % err) |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 83 | else: |
Zack Williams | da69db2 | 2019-01-29 16:44:52 -0700 | [diff] [blame] | 84 | pass |