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

Add optional label to BaseRetrying class #418

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

aragaer
Copy link

@aragaer aragaer commented Sep 26, 2023

If label is set it will be used instead of a function name in before_sleep_log.

I didn't find any appropriate place to add a test, so I simply wrote a small script to check that it all works:

#!/usr/bin/env python
import logging
import sys

from tenacity import (
    before_sleep_log,
    Retrying,
    stop_after_attempt,
    wait_fixed,
)

logger = logging.getLogger('MY LOGGER')
logging.basicConfig(stream=sys.stdout, level=logging.INFO)

for attempt in Retrying(
        label="MY LABEL",
        stop=stop_after_attempt(5),
        before_sleep=before_sleep_log(logger, logging.INFO),
        wait=wait_fixed(1)):
    with attempt:
        raise Exception("yo")

@mergify
Copy link
Contributor

mergify bot commented Sep 26, 2023

⚠️ No release notes detected. Please make sure to use reno to add a changelog entry.

Copy link
Owner

@jd jd left a comment

Choose a reason for hiding this comment

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

Not a fan of adding a custom field in the retrying code just for the logging code.
I'm also unsure about what that solves exactly?

@aragaer
Copy link
Author

aragaer commented Sep 27, 2023

Current code assumes that there is always a function name available (there even was a comment saying that this should never happen) -- which is not the case when using a context manager. Currently logging functions would output "Retrying <unknown>" and there is no way to specify which section of code failed and needs the retry. The idea is to specify an optional label for a retry block. And if retry block is repeated in a loop it also allows to specify which iteration caused a retry:

for item in some_iterable:
    try:
        for attempt in Retrying(
                label=f"doing stuff with {item}",
                stop=stop_after_attempt(5),
                before_sleep=before_sleep_log(logger, logging.DEBUG),
                wait=wait_fixed(10)):
            with attempt:
                data = do_stuff(item)
    except RetryError:
        continue
    ...

@jd
Copy link
Owner

jd commented Sep 27, 2023

This code snippet makes me think this should just be an argument to before_sleep to allow changing the string passed to logger.log.

@aragaer
Copy link
Author

aragaer commented Sep 27, 2023

So that the snippet would become like this?

for item in some_iterable:
    try:
        for attempt in Retrying(
                stop=stop_after_attempt(5),
                before_sleep=before_sleep_log(logger, logging.DEBUG, label=f"doing stuff with {item}"),
                wait=wait_fixed(10)):
            with attempt:
                data = do_stuff(item)
    except RetryError:
        continue
    ...

However fn_name is also used in before_log and after_log. Having a single label shared between them at BaseRetryObject makes a bit more sense than having a separate label for each function.

If label is set it will be used instead of a function name in
before_sleep_log
@aragaer
Copy link
Author

aragaer commented Sep 28, 2023

I've moved the "get label" code to the RetryCallState class and modified all the log functions to use it.

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

2 participants