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.

Allow more configuration via environment variables

See original GitHub issue

As the operator of a platform on which many different Python services are deployed, it would be enormously helpful to be able to configure more sentry SDK options via environment variables, in addition to the currently-supported SENTRY_DSN, SENTRY_ENVIRONMENT, and SENTRY_RELEASE options.

In particular, we would like to be able to set the value of the request_bodies and with_locals config options via environment variables, which we disable in production to help mitigate the risk of accidentally capturing PII, so that we can control them at the platform level rather than needing to copy/paste (and maintain) the same boilerplate code across each codebase.

We currently have the equivalent of this boilerplate code copy/pasted across the fleet of Python apps:

# SENTRY_DSN, SENTRY_ENVIRONMENT, and SENTRY_RELEASE are already set
# automatically in the process's environment at deploy time.

sentry_sdk.init(
    integrations=[DjangoIntegration()],
    request_bodies="never" if DEPLOY_ENV == "production" else "small",
    with_locals=DEPLOY_ENV != "production",
)

It would be nice to replace that with something more like

sentry_sdk.init(
    integrations=[DjangoIntegration()],
)

which is less complex and less prone to accidental misconfiguration.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
antonpirkercommented, Feb 24, 2022

I think loading config from the environment is not part of the SDKs job. If we add all the config parameters to be loaded from the environment some users will still need to do something like this:

import os

request_bodies = os.environ.get("SENTRY_REQUEST_BODIES")
if something_is_true:
    request_bodies = "some-other-value"

sentry_sdk.init(
    integrations=[DjangoIntegration()],
    request_bodies=request_bodies,
)

So no matter how we would implement this, it would never make everybody happy.

So imho the preferred way to do this would be something like this:

import os
dsn = os.environ.get("SENTRY_DSN", "[default-dsn]")
debug = os.environ.get("SENTRY_DEBUG", True)
release = os.environ.get("SENTRY_RELEASE", "x.x.x")
environment = os.environ.get("SENTRY_ENVIRONMENT, "local")
sample_rate = os.environ.get("SENTRY_SAMPLE_RATE", 0.2)

sentry_sdk.init(
    dsn=dsn,
    integrations=[DjangoIntegration()],
    debug=debug,
    release=release,
    environment=environment,
    sample_rate=sample_rate,
)

This way you can see what environment variables the config expects to be set and what default values are used in case they are not set. Makes the configuration explicit and not implicit. See also https://www.python.org/dev/peps/pep-0020/

1reaction
sl0thentr0pycommented, Jan 25, 2022

@n1ngu I don’t think we’ll ever do an exhaustive environment config mapping, that doesn’t make sense to me. A few key options are fine to expose as env values for convenience but generalizing even function configs is overkill in my opinion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Environment Variables and Secrets - Render
Click on Environment in the left pane, and then click on Add Environment Variable. Service environment. Enter the Key and Value for your...
Read more >
24. Externalized Configuration - Spring
You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration.
Read more >
How to Use Environment Variables the Right Way
Instead of using environment variables (dependencies) directly, we inject them at callsites (that is, the place they are actually used). This ...
Read more >
Use environment variables in the configuration - Elastic
You can use environment variable references in the config file to set values that need to be configurable during deployment. To do this,...
Read more >
Use multiple environments in ASP.NET Core | Microsoft Learn
Environments · Set the environment by setting an environment variable · Set the environment in code · Configuration by environment · Configure ......
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