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 | |
| 22 | log = create_logger(Config().get("logging")) |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 23 | |
| 24 | kafka_producer = None |
| 25 | |
| 26 | |
| 27 | class XOSKafkaProducer: |
| 28 | """ XOSKafkaProducer |
| 29 | Wrapper to share Kafka Producer connection |
| 30 | """ |
| 31 | |
| 32 | @staticmethod |
| 33 | def init(): |
| 34 | |
| 35 | global kafka_producer |
| 36 | |
| 37 | if kafka_producer: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 38 | raise Exception("XOSKafkaProducer already initialized") |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 39 | |
| 40 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 41 | log.info( |
| 42 | "Connecting to Kafka with bootstrap servers: %s" |
| 43 | % Config.get("kafka_bootstrap_servers") |
| 44 | ) |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 45 | |
| 46 | try: |
| 47 | producer_config = { |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 48 | "bootstrap.servers": ",".join(Config.get("kafka_bootstrap_servers")) |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | kafka_producer = confluent_kafka.Producer(**producer_config) |
| 52 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 53 | log.info("Connected to Kafka: %s" % kafka_producer) |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 54 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 55 | except confluent_kafka.KafkaError as e: |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 56 | log.exception("Kafka Error: %s" % e) |
| 57 | |
| 58 | @classmethod |
| 59 | def produce(cls, topic, key, value): |
| 60 | |
| 61 | try: |
| 62 | kafka_producer.produce( |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 63 | topic, value, key, callback=cls._kafka_delivery_callback |
| 64 | ) |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 65 | |
Zack Williams | c2b87bf | 2018-09-21 13:48:40 -0700 | [diff] [blame] | 66 | # see https://github.com/confluentinc/confluent-kafka-python/issues/16 |
| 67 | kafka_producer.poll(0) |
| 68 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 69 | except confluent_kafka.KafkaError as err: |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 70 | log.exception("Kafka Error", err) |
| 71 | |
Zack Williams | c2b87bf | 2018-09-21 13:48:40 -0700 | [diff] [blame] | 72 | def __del__(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 73 | if kafka_producer is not None: |
Zack Williams | c2b87bf | 2018-09-21 13:48:40 -0700 | [diff] [blame] | 74 | kafka_producer.flush() |
| 75 | |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 76 | @staticmethod |
| 77 | def _kafka_delivery_callback(err, msg): |
| 78 | if err: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 79 | log.error("Message failed delivery: %s" % err) |
Zack Williams | 73a1285 | 2018-09-05 15:33:35 -0700 | [diff] [blame] | 80 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 81 | log.trace("Message delivered", message=msg) |