question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Azure Event Hub resets connection

See original GitHub issue

Hi all,

Microsoft Azure offers a service they call Azure Event Hub, which can collect events from Kafka producers

Here’s an example from Microsoft using the confluent_kafka module: https://github.com/Azure/azure-event-hubs/blob/master/samples/kafka/python/producer.py

However, when I try similar steps with kafka-python, Azure disconnects. Any ideas?

import ssl

from kafka import KafkaProducer

class KafkaError(RuntimeError):
    """Raised when a Kafka error occurs"""

class KafkaClient(object):
    def __init__(self, kafka_hosts, use_ssl=False, username=None,
                 password=None):
        """
        Initializes the Kafka client
        Args:
            kafka_hosts (list): A list of Kafka hostnames
            (with optional port numbers)
            use_ssl (bool): Use a SSL/TLS connection
            username (str): An optional username
            password (str):  An optional password

        Notes:
            ``use_ssl=True`` is implied when a username or password are
            supplied.

            When using Azure Event Hubs, the username is literally
            ``$ConnectionString``, and the password is the
            Azure Event Hub connection string.
        """
        config = dict(value_serializer=lambda v: json.dumps(v).encode(
                              'utf-8'),
                      bootstrap_servers=kafka_hosts)
        if use_ssl or username or password:
            config["security_protocol"] = "SSL"
            config["ssl_context"] = ssl.create_default_context()
            if username or password:
                config["sasl_plain_username"] = username or ""
                config["sasl_plain_password"] = password or ""
        try:
            self.producer = KafkaProducer(**config)
        except NoBrokersAvailable:
            raise KafkaError("No Kafka brokers available")
ERROR:kafka.conn:<BrokerConnection node_id=bootstrap host=parsedmarc.servicebus.windows.net:9093 <connected> [IPv4 ('40.71.10.128', 9093)]>: Error receiving network data closing socket
Traceback (most recent call last):
  File "C:\Users\Sean\GitHub\parsedmarc\venv\lib\site-packages\kafka\conn.py", line 816, in _recv
    data = self._sock.recv(self.config['sock_chunk_bytes'])
  File "C:\Program Files\Python37\lib\ssl.py", line 1034, in recv
    return self.read(buflen)
  File "C:\Program Files\Python37\lib\ssl.py", line 910, in read
    return self._sslobj.read(len)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
ERROR:kafka.client:Unable to bootstrap from [('parsedmarc.servicebus.windows.net', 9093, <AddressFamily.AF_UNSPEC: 0>)]

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
arerlendcommented, Jun 5, 2019

@seanthegeek in version 1.4.4, this configuration works for me -

connection_string = 'Endpoint=sb://[namespace].servicebus.windows.net/;SharedAccessKeyName=[key name];SharedAccessKey=[key]
servers = '[namespace].servicebus.windows.net:9093'

producer = KafkaProducer(
    bootstrap_servers=servers,
    security_protocol='SASL_SSL', # this is likely the problem
    sasl_mechanism='PLAIN',
    sasl_plain_username='$ConnectionString',
    sasl_plain_password=connection_string,
    ssl_cafile=ca_path,
    api_version=(2,0,0) # for example - this should work for (1,0,0) and greater
)

In your case, we may be forcibly closing the connection because we’re expecting you to use the SSL plugin via SASL (API key 17 - first the handshake, then the auth bytes).

0reactions
Idofriedmancommented, Oct 24, 2019
ssl_cafile=ca_path,

What is the certificate? Where do I get it from?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connection reset by peer on attempt to GetRuntimeInformation
One thing worth checking out is the firewall in your network being opened for port 5671. I expected Event Hub to always be...
Read more >
Troubleshoot connectivity issues - Azure Event Hubs
This article provides information on troubleshooting connectivity issues with Azure Event Hubs.
Read more >
Frequently asked questions - Azure Event Hubs
This article provides a list of frequently asked questions (FAQ) for Azure Event Hubs and their answers.
Read more >
Event hub manual failover - EventProcessorClient checkpoint ...
Failover is a manual process and user should restart consumers after resetting the checkpoints once failover is complete. As per my conversation ...
Read more >
Ingest data from event hub into Azure Data Explorer
If the Event Hub is moved to a different resource or subscription, you won't be able to make changes to the connection from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found