Skip to content

Commit 7bf955a

Browse files
authored
Fix test spans (#631)
* Try to fix test span Add a span for scenarios and use it as parent. * Trigger CI * Try to change patch * Include undo in spans
1 parent 42cbe99 commit 7bf955a

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

tests/conftest.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import os
55

6-
# First patch httplib
6+
# First patch urllib
77
tracer = None
88
try:
99
from ddtrace import config, patch, tracer
@@ -18,8 +18,7 @@
1818
)
1919
tracer.configure(writer)
2020

21-
config.httplib["distributed_tracing"] = True
22-
patch(httplib=True)
21+
patch(urllib3=True)
2322

2423
from pytest import hookimpl
2524

@@ -80,6 +79,28 @@ def escape_reserved_keyword(word):
8079
return word
8180

8281

82+
def pytest_bdd_before_scenario(request, feature, scenario):
83+
if tracer is None:
84+
return
85+
86+
span = tracer.start_span(
87+
scenario.name,
88+
span_type="scenario",
89+
child_of=tracer.current_trace_context(),
90+
activate=True,
91+
)
92+
setattr(scenario, "__dd_span__", span)
93+
94+
95+
def pytest_bdd_after_scenario(request, feature, scenario):
96+
ctx = request.getfixturevalue("context")
97+
for undo in reversed(ctx["undo_operations"]):
98+
undo()
99+
span = getattr(scenario, "__dd_span__", None)
100+
if span is not None:
101+
span.finish()
102+
103+
83104
def pytest_bdd_before_step(request, feature, scenario, step, step_func):
84105
if tracer is None:
85106
return
@@ -88,7 +109,8 @@ def pytest_bdd_before_step(request, feature, scenario, step, step_func):
88109
step.type,
89110
resource=step.name,
90111
span_type=step.type,
91-
child_of=tracer.current_trace_context(),
112+
child_of=getattr(scenario, "__dd_span__"),
113+
activate=True,
92114
)
93115
setattr(step_func, "__dd_span__", span)
94116

@@ -230,9 +252,6 @@ def context(vcr, unique, unique_lower, freezer):
230252

231253
yield ctx
232254

233-
for undo in reversed(ctx["undo_operations"]):
234-
undo()
235-
236255

237256
@pytest.fixture(scope="session")
238257
def record_mode(request):

0 commit comments

Comments
 (0)