Skip to content

fix(monitoring): allow distinguishing data from different deployments in New Relic#1311

Merged
mihow merged 1 commit into
mainfrom
fix/newrelic-restore-env-sections
May 20, 2026
Merged

fix(monitoring): allow distinguishing data from different deployments in New Relic#1311
mihow merged 1 commit into
mainfrom
fix/newrelic-restore-env-sections

Conversation

@mihow
Copy link
Copy Markdown
Collaborator

@mihow mihow commented May 19, 2026

Summary

Narrow follow-up to #1299. The NEW_RELIC_APP_NAME env var is silently ignored by newrelic agent 12.1.0 when any [newrelic*] section in config/newrelic.ini sets app_name. The previous PR removed it from [newrelic:production] only and assumed the env var would supply it — but the base [newrelic] section still set it, so the env var was never consulted.

This PR drops app_name from every [newrelic*] section, making NEW_RELIC_APP_NAME in .envs/.production/.django the single source of truth. This is the only configuration shape that actually lets per-deploy app naming work in 12.1.0, and it suits the upcoming dynamic preview stacks (each preview sets its own NEW_RELIC_APP_NAME at spawn time, no repo churn).

Evidence

Reproduced inside a deployed django container (agent 12.1.0):

$ docker exec <c> newrelic-admin run-program python -c \
  ""from newrelic.core.config import global_settings; \
   import os; \
   print('env:', repr(os.environ.get('NEW_RELIC_APP_NAME'))); \
   print('app:', repr(global_settings().app_name))""

env: 'Antenna Backend (Demo)'
app: 'AMI Backend'         ← base ini wins, env var ignored

Comment out the base app_name = AMI Backend line in a temp ini and re-run with the same env var:

env: 'Antenna Backend (Demo)'
app: 'Antenna Backend (Demo)'  ← env var now resolves

Confirms the ini base entry — not the env var read path — is the blocker. Removing app_name from the ini is the fix.

Failure mode is visible, not silent

If a deploy forgets to set NEW_RELIC_APP_NAME, the agent reports under the literal name Python Application. That is an easy-to-spot orphan in NR's APM list, not a silent merge into another env's entity. Verified:

# (no env var, no ini app_name)
$ ... print(global_settings().app_name)
'Python Application'

This is the opposite of the pre-fix behavior, which (combined with an NR account-side alias mapping AMI Backend → the production entity) caused unrelated deploys to silently merge into prod APM metrics. That issue is what surfaced this PR.

Suits dynamic preview stacks

Per-PR / per-branch preview stacks set NEW_RELIC_APP_NAME at spawn time without committing a new [newrelic:<env>] section to this repo for every ephemeral environment.

Deploy coordination — required before merge

Each long-lived environment must set NEW_RELIC_APP_NAME in its deploy-time .envs/.production/.django before this lands. Suggested values (use the Antenna Backend (<env>) pattern for any new entity; the legacy AMI Backend (Staging) entity already exists in NR and can keep that name for history continuity if preferred):

Env NEW_RELIC_APP_NAME
production Antenna Backend (live)
demo Antenna Backend (Demo)
staging Antenna Backend (Staging) (new) or AMI Backend (Staging) (keep)
preview stacks Antenna Backend (preview-<id>)

Adding the env var on hosts where the ini still sets app_name is a no-op today (ini wins), so it can be staged in advance of merging without risk.

Test plan

  • In a running django container, build a temp ini with no app_name anywhere + set NEW_RELIC_APP_NAME env var → global_settings() resolves to the env var value.
  • Same with no env var set → resolves to Python Application (confirmed orphan, no silent merge).
  • Confirmed in NR APM with the corresponding ini hot-patch on one env: agent registers under the env-var name, transactions land in the expected entity (not another env's entity).
  • After deploy: each env's appName in NR matches the value of its NEW_RELIC_APP_NAME env var; no env reports as Python Application or AMI Backend.
  • Agent log shows NR-Activate-Session/<expected app name> per env.

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings May 19, 2026 23:56
@netlify
Copy link
Copy Markdown

netlify Bot commented May 19, 2026

Deploy Preview for antenna-ssec canceled.

Name Link
🔨 Latest commit 04b265b
🔍 Latest deploy log https://app.netlify.com/projects/antenna-ssec/deploys/6a0cfe9a2c42370008fea9cb

@netlify
Copy link
Copy Markdown

netlify Bot commented May 19, 2026

Deploy Preview for antenna-preview canceled.

Name Link
🔨 Latest commit 04b265b
🔍 Latest deploy log https://app.netlify.com/projects/antenna-preview/deploys/6a0cfe9addaa7900082a1617

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 19, 2026

Warning

Rate limit exceeded

@mihow has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 19 minutes and 26 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a96c34c1-c64f-436e-86c3-fc83147b4b0c

📥 Commits

Reviewing files that changed from the base of the PR and between f4fb6f1 and 04b265b.

📒 Files selected for processing (2)
  • .envs/.production/.django-example
  • config/newrelic.ini
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/newrelic-restore-env-sections

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR restores deterministic per-environment New Relic app_name selection via config/newrelic.ini sections after finding that, in newrelic agent 12.1.0, NEW_RELIC_APP_NAME does not override a base [newrelic] app_name value—preventing demo/staging traffic from being attributed to the wrong NR entity.

Changes:

  • Re-add explicit app_name for [newrelic:production] and introduce [newrelic:demo], both keyed by NEW_RELIC_ENVIRONMENT.
  • Remove the misleading NEW_RELIC_APP_NAME line from the production env template and document the agent behavior/precedence.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
config/newrelic.ini Adds [newrelic:demo] and restores per-env app_name for production; documents why app_name must live in env sections.
.envs/.production/.django-example Removes NEW_RELIC_APP_NAME template line and adds guidance to use NEW_RELIC_ENVIRONMENT-selected ini sections for app naming.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread config/newrelic.ini Outdated
# more aggressive tracing via NEW_RELIC_* env vars at deploy time.
#
# NOTE: app_name override via NEW_RELIC_APP_NAME env var does NOT work in
# agent 12.1.0 — the base [newrelic] section's app_name wins even when the
Comment thread config/newrelic.ini Outdated
Comment on lines 300 to 304
[newrelic:demo]
app_name = Antenna Backend (Demo)
monitor_mode = true

[newrelic:production]
Comment thread .envs/.production/.django-example Outdated
Comment on lines +67 to +69
# app_name is NOT settable via env var (NEW_RELIC_APP_NAME is ignored by
# agent 12.1.0 when [newrelic] sets app_name). Use NEW_RELIC_ENVIRONMENT
# to select a [newrelic:<env>] section in config/newrelic.ini, which is
Drops `app_name` from every `[newrelic*]` section in `config/newrelic.ini`
and requires each deploy to set `NEW_RELIC_APP_NAME` in
`.envs/.production/.django`. This is the only configuration shape that
actually lets per-deploy app names work in `newrelic` agent 12.1.0.

## Why this design

PR #1299 intended `NEW_RELIC_APP_NAME` to override per-env. Empirically
that doesn't work when `[newrelic]` (or any `[newrelic:<env>]` matched by
`NEW_RELIC_ENVIRONMENT`) sets `app_name` — the ini value wins and the
env var is silently ignored. Verified in a deployed django container:

