Example #1
0
def test_tracer_usage_fork():
    q = MPQueue()
    pid = os.fork()

    # Similar test to test_fork() above except we use the tracer API.
    # In this case we expect to never have collisions.
    if pid > 0:
        # parent
        parent_ids_list = list(
            chain.from_iterable((s.span_id, s.trace_id) for s in [tracer.start_span("s") for _ in range(100)])
        )
        parent_ids = set(parent_ids_list)
        assert len(parent_ids) == len(parent_ids_list), "Collisions found in parent process ids"

        child_ids_list = q.get()

        child_ids = set(child_ids_list)

        assert len(child_ids) == len(child_ids_list), "Collisions found in child process ids"
        assert parent_ids & child_ids == set()
    else:
        # child
        try:
            child_ids = list(
                chain.from_iterable((s.span_id, s.trace_id) for s in [tracer.start_span("s") for _ in range(100)])
            )
            q.put(child_ids)
        finally:
            # Kill the process so it doesn't continue running the rest of the
            # test suite in a separate process. Note we can't use sys.exit()
            # as it raises an exception that pytest will detect as an error.
            os._exit(0)
Example #2
0
def test_tracer_usage_multiprocess():
    q = MPQueue()

    # Similar to test_multiprocess(), ensures that no collisions are
    # generated between parent and child processes while using
    # multiprocessing.

    # Note that we have to be wary of the size of the underlying
    # pipe in the queue: https://bugs.python.org/msg143081

    def target(q):
        ids_list = list(
            chain.from_iterable((s.span_id, s.trace_id) for s in [tracer.start_span("s") for _ in range(10)])
        )
        q.put(ids_list)

    ps = [mp.Process(target=target, args=(q,)) for _ in range(30)]
    for p in ps:
        p.start()

    for p in ps:
        p.join()

    ids_list = list(chain.from_iterable((s.span_id, s.trace_id) for s in [tracer.start_span("s") for _ in range(100)]))
    ids = set(ids_list)
    assert len(ids) == len(ids_list), "Collisions found in ids"

    while not q.empty():
        child_ids_list = q.get()
        child_ids = set(child_ids_list)

        assert len(child_ids) == len(child_ids_list), "Collisions found in subprocess ids"

        assert ids & child_ids == set()
        ids = ids | child_ids  # accumulate the ids
Example #3
0
 def parse(self, response):
     for quote in response.css("div.quote"):
         with tracer.start_span("producer.parse") as span:
             return {
                 "content": quote.css("span.text::text").get(),
                 "span": span
             }
def pytest_bdd_before_step(request, feature, scenario, step, step_func):
    if tracer is None:
        return

    context = tracer.get_call_context()
    span = tracer.start_span(step.type, resource=step.name, span_type=step.type, child_of=context,)
    setattr(step_func, "__dd_span__", span)
Example #5
0
def send_to_queue(content, span):
    tracer.context_provider.activate(span.context)
    with tracer.start_span("producer.send", child_of=span):
        client = boto3.client("sqs")
        client.send_message(
            QueueUrl=os.environ.get("QUEUE_URL"),
            MessageBody=content,
        )
Example #6
0
 def target(q):
     ids_list = list(
         chain.from_iterable((s.span_id, s.trace_id) for s in [tracer.start_span("s") for _ in range(10)])
     )
     q.put(ids_list)
Example #7
0
def _test_tracer_usage_multiprocess_target(q):
    ids_list = list(
        chain.from_iterable(
            (s.span_id, s.trace_id)
            for s in [tracer.start_span("s") for _ in range(10)]))
    q.put(ids_list)
Example #8
0
from ddtrace import tracer
from time import sleep

while True:
    span = tracer.start_span(name='test-python', service='tracegen', resource='python' )
    span.finish()
    sleep(1)