def test_thread_to_span_thread_isolation(tracer_and_collector): t, c = tracer_and_collector root = t.start_span("root") thread_id = _nogevent.thread_get_ident() assert c._thread_span_links.get_active_leaf_spans_from_thread_id( thread_id) == {root} quit_thread = threading.Event() span_started = threading.Event() store = {} def start_span(): store["span2"] = t.start_span("thread2") span_started.set() quit_thread.wait() th = threading.Thread(target=start_span) th.start() span_started.wait() if TESTING_GEVENT: # We track *real* threads, gevent is using only one in this case assert c._thread_span_links.get_active_leaf_spans_from_thread_id( thread_id) == {root, store["span2"]} assert c._thread_span_links.get_active_leaf_spans_from_thread_id( th.ident) == set() else: assert c._thread_span_links.get_active_leaf_spans_from_thread_id( thread_id) == {root} assert c._thread_span_links.get_active_leaf_spans_from_thread_id( th.ident) == {store["span2"]} # Do not quit the thread before we test, otherwise the collector might clean up the thread from the list of spans quit_thread.set() th.join()
def test_thread_to_child_span_clear(tracer_and_collector): t, c = tracer_and_collector root = t.start_span("root") thread_id = _nogevent.thread_get_ident() assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == {root} c._thread_span_links.clear_threads(set()) assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == set()
def test_thread_to_span_multiple(tracer_and_collector): t, c = tracer_and_collector root = t.start_span("root") thread_id = _nogevent.thread_get_ident() assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == {root} subspan = t.start_span("subtrace", child_of=root) assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == {subspan} subspan.finish() assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == {root} root.finish() assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == set()
def test_thread_to_child_span_multiple_more_children(tracer_and_collector): t, c = tracer_and_collector root = t.start_span("root") thread_id = _nogevent.thread_get_ident() assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == {root} subspan = t.start_span("subtrace", child_of=root) subsubspan = t.start_span("subsubtrace", child_of=subspan) assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == {subsubspan} subsubspan2 = t.start_span("subsubtrace2", child_of=subspan) assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == {subsubspan, subsubspan2} # Рџа subspan is not supposed to finish before its children, but the API authorizes it # In that case, we would return also the root span as it's becoming a parent without children Ъци subspan.finish() assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == {root, subsubspan, subsubspan2}
def test_exception_collection(): r = recorder.Recorder() c = stack.StackCollector(r) with c: try: raise ValueError("hello") except Exception: _nogevent.sleep(1) exception_events = r.events[stack.StackExceptionSampleEvent] assert len(exception_events) >= 1 e = exception_events[0] assert e.timestamp > 0 assert e.sampling_period > 0 assert e.thread_id == _nogevent.thread_get_ident() assert e.thread_name == "MainThread" assert e.frames == [(__file__, 237, "test_exception_collection")] assert e.nframes == 1 assert e.exc_type == ValueError
def test_thread_to_span_thread_isolation(tracer_and_collector): t, c = tracer_and_collector root = t.start_span("root") thread_id = _nogevent.thread_get_ident() assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == {root} store = {} def start_span(): store["span2"] = t.start_span("thread2") th = threading.Thread(target=start_span) th.start() th.join() if TESTING_GEVENT: # We track *real* threads, gevent is using only one in this case assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == {root, store["span2"]} assert c._thread_span_links.get_active_leaf_spans_from_thread_id(th.ident) == set() else: assert c._thread_span_links.get_active_leaf_spans_from_thread_id(thread_id) == {root} assert c._thread_span_links.get_active_leaf_spans_from_thread_id(th.ident) == {store["span2"]}