```
$ docker exec <c> newrelic-admin run-program python -c \
  "from newrelic.core.config import global_settings; \
   print(global_settings().app_name)"
# env NEW_RELIC_APP_NAME='Antenna Backend (Demo)'
# resolved 'AMI Backend'  ← base ini wins
```

Remove the base `app_name` line and the env var resolves correctly:

```
# (same command, ini base app_name commented out)
# resolved 'Antenna Backend (Demo)'
```

So the fix is to leave `app_name` unset everywhere in the ini and treat
the env var as the canonical source. None of `[newrelic]`,
`[newrelic:development]`, `[newrelic:staging]`, `[newrelic:demo]`,
`[newrelic:production]` set app_name anymore — `NEW_RELIC_APP_NAME`
in `.envs/.production/.django` defines it per deploy.

## Failure mode is visible, not silent

If a deploy forgets to set `NEW_RELIC_APP_NAME`, the agent reports under
the literal name `Python Application`. That is an easy-to-spot orphan in
NR's APM list, not a silent merge into another env's entity. This is the
opposite of the pre-fix behavior, which (combined with an NR account
alias mapping `AMI Backend` → the production entity) caused unrelated
deploys to silently merge into prod APM metrics.

## Suits dynamic preview stacks

Per-PR / per-branch preview stacks can set `NEW_RELIC_APP_NAME` at spawn
time without committing a new `[newrelic:<env>]` section to this repo
for every ephemeral environment.

## Deploy coordination — required before merge

Each long-lived environment must set `NEW_RELIC_APP_NAME` in its
deploy-time `.envs/.production/.django` BEFORE this lands, otherwise it
will start reporting under `Python Application`. Values to set:

| Env | NEW_RELIC_APP_NAME |
|-----|--------------------|
| production | `Antenna Backend (live)` |
| demo | `Antenna Backend (Demo)` |
| staging | `AMI Backend (Staging)` |
| any preview stack | `Antenna Backend (preview-<id>)` |

Adding the env var on hosts where the ini still sets `app_name` is a
no-op (the ini wins today), so it can be staged in advance of merging
this PR without any risk.

## Test plan

- [x] In a running django container, build a temp ini with no app_name
      anywhere + set `NEW_RELIC_APP_NAME` env var → `global_settings()`
      resolves to the env var value.
- [x] Same with no env var set → resolves to `Python Application`
      (confirmed orphan, no silent merge).
- [ ] After deploy: each env's `appName` in NR matches the value of its
      `NEW_RELIC_APP_NAME` env var; no env reports as `Python Application`
      or `AMI Backend`.
- [ ] CI agent log shows `NR-Activate-Session/<expected app name>` per env.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mihow mihow force-pushed the fix/newrelic-restore-env-sections branch from 2c877ca to 04b265b Compare May 20, 2026 00:21
@mihow mihow changed the title fix(newrelic): restore per-env app_name in ini — NEW_RELIC_APP_NAME env var doesn't override fix(newrelic): make app_name env-var-driven (drop from ini) May 20, 2026
@mihow mihow merged commit e721c8b into main May 20, 2026
7 checks passed
@mihow mihow deleted the fix/newrelic-restore-env-sections branch May 20, 2026 01:25
@mihow mihow changed the title fix(newrelic): make app_name env-var-driven (drop from ini) fix(monitoring): allow distinguishing data from different deployments in New Relic May 20, 2026
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.

2 participants