Skip to content

Commit

Permalink
fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdinur committed Dec 19, 2024
1 parent 6871715 commit 4bb4c5e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 36 deletions.
3 changes: 2 additions & 1 deletion ddtrace/contrib/internal/requests/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ddtrace.internal.schema.span_attribute_schema import SpanDirection
from ddtrace.internal.utils import get_argument_value
from ddtrace.propagation.http import HTTPPropagator
from ddtrace.settings.asm import config as asm_config


log = get_logger(__name__)
Expand Down Expand Up @@ -59,7 +60,7 @@ def _wrap_send(func, instance, args, kwargs):
tracer = getattr(instance, "datadog_tracer", ddtrace.tracer)

# skip if tracing is not enabled
if not tracer.enabled:
if not tracer.enabled and not asm_config._apm_opt_out:
return func(*args, **kwargs)

request = get_argument_value(args, kwargs, 0, "request")
Expand Down
3 changes: 2 additions & 1 deletion ddtrace/pin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import wrapt

import ddtrace
from ddtrace.settings.asm import config as asm_config

from .internal.logger import get_logger

Expand Down Expand Up @@ -144,7 +145,7 @@ def override(
def enabled(self):
# type: () -> bool
"""Return true if this pin's tracer is enabled."""
return bool(self.tracer) and self.tracer.enabled
return bool(self.tracer) and (self.tracer.enabled or asm_config._apm_opt_out)

def onto(self, obj, send=True):
# type: (Any, bool) -> None
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/settings/asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def _api_security_feature_active(self) -> bool:
@property
def _apm_opt_out(self) -> bool:
return (
self._asm_enabled or self._iast_enabled or tracer_config._sca_enabled
self._asm_enabled or self._iast_enabled or tracer_config._sca_enabled is True
) and self._appsec_standalone_enabled

@property
Expand Down
2 changes: 0 additions & 2 deletions tests/contrib/flask/test_flask_appsec_iast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,8 +1379,6 @@ def setUp(self):
)
):
super(FlaskAppSecIASTDisabledTestCase, self).setUp()
self.tracer._iast_enabled = False
self.tracer._asm_enabled = False
self.tracer.configure(api_version="v0.4")

@pytest.mark.skipif(not python_supported_by_iast(), reason="Python version not supported by IAST")
Expand Down
66 changes: 35 additions & 31 deletions tests/contrib/requests/test_requests_distributed.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from requests_mock import Adapter

from ddtrace import config
from ddtrace.settings.asm import config as asm_config
from tests.utils import TracerTestCase
from tests.utils import get_128_bit_trace_id_from_headers

Expand Down Expand Up @@ -117,45 +118,48 @@ def matcher(request):

def test_propagation_apm_opt_out_true(self):
# ensure distributed tracing works when APM is opted out
self.tracer.enabled = False
with self.override_global_config(dict(_appsec_standalone_enabled=True, _asm_enabled=True)):
assert asm_config._apm_opt_out
self.tracer.enabled = False
cfg = config.get_from(self.session)
cfg["distributed_tracing"] = True
adapter = Adapter()
self.session.mount("mock", adapter)

cfg = config.get_from(self.session)
cfg["distributed_tracing"] = True
adapter = Adapter()
self.session.mount("mock", adapter)
with self.tracer.trace("root") as root:

with self.tracer.trace("root") as root:
def matcher(request):
return self.headers_here(self.tracer, request, root)

def matcher(request):
return self.headers_here(self.tracer, request, root)
adapter.register_uri("GET", "mock://datadog/foo", additional_matcher=matcher, text="bar")
resp = self.session.get("mock://datadog/foo")
assert 200 == resp.status_code
assert "bar" == resp.text

adapter.register_uri("GET", "mock://datadog/foo", additional_matcher=matcher, text="bar")
resp = self.session.get("mock://datadog/foo")
assert 200 == resp.status_code
assert "bar" == resp.text

spans = self.pop_spans()
root, req = spans
assert "root" == root.name
assert "requests.request" == req.name
assert root.trace_id == req.trace_id
assert root.span_id == req.parent_id
spans = self.pop_spans()
root, req = spans
assert "root" == root.name
assert "requests.request" == req.name
assert root.trace_id == req.trace_id
assert root.span_id == req.parent_id

def test_propagation_apm_opt_out_false(self):
# ensure distributed tracing doesn't works when APM is disabled but not opted out
self.tracer.enabled = False
with self.override_global_config(dict(_appsec_standalone_enabled=False, _asm_enabled=True)):
assert not asm_config._apm_opt_out
self.tracer.enabled = False

cfg = config.get_from(self.session)
cfg["distributed_tracing"] = True
adapter = Adapter()
self.session.mount("mock", adapter)
cfg = config.get_from(self.session)
cfg["distributed_tracing"] = True
adapter = Adapter()
self.session.mount("mock", adapter)

with self.tracer.trace("root"):
with self.tracer.trace("root"):

def matcher(request):
return self.headers_not_here(self.tracer, request)
def matcher(request):
return self.headers_not_here(self.tracer, request)

adapter.register_uri("GET", "mock://datadog/foo", additional_matcher=matcher, text="bar")
resp = self.session.get("mock://datadog/foo")
assert 200 == resp.status_code
assert "bar" == resp.text
adapter.register_uri("GET", "mock://datadog/foo", additional_matcher=matcher, text="bar")
resp = self.session.get("mock://datadog/foo")
assert 200 == resp.status_code
assert "bar" == resp.text

0 comments on commit 4bb4c5e

Please sign in to comment.