Skip to content

feat: Allow configuring keep_alive via environment variable #4366

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

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

Conversation

szokeasaurusrex
Copy link
Member

This commit enables the keep_alive option to be set via the SENTRY_KEEP_ALIVE environment variable. When both the environment variable and the argument are provided, the argument takes precedence.

Closes #4354

This commit enables the `keep_alive` option to be set via the `SENTRY_KEEP_ALIVE` environment variable. When both the environment variable and the argument are provided, the argument takes precedence.

Closes #4354
@szokeasaurusrex szokeasaurusrex requested a review from a team as a code owner May 6, 2025 09:22
@szokeasaurusrex szokeasaurusrex requested review from antonpirker and sentrivana and removed request for a team May 6, 2025 09:22
Comment on lines +1543 to +1545
# Argument overrides environment variable
("0", True, True), # env false, arg true
("1", False, False), # env true, arg false
Copy link
Member Author

Choose a reason for hiding this comment

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

Do you all think this order of precedence is reasonable? Or, should we not guarantee any specific order of precedence (i.e. if both are set to different values, behavior is undefined)?

Copy link
Member

Choose a reason for hiding this comment

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

In my opinion the env var should take precedence over the option set in the script (this way it allows the user to change the behavior by setting env vars, no matter was is set or not set in the script)

But if I look at our env var handling (SENTRY_ENVIRONMENT et al), if an option is set in the script, the env vars are not looked at at all.

So your order is OK. Consistency trumps other concerns.

Copy link
Member Author

@szokeasaurusrex szokeasaurusrex May 6, 2025

Choose a reason for hiding this comment

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

I suppose the middle option would be to remove the tests asserting order and not provide any specific guarantee on precedence for now.

That way we could change the precedence later even in a minor release

What do you think @antonpirker?

Copy link
Member

Choose a reason for hiding this comment

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

But we have an order right now: Options set when calling init() are overruling env vars.

And I think we can not change this in a minor release, because it can alter behavior for users that have a init option and an env var set.

the tests are good, because if we change the precedence by accident, the tests will fail.

Copy link
Member Author

Choose a reason for hiding this comment

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

sounds good, then we stay with what we currently have

Copy link

codecov bot commented May 6, 2025

❌ 6 Tests Failed:

Tests completed Failed Passed Skipped
1979 6 1973 15
View the top 3 failed test(s) by shortest run time
tests.integrations.redis.cluster.test_redis_cluster::test_rediscluster_pipeline[False-expected_first_ten0]
Stack Traces | 0.089s run time
.../redis/cluster/test_redis_cluster.py:121: in test_rediscluster_pipeline
    pipeline = rc.pipeline()
.../redis/cluster/test_redis_cluster.py:16: in <lambda>
    redis.RedisCluster.pipeline = lambda *_, **__: pipeline_cls(None, None)
.tox/py3.12-redis-latest/lib/python3.12.../site-packages/redis/utils.py:188: in wrapper
    return func(*args, **kwargs)
.tox/py3.12-redis-latest/lib/python3.12.../site-packages/redis/cluster.py:2125: in __init__
    retries=self.cluster_error_retry_attempts,
E   AttributeError: 'ClusterPipeline' object has no attribute 'cluster_error_retry_attempts'
tests.integrations.redis.cluster.test_redis_cluster::test_rediscluster_pipeline[True-expected_first_ten1]
Stack Traces | 0.089s run time
.../redis/cluster/test_redis_cluster.py:121: in test_rediscluster_pipeline
    pipeline = rc.pipeline()
.../redis/cluster/test_redis_cluster.py:16: in <lambda>
    redis.RedisCluster.pipeline = lambda *_, **__: pipeline_cls(None, None)
.tox/py3.12-redis-latest/lib/python3.12.../site-packages/redis/utils.py:188: in wrapper
    return func(*args, **kwargs)
.tox/py3.12-redis-latest/lib/python3.12.../site-packages/redis/cluster.py:2125: in __init__
    retries=self.cluster_error_retry_attempts,
E   AttributeError: 'ClusterPipeline' object has no attribute 'cluster_error_retry_attempts'
tests.integrations.redis.cluster.test_redis_cluster::test_rediscluster_span_origin
Stack Traces | 0.09s run time
.../redis/cluster/test_redis_cluster.py:162: in test_rediscluster_span_origin
    pipeline = rc.pipeline(transaction=False)
.../redis/cluster/test_redis_cluster.py:16: in <lambda>
    redis.RedisCluster.pipeline = lambda *_, **__: pipeline_cls(None, None)
.tox/py3.12-redis-latest/lib/python3.12.../site-packages/redis/utils.py:188: in wrapper
    return func(*args, **kwargs)
.tox/py3.12-redis-latest/lib/python3.12.../site-packages/redis/cluster.py:2125: in __init__
    retries=self.cluster_error_retry_attempts,
E   AttributeError: 'ClusterPipeline' object has no attribute 'cluster_error_retry_attempts'

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

Copy link
Member

@antonpirker antonpirker left a comment

Choose a reason for hiding this comment

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

Nice!

@@ -142,6 +142,11 @@ def _get_options(*args, **kwargs):
)
rv["socket_options"] = None

if rv["keep_alive"] is None:
rv["keep_alive"] = (
env_to_bool(os.environ.get("SENTRY_KEEP_ALIVE"), strict=True) or False
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
env_to_bool(os.environ.get("SENTRY_KEEP_ALIVE"), strict=True) or False
env_to_bool(os.environ.get("SENTRY_KEEP_ALIVE", "False"), strict=True)

to be consistent with SENTRY_DEBUG handling in the same file.

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'm not sure we should make this change because env_to_bool(os.environ.get("SENTRY_KEEP_ALIVE"), strict=True) or False and env_to_bool(os.environ.get("SENTRY_KEEP_ALIVE", "False"), strict=True) are not equivalent.

For example, if SENTRY_KEEP_ALIVE is set to an invalid value like "invalid", then the current code with or False evaluates to False, whereas your proposal would evaluate to None. Since keep_alive should have a boolean value, I think the current option is preferable.

If anything, I would consider changing the SENTRY_DEBUG handling to match what I have implemented here (although that would need a separate PR)

Copy link
Member

Choose a reason for hiding this comment

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

The important part is that all env vars are handled in the same way.
Its fine if you change the others, as long as all are behaving the same.

Comment on lines +1543 to +1545
# Argument overrides environment variable
("0", True, True), # env false, arg true
("1", False, False), # env true, arg false
Copy link
Member

Choose a reason for hiding this comment

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

In my opinion the env var should take precedence over the option set in the script (this way it allows the user to change the behavior by setting env vars, no matter was is set or not set in the script)

But if I look at our env var handling (SENTRY_ENVIRONMENT et al), if an option is set in the script, the env vars are not looked at at all.

So your order is OK. Consistency trumps other concerns.

Copy link
Member

@antonpirker antonpirker left a comment

Choose a reason for hiding this comment

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

see my comments, otherwise looks good!

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.

Allow setting keep_alive via environment variable.
2 participants