Skip to content

Commit c512273

Browse files
committed
disable django instrumentation if DJANGO_SETTINGS_MODULE is not set
1 parent 985be39 commit c512273

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_distro.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import sys
1717
from logging import ERROR, Logger, getLogger
1818

19-
from amazon.opentelemetry.distro._utils import get_aws_region, is_agent_observability_enabled
19+
from amazon.opentelemetry.distro._utils import get_aws_region, is_agent_observability_enabled, is_installed
2020
from amazon.opentelemetry.distro.aws_opentelemetry_configurator import (
2121
APPLICATION_SIGNALS_ENABLED_CONFIG,
2222
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT,
@@ -86,6 +86,21 @@ def _configure(self, **kwargs):
8686
if cwd_path not in sys.path:
8787
sys.path.insert(0, cwd_path)
8888

89+
# Check if Django is installed and determine if Django instrumentation should be enabled
90+
if is_installed("django"):
91+
# Django instrumentation is allowed when DJANGO_SETTINGS_MODULE is set
92+
if not os.getenv("DJANGO_SETTINGS_MODULE"):
93+
# DJANGO_SETTINGS_MODULE is not set, disable Django instrumentation
94+
disabled_instrumentations = os.getenv("OTEL_PYTHON_DISABLED_INSTRUMENTATIONS", "")
95+
os.environ["OTEL_PYTHON_DISABLED_INSTRUMENTATIONS"] = disabled_instrumentations + ",django"
96+
_logger.warning(
97+
"Django is installed but DJANGO_SETTINGS_MODULE is not set. Disabling django instrumentation."
98+
)
99+
else:
100+
_logger.debug(
101+
"Django instrumentation enabled: DJANGO_SETTINGS_MODULE=%s", os.getenv("DJANGO_SETTINGS_MODULE")
102+
)
103+
89104
os.environ.setdefault(OTEL_EXPORTER_OTLP_PROTOCOL, "http/protobuf")
90105

91106
if os.environ.get(OTEL_PROPAGATORS, None) is None:

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_aws_opentelemetry_distro.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def setUp(self):
3232
"OTEL_PYTHON_DISABLED_INSTRUMENTATIONS",
3333
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED",
3434
"OTEL_AWS_APPLICATION_SIGNALS_ENABLED",
35+
"DJANGO_SETTINGS_MODULE",
3536
]
3637

