Skip to content
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

FIX add error_cb to confluent.Consumer config in ConsumerFromTopic #44307

Merged
merged 8 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions providers/src/airflow/providers/apache/kafka/hooks/consume.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,23 @@

from typing import Sequence

from confluent_kafka import Consumer
from confluent_kafka import Consumer, KafkaError

from airflow.providers.apache.kafka.hooks.base import KafkaBaseHook
from airflow.utils.module_loading import import_string


class KafkaAuthenticationError(Exception):
potiuk marked this conversation as resolved.
Show resolved Hide resolved
"""Custom exception for Kafka authentication failures."""

pass


def error_callback(err):
SuccessMoses marked this conversation as resolved.
Show resolved Hide resolved
"""Handle kafka errors."""
if err.code() == KafkaError._AUTHENTICATION:
raise KafkaAuthenticationError(f"Authentication failed: {err}")
print("Exception received: ", err)
SuccessMoses marked this conversation as resolved.
Show resolved Hide resolved


class KafkaConsumerHook(KafkaBaseHook):
Expand All @@ -36,7 +50,12 @@ def __init__(self, topics: Sequence[str], kafka_config_id=KafkaBaseHook.default_
self.topics = topics

def _get_client(self, config) -> Consumer:
return Consumer(config)
config_shallow = config.copy()
if config.get("error_cb") is None:
config_shallow["error_cb"] = error_callback
else:
config_shallow["error_cb"] = import_string(config["error_cb"])
return Consumer(config_shallow)

def get_consumer(self) -> Consumer:
"""Return a Consumer that has been subscribed to topics."""
Expand Down
16 changes: 16 additions & 0 deletions providers/src/airflow/providers/standard/triggers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.