1""" 2pytest local plugin used to automatically make the following fixtures 3available for all tests in this directory 4 5https://docs.pytest.org/en/latest/writing_plugins.html#testing-plugins 6""" 7import pytest 8 9from ddtrace.opentracer import Tracer 10from ddtrace.opentracer import set_global_tracer 11from tests.utils import DummyTracer 12from tests.utils import TracerSpanContainer 13 14 15@pytest.fixture() 16def ot_tracer_factory(): 17 """Fixture which returns an opentracer ready to use for testing.""" 18 19 def make_ot_tracer(service_name="my_svc", config=None, scope_manager=None, context_provider=None): 20 config = config or {} 21 tracer = Tracer(service_name=service_name, config=config, scope_manager=scope_manager) 22 23 # similar to how we test the ddtracer, use a dummy tracer 24 dd_tracer = DummyTracer() 25 if context_provider: 26 dd_tracer.configure(context_provider=context_provider) 27 28 # attach the dummy tracer to the opentracer 29 tracer._dd_tracer = dd_tracer 30 return tracer 31 32 return make_ot_tracer 33 34 35@pytest.fixture() 36def ot_tracer(ot_tracer_factory): 37 """Fixture for a default opentracer.""" 38 return ot_tracer_factory() 39 40 41@pytest.fixture 42def test_spans(ot_tracer): 43 container = TracerSpanContainer(ot_tracer._dd_tracer) 44 yield container 45 container.reset() 46 47 48@pytest.fixture() 49def global_tracer(ot_tracer): 50 """A function similar to one OpenTracing users would write to initialize 51 their OpenTracing tracer. 52 """ 53 set_global_tracer(ot_tracer) 54 55 return ot_tracer 56 57 58@pytest.fixture() 59def dd_tracer(ot_tracer): 60 return ot_tracer._dd_tracer 61