Skip to content

Commit 2492b73

Browse files
authored
Merge pull request #164 from rostilos/1.5.5-rc
feat: Enhance New Relic APM integration with improved initialization …
2 parents 7ed76d2 + 42942fa commit 2492b73

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

python-ecosystem/inference-orchestrator/src/api/app.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ def create_app() -> FastAPI:
8080
def run_http_server(host: str = "0.0.0.0", port: int = 8000):
8181
"""Run the FastAPI application."""
8282
app = create_app()
83+
84+
# Wrap with New Relic ASGI instrumentation when the agent is active.
85+
# initialize() sets up import hooks but cannot wrap the top-level ASGI
86+
# protocol when the app is passed as a Python object to uvicorn.run().
87+
try:
88+
import newrelic.agent
89+
nr_app = newrelic.agent.application()
90+
# application() returns a non-None sentinel even when uninitialised,
91+
# but the settings object is only populated after initialize().
92+
if nr_app and nr_app.settings:
93+
app = newrelic.agent.ASGIApplicationWrapper(app)
94+
logger.info("New Relic ASGI wrapper applied")
95+
except Exception:
96+
pass # NR not installed or not initialized — run without it
97+
8398
import uvicorn
8499
uvicorn.run(app, host=host, port=port, log_level="info", timeout_keep_alive=300)
85100

python-ecosystem/inference-orchestrator/src/main.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@
1414
# ── New Relic APM — must be initialized before any other imports ─────────
1515
import os as _os
1616
_nr_config = _os.environ.get('NEW_RELIC_CONFIG_FILE')
17-
if _nr_config and _os.path.exists(_nr_config):
18-
import newrelic.agent
19-
newrelic.agent.initialize(_nr_config)
17+
print(f"[NR-BOOT] NEW_RELIC_CONFIG_FILE = {_nr_config!r}", flush=True)
18+
if _nr_config:
19+
_nr_exists = _os.path.exists(_nr_config)
20+
print(f"[NR-BOOT] Config file exists: {_nr_exists}", flush=True)
21+
if _nr_exists:
22+
try:
23+
import newrelic.agent
24+
print(f"[NR-BOOT] newrelic.agent imported, version={newrelic.version}", flush=True)
25+
newrelic.agent.initialize(_nr_config)
26+
print("[NR-BOOT] newrelic.agent.initialize() completed successfully", flush=True)
27+
except Exception as _nr_err:
28+
print(f"[NR-BOOT] ERROR during initialization: {_nr_err}", flush=True)
29+
else:
30+
print("[NR-BOOT] Skipping — no config file env var set", flush=True)
2031
# ─────────────────────────────────────────────────────────────────────────
2132

2233
import os

0 commit comments

Comments
 (0)