Skip to content

Instantly share code, notes, and snippets.

@a-recknagel
Last active October 28, 2020 21:36
Show Gist options
  • Save a-recknagel/188330ad877b310f883e3d9bf4cd8379 to your computer and use it in GitHub Desktop.
Save a-recknagel/188330ad877b310f883e3d9bf4cd8379 to your computer and use it in GitHub Desktop.
fastapi + newrelic mcve with late license_key plugging bug
#import os
#os.environ["NEW_RELIC_LICENSE_KEY"] = "some_valid_key"
import sys
import logging
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
import newrelic.agent
newrelic.agent.initialize("newrelic.ini")
newrelic.agent.global_settings().license_key = "some_valid_key"
newrelic.agent.register_application()
import fastapi
app = fastapi.FastAPI()
@app.get("/")
async def health():
return "hello world!"
[newrelic]
app_name = HelloWorld
monitor_mode = true
fastapi==0.60.2
newrelic==5.22.0.151
uvicorn==0.12.2
@a-recknagel
Copy link
Author

Set up virtual env, activate it, install dependencies, and run server:

$ python -m venv venv
$ source venv/bin/activate
(venv)$ pip install -r requirements.txt
(venv)$ uvicorn app:app 

Should result in

CRITICAL:newrelic.core.agent_protocol:Disconnection of the agent has been requested by the data collector for the application where the agent run was None. Please contact New Relic support for further information.

Using os.environ to plug in the license on the other hand:

INFO:newrelic.core.agent_protocol:Reporting to: https://rpm.eu.newrelic.com/accounts/xxxxx/applications/xxxxxx

@a-recknagel
Copy link
Author

a-recknagel commented Oct 28, 2020

Found the solution: If using an EU based license key, you need to set the host together with the license:

newrelic.agent.initialize("newrelic.ini")
newrelic.agent.global_settings().license_key = "some_valid_key"
newrelic.agent.global_settings().host = newrelic.core.config.default_host("some_valid_key")  # EU-only
newrelic.agent.register_application()

Cheers to a-feld for debugging it for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment