Skip to content

fix: DjangoInstrumentor poisons settings when OTEL_EXPORTER_OTLP_ENDPOINT is set#192

Merged
khvn26 merged 1 commit intomainfrom
fix/otel-settings-poisoning
Apr 6, 2026
Merged

fix: DjangoInstrumentor poisons settings when OTEL_EXPORTER_OTLP_ENDPOINT is set#192
khvn26 merged 1 commit intomainfrom
fix/otel-settings-poisoning

Conversation

@khvn26
Copy link
Copy Markdown
Member

@khvn26 khvn26 commented Apr 4, 2026

Closes #191.

In this PR, we reorder ensure_cli_env() so that sys.path.append(os.getcwd()) and os.environ.setdefault("DJANGO_SETTINGS_MODULE", ...) run before the OTel setup block. Previously, DjangoInstrumentor().instrument() accessed settings.MIDDLEWARE before the project directory was on sys.path, causing Django to fail the settings import. The instrumentor silently caught the error and called settings.configure() with empty defaults, poisoning Django for the rest of the process — making commands like flagsmith docgen disappear.

Also, we migrate test_main.py from unittest.mock.patch/patch.dict to pytest-mock/monkeypatch.setenv, and add tests for the docgen and task-processor argv branches.

@khvn26 khvn26 requested a review from a team as a code owner April 4, 2026 22:37
@khvn26 khvn26 requested review from Zaimwa9 and removed request for a team April 4, 2026 22:37
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.95%. Comparing base (396f138) to head (a0e4e1c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #192      +/-   ##
==========================================
+ Coverage   96.88%   96.95%   +0.06%     
==========================================
  Files         101      101              
  Lines        3983     4008      +25     
==========================================
+ Hits         3859     3886      +27     
+ Misses        124      122       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

DjangoInstrumentor accesses settings.MIDDLEWARE during instrument(),
which requires the project directory on sys.path and
DJANGO_SETTINGS_MODULE set. Previously these were set after the OTel
block, causing the instrumentor to call settings.configure() with
empty defaults and poisoning Django for the rest of the process.

Also migrates test_main to pytest-mock (MockerFixture) and
monkeypatch.setenv.

Fixes #191
@khvn26 khvn26 force-pushed the fix/otel-settings-poisoning branch from 4cb2c78 to a0e4e1c Compare April 4, 2026 23:47
@khvn26 khvn26 requested a review from Zaimwa9 April 6, 2026 09:25
@khvn26
Copy link
Copy Markdown
Member Author

khvn26 commented Apr 6, 2026

Tested on Depot CI (where OTEL_EXPORTER_OTLP_ENDPOINT is set as a platform default). With the fix installed from this branch, flagsmith help shows the full command list including common.core commands. Without the fix, only [django] commands appear because DjangoInstrumentor poisons settings before CWD is on sys.path.

@khvn26 khvn26 merged commit 404031a into main Apr 6, 2026
4 checks passed
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.

DjangoInstrumentor poisons settings when OTEL_EXPORTER_OTLP_ENDPOINT is set

3 participants