3738
# First, save all current values
@@ -277,3 +278,116 @@ def test_otel_propagators_added_when_not_user_defined(self, mock_super_configure
277278
for prop in individual_propagators:
278279
actual_propagators.append(type(prop).__name__)
279280
self.assertEqual(expected_propagators, actual_propagators)
281+
282+
# Django Instrumentation Tests
283+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.is_installed")
284+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches")
285+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.OpenTelemetryDistro._configure")
286+
def test_django_instrumentation_enabled_with_settings_module(
287+
self, mock_super_configure, mock_apply_patches, mock_is_installed
288+
):
289+
"""Test that Django instrumentation is enabled when DJANGO_SETTINGS_MODULE is set"""
290+
mock_is_installed.return_value = True
291+
os.environ["DJANGO_SETTINGS_MODULE"] = "myproject.settings"
292+
293+
distro = AwsOpenTelemetryDistro()
294+
distro._configure()
295+
296+
# Verify that django is NOT in disabled instrumentations
297+
disabled_instrumentations = os.environ.get("OTEL_PYTHON_DISABLED_INSTRUMENTATIONS", "")
298+
self.assertNotIn("django", disabled_instrumentations)
299+
300+
mock_is_installed.assert_called_once_with("django")
301+
302+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.is_installed")
303+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches")
304+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.OpenTelemetryDistro._configure")
305+
def test_django_instrumentation_disabled_without_settings_module(
306+
self, mock_super_configure, mock_apply_patches, mock_is_installed
307+
):
308+
"""Test that Django instrumentation is disabled when DJANGO_SETTINGS_MODULE is not set"""
309+
mock_is_installed.return_value = True
310+
311+
distro = AwsOpenTelemetryDistro()
312+
distro._configure()
313+
314+
# Verify that django is in disabled instrumentations
315+
disabled_instrumentations = os.environ.get("OTEL_PYTHON_DISABLED_INSTRUMENTATIONS", "")
316+
self.assertIn("django", disabled_instrumentations)
317+
318+
mock_is_installed.assert_called_once_with("django")
319+
320+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.is_installed")
321+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches")
322+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.OpenTelemetryDistro._configure")
323+
def test_django_instrumentation_disabled_with_existing_disabled_instrumentations(
324+
self, mock_super_configure, mock_apply_patches, mock_is_installed
325+
):
326+
"""Test that Django is appended to existing disabled instrumentations"""
327+
mock_is_installed.return_value = True
328+
os.environ["OTEL_PYTHON_DISABLED_INSTRUMENTATIONS"] = "flask,fastapi"
329+
330+
distro = AwsOpenTelemetryDistro()
331+
distro._configure()
332+
333+
# Verify that django is appended to existing disabled instrumentations
334+
disabled_instrumentations = os.environ.get("OTEL_PYTHON_DISABLED_INSTRUMENTATIONS", "")
335+
self.assertEqual("flask,fastapi,django", disabled_instrumentations)
336+
337+
mock_is_installed.assert_called_once_with("django")
338+
339+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.is_installed")
340+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches")
341+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.OpenTelemetryDistro._configure")
342+
def test_django_not_installed_no_effect(self, mock_super_configure, mock_apply_patches, mock_is_installed):
343+
"""Test that when Django is not installed, no changes are made to disabled instrumentations"""
344+
mock_is_installed.return_value = False
345+
346+
distro = AwsOpenTelemetryDistro()
347+
distro._configure()
348+
349+
# Verify that OTEL_PYTHON_DISABLED_INSTRUMENTATIONS is not affected
350+
disabled_instrumentations = os.environ.get("OTEL_PYTHON_DISABLED_INSTRUMENTATIONS", "")
351+
self.assertEqual("", disabled_instrumentations)
352+
353+
mock_is_installed.assert_called_once_with("django")
354+
355+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.is_installed")
356+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches")
357+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.OpenTelemetryDistro._configure")
358+
def test_django_instrumentation_enabled_with_settings_module_and_existing_disabled(
359+
self, mock_super_configure, mock_apply_patches, mock_is_installed
360+
):
361+
"""Test that Django instrumentation is enabled even with existing disabled instrumentations"""
362+
mock_is_installed.return_value = True
363+
os.environ["DJANGO_SETTINGS_MODULE"] = "myproject.settings"
364+
os.environ["OTEL_PYTHON_DISABLED_INSTRUMENTATIONS"] = "flask,fastapi"
365+
366+
distro = AwsOpenTelemetryDistro()
367+
distro._configure()
368+
369+
# Verify that django is NOT added to disabled instrumentations
370+
disabled_instrumentations = os.environ.get("OTEL_PYTHON_DISABLED_INSTRUMENTATIONS", "")
371+
self.assertEqual("flask,fastapi", disabled_instrumentations)
372+
self.assertNotIn("django", disabled_instrumentations)
373+
374+
mock_is_installed.assert_called_once_with("django")
375+
376+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.is_installed")
377+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.apply_instrumentation_patches")
378+
@patch("amazon.opentelemetry.distro.aws_opentelemetry_distro.OpenTelemetryDistro._configure")
379+
def test_django_instrumentation_disabled_empty_settings_module(
380+
self, mock_super_configure, mock_apply_patches, mock_is_installed
381+
):
382+
"""Test that Django instrumentation is disabled when DJANGO_SETTINGS_MODULE is empty"""
383+
mock_is_installed.return_value = True
384+
os.environ["DJANGO_SETTINGS_MODULE"] = ""
385+
386+
distro = AwsOpenTelemetryDistro()
387+
distro._configure()
388+
389+
# Verify that django is in disabled instrumentations
390+
disabled_instrumentations = os.environ.get("OTEL_PYTHON_DISABLED_INSTRUMENTATIONS", "")
391+
self.assertIn("django", disabled_instrumentations)
392+
393+
mock_is_installed.assert_called_once_with("django")

0 commit comments

Comments
 (0)