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

Logger with Admin Client #1758

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

pranavrth
Copy link
Member

  • Fixed logger not working when provided as an argument to AdminClient
  • Updated examples/adminapi.py to include usage of the custom logger with AdminClient

Copy link
Member

@anchitj anchitj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments

"""
Create a new AdminClient using the provided configuration dictionary.

The AdminClient is a standard Kafka protocol client, supporting
the standard librdkafka configuration properties as specified at
https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md

:param dict config: Configuration properties. At a minimum ``bootstrap.servers`` **should** be set\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be named conf? And also the trailing \n can be removed

"""
Create a new AdminClient using the provided configuration dictionary.

The AdminClient is a standard Kafka protocol client, supporting
the standard librdkafka configuration properties as specified at
https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md

:param dict config: Configuration properties. At a minimum ``bootstrap.servers`` **should** be set\n"
:param Logger logger: Optional Logger instance to use as a custom log messages handler.

At least 'bootstrap.servers' should be configured.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed as it's included above now

At least 'bootstrap.servers' should be configured.
"""
if logger is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to throw an exception here if the conf doesn't includes bootstrap? like

        if 'bootstrap.servers' not in conf:
            raise ValueError("Configuration must include 'bootstrap.servers'")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not doing this anywhere else. Let's not do here as well.

Copy link
Contributor

@emasab emasab Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For anchit's comment about bootstrap.servers: this is checked later by librdkafka, whenever possible we delegate the validation there

Comment on lines 187 to 192
stringBuffer = StringIO()
logger = logging.getLogger('Admin')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(stringBuffer)
handler.setFormatter(logging.Formatter('%(name)s Logger | %(message)s'))
logger.addHandler(handler)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is being repeated here and the above two functions. Can be moved to setup_logger or some helper function

@@ -920,3 +928,6 @@ def example_list_offsets(a, args):
sys.exit(1)

opsmap[operation](a, args)

# Log messages through custom logger if provided
a.poll(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline missing

logMessage = stringBuffer.getvalue().strip()
stringBuffer.close()

assert "Admin Logger | INIT" in logMessage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline missing

# Create Admin client
a = AdminClient({'bootstrap.servers': broker})
a = AdminClient({'bootstrap.servers': broker},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger should be a property like in the producer and consumer, not an additional parameter
'logger': logger,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In python, its an additional parameter as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, I delete it so I don't request this change

* Updated examples/adminapi.py to include usage of the custom logger with AdminClient
@pranavrth pranavrth force-pushed the dev_fix-logger-arg-admin-client branch from 5479623 to a2be27b Compare June 20, 2024 11:09
At least 'bootstrap.servers' should be configured.
"""
if logger is not None:
Copy link
Contributor

@emasab emasab Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For anchit's comment about bootstrap.servers: this is checked later by librdkafka, whenever possible we delegate the validation there

"""
if logger is not None:
conf['logger'] = logger
super(AdminClient, self).__init__(conf)
Copy link
Contributor

@emasab emasab Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just pass the kwargs here without changing the conf, that'll be done by the C code

Suggested change
super(AdminClient, self).__init__(conf)
super(AdminClient, self).__init__(conf, **kwargs)

Copy link
Member Author

@pranavrth pranavrth Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work in case the logger is None. I tried this only first.

In general, if the logger is provided in conf as well as in the parameter, parameter gets more priority. By doing this, the logger is always None if it is provided in conf.

https://github.com/confluentinc/confluent-kafka-python/blob/master/src/confluent_kafka/src/confluent_kafka.c#L2232-L2241

Copy link
Contributor

@emasab emasab Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, was checking this an updated my comment with kwargs

@@ -109,16 +109,19 @@ class AdminClient (_AdminClientImpl):
Requires broker version v0.11.0.0 or later.
"""

def __init__(self, conf):
def __init__(self, conf, logger=None):
Copy link
Contributor

@emasab emasab Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __init__(self, conf, logger=None):
def __init__(self, conf, **kwargs):

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will start taking all the properties in the conf as parameter. I don't know if we should support this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of only supporting logger property like this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what happens with the Producer and Consumer too

@pranavrth
Copy link
Member Author

This is checked later by librdkafka, whenever possible we delegate the validation there

There is inbetween manipulation of the config properties in python binding as well. Due to that part, I have to check here first.

https://github.com/confluentinc/confluent-kafka-python/blob/master/src/confluent_kafka/src/confluent_kafka.c#L2215

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants