def test_override_exports(self): export_tracing = Mock() export_timers = Mock() sw = StopWatch( export_tracing_func=export_tracing, export_aggregated_timers_func=export_timers, ) add_timers(sw) agg_report = sw.get_last_aggregated_report() traces = sw.get_last_trace_report() export_timers.assert_called_once_with(aggregated_report=agg_report) export_tracing.assert_called_once_with(reported_traces=traces) assert agg_report.aggregated_values == { 'root': [900000.0, 1, None], 'root#child1': [240000.0, 2, MyBuckets.BUCKET_A], 'root#child1#grand_children1': [20000.0, 1, None], 'root#child1#grand_children2': [80000.0, 2, None], 'root#child1#grand_children3': [10000.0, 1, None], 'root#child2': [560000.0, 1, MyBuckets.BUCKET_B], 'root#child2#grand_children1': [260000.0, 1, None], 'root#child2#grand_children3': [10000.0, 1, None], } assert agg_report.root_timer_data.start_time == 20.0 assert agg_report.root_timer_data.end_time == 920.0 assert agg_report.root_timer_data.name == 'root' assert agg_report.root_timer_data.trace_annotations == [ TraceAnnotation('Cooltag', '1', 50), TraceAnnotation('Slowtag', '1', 920), ] # Traces are listed in the same order that scopes close assert [(trace.name, trace.log_name, trace.start_time, trace.end_time, trace.parent_span_id) for trace in traces] == [ ('grand_children1', 'root#child1#grand_children1', 60, 80, traces[2].span_id), ('grand_children2', 'root#child1#grand_children2', 100, 120, traces[2].span_id), ('child1', 'root#child1', 40, 140, traces[9].span_id), ('grand_children3', 'root#child1#grand_children3', 180, 190, traces[5].span_id), ('grand_children2', 'root#child1#grand_children2', 220, 280, traces[5].span_id), ('child1', 'root#child1', 160, 300, traces[9].span_id), ('grand_children3', 'root#child2#grand_children3', 380, 390, traces[8].span_id), ('grand_children1', 'root#child2#grand_children1', 520, 780, traces[8].span_id), ('child2', 'root#child2', 320, 880, traces[9].span_id), ('root', 'root', 20, 920, None), ] assert all(trace.trace_annotations == [] for trace in traces[:9]) assert traces[9].trace_annotations == [ TraceAnnotation('Cooltag', '1', 50), TraceAnnotation('Slowtag', '1', 920), ]
def test_exception_annotation(self): class SpecialError(Exception): pass sw = StopWatch() with pytest.raises(SpecialError): with sw.timer('root', start_time=10, end_time=1000): raise SpecialError("Ahhh") trace_report = sw.get_last_trace_report() assert trace_report[0].trace_annotations == [ TraceAnnotation('Exception', 'SpecialError', 1000), ]
def test_exception_annotation(self): class SpecialError(Exception): pass sw = StopWatch() with pytest.raises(SpecialError): with sw.timer('root', start_time=10, end_time=1000): raise SpecialError("Ahhh") trace_report = sw.get_last_trace_report() assert trace_report[0].trace_annotations == [ TraceAnnotation('Exception', 'SpecialError', 1000), ]
def test_override_exports(self): export_tracing = Mock() export_timers = Mock() sw = StopWatch( export_tracing_func=export_tracing, export_aggregated_timers_func=export_timers, ) add_timers(sw) agg_report = sw.get_last_aggregated_report() traces = sw.get_last_trace_report() export_timers.assert_called_once_with(aggregated_report=agg_report) export_tracing.assert_called_once_with(reported_traces=traces) assert agg_report.aggregated_values == { 'root': [900000.0, 1, None], 'root#child1': [240000.0, 2, MyBuckets.BUCKET_A], 'root#child1#grand_children1': [20000.0, 1, None], 'root#child1#grand_children2': [80000.0, 2, None], 'root#child1#grand_children3': [10000.0, 1, None], 'root#child2': [560000.0, 1, MyBuckets.BUCKET_B], 'root#child2#grand_children1': [260000.0, 1, None], 'root#child2#grand_children3': [10000.0, 1, None], } assert agg_report.root_timer_data.start_time == 20.0 assert agg_report.root_timer_data.end_time == 920.0 assert agg_report.root_timer_data.name == 'root' assert agg_report.root_timer_data.trace_annotations == [ TraceAnnotation('Cooltag', '1', 50), TraceAnnotation('Slowtag', '1', 920), ] # Traces are listed in the same order that scopes close assert [(trace.name, trace.log_name, trace.start_time, trace.end_time, trace.parent_span_id) for trace in traces] == [ ('grand_children1', 'root#child1#grand_children1', 60, 80, traces[2].span_id), ('grand_children2', 'root#child1#grand_children2', 100, 120, traces[2].span_id), ('child1', 'root#child1', 40, 140, traces[9].span_id), ('grand_children3', 'root#child1#grand_children3', 180, 190, traces[5].span_id), ('grand_children2', 'root#child1#grand_children2', 220, 280, traces[5].span_id), ('child1', 'root#child1', 160, 300, traces[9].span_id), ('grand_children3', 'root#child2#grand_children3', 380, 390, traces[8].span_id), ('grand_children1', 'root#child2#grand_children1', 520, 780, traces[8].span_id), ('child2', 'root#child2', 320, 880, traces[9].span_id), ('root', 'root', 20, 920, None), ] assert all(trace.trace_annotations == [] for trace in traces[:9]) assert traces[9].trace_annotations == [ TraceAnnotation('Cooltag', '1', 50), TraceAnnotation('Slowtag', '1', 920), ]
def test_override_exports(self): export_tracing = Mock() export_timers = Mock() sw = StopWatch( export_tracing_func=export_tracing, export_aggregated_timers_func=export_timers, ) add_timers(sw) agg_report = sw.get_last_aggregated_report() traces = sw.get_last_trace_report() assert export_timers.call_args[1]['reported_values'] == agg_report[0] assert export_timers.call_args[1]['tags'] == agg_report[1] export_tracing.assert_called_once_with(reported_traces=traces) export_timers.assert_called_once_with( reported_values={ 'root': [900000.0, 1, None], 'root#child1': [240000.0, 2, MyBuckets.BUCKET_A], 'root#child1#grand_children1': [20000.0, 1, None], 'root#child1#grand_children2': [80000.0, 2, None], 'root#child1#grand_children3': [10000.0, 1, None], 'root#child2': [560000.0, 1, MyBuckets.BUCKET_B], 'root#child2#grand_children1': [260000.0, 1, None], 'root#child2#grand_children3': [10000.0, 1, None], }, tags=set(["Cooltag", "Slowtag"]), total_time_ms=900000.0, root_span_name="root", ) # Traces are listed in the same order that scopes close assert [(trace.name, trace.log_name, trace.start_time, trace.end_time, trace.parent_span_id) for trace in traces] == [ ('grand_children1', 'root#child1#grand_children1', 60, 80, traces[2].span_id), ('grand_children2', 'root#child1#grand_children2', 100, 120, traces[2].span_id), ('child1', 'root#child1', 40, 140, traces[9].span_id), ('grand_children3', 'root#child1#grand_children3', 180, 190, traces[5].span_id), ('grand_children2', 'root#child1#grand_children2', 220, 280, traces[5].span_id), ('child1', 'root#child1', 160, 300, traces[9].span_id), ('grand_children3', 'root#child2#grand_children3', 380, 390, traces[8].span_id), ('grand_children1', 'root#child2#grand_children1', 520, 780, traces[8].span_id), ('child2', 'root#child2', 320, 880, traces[9].span_id), ('root', 'root', 20, 920, None), ] assert all(trace.trace_annotations == [] for trace in traces[:9]) assert traces[9].trace_annotations == [ KeyValueAnnotation('Cooltag', '1'), KeyValueAnnotation('Slowtag', '1'), ]
def test_trace_annotations(self): sw = StopWatch() sw.add_annotation('key0', 'value0', event_time=0) with sw.timer('root', start_time=10, end_time=1000): with sw.timer('child', start_time=20, end_time=900): sw.add_span_annotation('key1', 'value1', event_time=101) sw.add_span_annotation('key2', 'value2', event_time=104) sw.add_annotation('key3', 'value3', event_time=107) trace_report = sw.get_last_trace_report() assert len(trace_report) == 2 assert trace_report[0].name == 'child' assert trace_report[0].trace_annotations == [ TraceAnnotation('key1', 'value1', 101), TraceAnnotation('key2', 'value2', 104), ] assert trace_report[1].name == 'root' assert trace_report[1].trace_annotations == [ TraceAnnotation('key0', 'value0', 0), TraceAnnotation('key3', 'value3', 107), ]
def test_trace_annotations(self): sw = StopWatch() sw.add_annotation('key0', 'value0', event_time=0) with sw.timer('root', start_time=10, end_time=1000): with sw.timer('child', start_time=20, end_time=900): sw.add_span_annotation('key1', 'value1', event_time=101) sw.add_span_annotation('key2', 'value2', event_time=104) sw.add_annotation('key3', 'value3', event_time=107) trace_report = sw.get_last_trace_report() assert len(trace_report) == 2 assert trace_report[0].name == 'child' assert trace_report[0].trace_annotations == [ TraceAnnotation('key1', 'value1', 101), TraceAnnotation('key2', 'value2', 104), ] assert trace_report[1].name == 'root' assert trace_report[1].trace_annotations == [ TraceAnnotation('key0', 'value0', 0), TraceAnnotation('key3', 'value3', 107), ]