[Python] How to reference newrelic keys via settings.py

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am just using NewRelic for the first time. I have the new relic.ini file, which includes the license_key and app_name hard coded, in my Django Python repository and have updated my manage.py script to be as so:

    #!/usr/bin/env python
    """Django's command-line utility for administrative tasks."""
    import newrelic.agent
    newrelic.agent.initialize('newrelic.ini')

    import os
    import sys


    def main():

    .....

    if __name__ == '__main__':
    main()


    When I run my api locally via ./manage.py runserver I can see the logs being tracked in NewRelic which is good.

    However, I'm wanting to remove the hardcoded license_key and app_name from the newrelic.ini file so that I can reference environment variables. I have read this documentation https://docs.newrelic.com/docs/apm/...gent-configuration/#server-side-configuration which outlines the environment and config variables.

    I added this to my settings.py file so that I can override the env variables on deployment:

    NEW_RELIC_APP_NAME = "My API"

    NEW_RELIC_LICENSE_KEY="XXXXXXXXXXXXXXXX"


    I have tried

    • removing license_key and app_name entirely from newrelic.ini
    • keeping the variables but setting them equal to ""
    • keeping the variables but setting license_key = NEW_RELIC_LICENSE_KEY

    but I can't seem to get it tracking without it being hardcoded in the newrelic.ini file

    So my question - how can I use environment variables with the newrelic.ini file?

    thanks in advance

    Continue reading...

Compartilhe esta Página