def link_from_headers(cls, headers): # type: (Dict[str, str]) -> None """ Given a dictionary, extracts the context and links the context to the current tracer. :param headers: A key value pair dictionary :type headers: dict """ ctx = trace_context_http_header_format.TraceContextPropagator().from_headers(headers) current_span = cls.get_current_span() current_span.add_link(Link(ctx.trace_id, ctx.span_id))
def test_do_not_crash(self): span_id = 'test_span_id' span_name = 'test_span_name' patch = mock.patch( 'opencensus.trace.blank_span.generate_span_id', return_value=span_id) with patch: span = self._make_one(span_name) self.assertEqual(span.name, span_name) self.assertEqual(span.span_id, span_id) self.assertIsNotNone(span.parent_span) self.assertIsNotNone(span.parent_span.span()) self.assertEqual(span.attributes, {}) self.assertIsNone(span.start_time) self.assertIsNone(span.end_time) self.assertEqual(span.children, []) self.assertIsNone(span.context_tracer) span.add_attribute('attribute_key', 'attribute_value') span.add_annotation('This is a test', name='blank-span') link = Link(span_id='1234', trace_id='4567') span.add_link(link) time_event = mock.Mock() with self.assertRaises(TypeError): span.add_time_event(time_event) time_event = TimeEvent(datetime.datetime.now()) span.add_time_event(time_event) span_iter_list = list(iter(span)) self.assertEqual(span_iter_list, [span]) expected_span_json = { 'spanId': 'test_span_id', 'startTime': None, 'endTime': None, 'displayName': { 'truncated_byte_count': 0, 'value': 'test_span_name' }, 'childSpanCount': 0, } span_json = format_span_json(span) self.assertEqual(span_json, expected_span_json) span.start() span.finish()
def test_add_link(self): from opencensus.trace.link import Link span_name = 'test_span_name' span = self._make_one(span_name) link = mock.Mock() with self.assertRaises(TypeError): span.add_link(link) link = Link(span_id='1234', trace_id='4567') span.add_link(link) self.assertEqual(len(span.links), 1)
def __init__(self, span=None, name="span", **kwargs): # type: (Optional[Span], Optional[str], Any) -> None """ If a span is not passed in, creates a new tracer. If the instrumentation key for Azure Exporter is given, will configure the azure exporter else will just create a new tracer. :param span: The OpenCensus span to wrap :type span: :class: opencensus.trace.Span :param name: The name of the OpenCensus span to create if a new span is needed :type name: str :keyword SpanKind kind: The span kind of this span. :keyword links: The list of links to be added to the span. :paramtype links: list[~azure.core.tracing.Link] """ tracer = self.get_current_tracer() value = kwargs.pop('kind', None) kind = ( OpenCensusSpanKind.CLIENT if value == SpanKind.CLIENT else OpenCensusSpanKind.CLIENT if value == SpanKind.PRODUCER else # No producer in opencensus OpenCensusSpanKind.SERVER if value == SpanKind.SERVER else OpenCensusSpanKind.CLIENT if value == SpanKind.CONSUMER else # No consumer in opencensus OpenCensusSpanKind.UNSPECIFIED if value == SpanKind.INTERNAL else # No internal in opencensus OpenCensusSpanKind.UNSPECIFIED if value == SpanKind.UNSPECIFIED else None) # type: SpanKind if value and kind is None: raise ValueError( "Kind {} is not supported in OpenCensus".format(value)) links = kwargs.pop('links', None) self._span_instance = span or tracer.start_span(name=name, **kwargs) if kind is not None: self._span_instance.span_kind = kind if links: try: for link in links: ctx = trace_context_http_header_format.TraceContextPropagator( ).from_headers(link.headers) self._span_instance.add_link( Link(trace_id=ctx.trace_id, span_id=ctx.span_id, attributes=link.attributes)) except AttributeError: # we will just send the links as is if it's not ~azure.core.tracing.Link without any validation # assuming user knows what they are doing. self._span_instance.links = links
def test_format_span_json_with_parent_span(self, time_event_mock, status_mock, stack_trace_mock): import datetime from opencensus.trace.link import Link from opencensus.trace.span import format_span_json name = 'test span' span_id = 1234 trace_id = '3456' attributes = { '/http/status_code': '200', '/component': 'HTTP load balancer', 'none_key': None } links = { 'link': [ { 'trace_id': trace_id, 'span_id': span_id, 'type': 0 }, ], } start_time = '2017-06-25' end_time = '2017-06-26' parent_span = mock.Mock() parent_span_id = 5678 parent_span.span_id = parent_span_id span = mock.Mock() span.parent_span = parent_span span.name = name span.attributes = attributes span.span_id = span_id span.start_time = start_time span.end_time = end_time span._child_spans = [] span.time_events = [TimeEvent(datetime.datetime.now())] span.stack_trace = StackTrace() span.status = Status(code='200', message='test') span.links = [Link(trace_id, span_id)] span.same_process_as_parent_span = True mock_stack_trace = 'stack trace' mock_status = 'status' mock_time_event = 'time event' stack_trace_mock.return_value = mock_stack_trace status_mock.return_value = mock_status time_event_mock.return_value = mock_time_event expected_span_json = { 'spanId': span_id, 'parentSpanId': parent_span_id, 'startTime': start_time, 'endTime': end_time, 'attributes': { 'attributeMap': { '/component': { 'string_value': { 'truncated_byte_count': 0, 'value': 'HTTP load balancer' } }, '/http/status_code': { 'string_value': { 'truncated_byte_count': 0, 'value': '200' } } } }, 'links': links, 'stackTrace': mock_stack_trace, 'status': mock_status, 'timeEvents': { 'timeEvent': [mock_time_event] }, 'displayName': { 'truncated_byte_count': 0, 'value': 'test span' }, 'childSpanCount': 0, 'sameProcessAsParentSpan': True } span_json = format_span_json(span) print(span_json) print(expected_span_json) self.assertEqual(span_json, expected_span_json)
def test_span_data_to_envelope(self): from opencensus.trace.span import SpanKind from opencensus.trace.span_context import SpanContext from opencensus.trace.span_data import SpanData from opencensus.trace.status import Status from opencensus.trace.trace_options import TraceOptions from opencensus.trace.tracestate import Tracestate exporter = trace_exporter.AzureExporter( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', storage_path=os.path.join(TEST_FOLDER, self.id()), ) # SpanKind.CLIENT HTTP envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={ 'component': 'HTTP', 'http.method': 'GET', 'http.url': 'https://www.wikipedia.org/wiki/Rabbit', 'http.status_code': 200, }, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=[ Link('6e0c63257de34c90bf9efcd03927272e', '6e0c63257de34c91') ], status=Status(0), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.CLIENT, )) self.assertEqual( envelope.iKey, '12345678-1234-5678-abcd-12345678abcd') self.assertEqual( envelope.name, 'Microsoft.ApplicationInsights.RemoteDependency') self.assertEqual( envelope.tags['ai.operation.parentId'], '6e0c63257de34c93') self.assertEqual( envelope.tags['ai.operation.id'], '6e0c63257de34c90bf9efcd03927272e') self.assertEqual( envelope.time, '2010-10-24T07:28:38.123456Z') self.assertEqual( envelope.data.baseData.name, 'GET /wiki/Rabbit') self.assertEqual( envelope.data.baseData.data, 'https://www.wikipedia.org/wiki/Rabbit') self.assertEqual( envelope.data.baseData.target, 'www.wikipedia.org') self.assertEqual( envelope.data.baseData.id, '6e0c63257de34c92') self.assertEqual( envelope.data.baseData.resultCode, '200') self.assertEqual( envelope.data.baseData.duration, '0.00:00:00.111') self.assertEqual( envelope.data.baseData.type, 'HTTP') self.assertEqual( envelope.data.baseType, 'RemoteDependencyData') json_dict = json.loads( envelope.data.baseData.properties["_MS.links"] )[0] self.assertEqual( json_dict["id"], "6e0c63257de34c91", ) self.assertEqual( json_dict["operation_Id"], "6e0c63257de34c90bf9efcd03927272e", ) # SpanKind.CLIENT unknown type envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={}, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=None, status=Status(0), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.CLIENT, )) self.assertEqual( envelope.iKey, '12345678-1234-5678-abcd-12345678abcd') self.assertEqual( envelope.name, 'Microsoft.ApplicationInsights.RemoteDependency') self.assertEqual( envelope.tags['ai.operation.parentId'], '6e0c63257de34c93') self.assertEqual( envelope.tags['ai.operation.id'], '6e0c63257de34c90bf9efcd03927272e') self.assertEqual( envelope.time, '2010-10-24T07:28:38.123456Z') self.assertEqual( envelope.data.baseData.name, 'test') self.assertEqual( envelope.data.baseData.id, '6e0c63257de34c92') self.assertEqual( envelope.data.baseData.duration, '0.00:00:00.111') self.assertEqual( envelope.data.baseData.type, None) self.assertEqual( envelope.data.baseType, 'RemoteDependencyData') # SpanKind.CLIENT missing method envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={ 'component': 'HTTP', 'http.url': 'https://www.wikipedia.org/wiki/Rabbit', 'http.status_code': 200, }, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=None, status=Status(0), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.CLIENT, )) self.assertEqual( envelope.iKey, '12345678-1234-5678-abcd-12345678abcd') self.assertEqual( envelope.name, 'Microsoft.ApplicationInsights.RemoteDependency') self.assertEqual( envelope.tags['ai.operation.parentId'], '6e0c63257de34c93') self.assertEqual( envelope.tags['ai.operation.id'], '6e0c63257de34c90bf9efcd03927272e') self.assertEqual( envelope.time, '2010-10-24T07:28:38.123456Z') self.assertEqual( envelope.data.baseData.name, 'test') self.assertEqual( envelope.data.baseData.data, 'https://www.wikipedia.org/wiki/Rabbit') self.assertEqual( envelope.data.baseData.target, 'www.wikipedia.org') self.assertEqual( envelope.data.baseData.id, '6e0c63257de34c92') self.assertEqual( envelope.data.baseData.resultCode, '200') self.assertEqual( envelope.data.baseData.duration, '0.00:00:00.111') self.assertEqual( envelope.data.baseData.type, 'HTTP') self.assertEqual( envelope.data.baseType, 'RemoteDependencyData') # SpanKind.SERVER HTTP - 200 request envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={ 'component': 'HTTP', 'http.method': 'GET', 'http.path': '/wiki/Rabbit', 'http.route': '/wiki/Rabbit', 'http.url': 'https://www.wikipedia.org/wiki/Rabbit', 'http.status_code': 200, }, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=None, status=Status(0), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.SERVER, )) self.assertEqual( envelope.iKey, '12345678-1234-5678-abcd-12345678abcd') self.assertEqual( envelope.name, 'Microsoft.ApplicationInsights.Request') self.assertEqual( envelope.tags['ai.operation.parentId'], '6e0c63257de34c93') self.assertEqual( envelope.tags['ai.operation.id'], '6e0c63257de34c90bf9efcd03927272e') self.assertEqual( envelope.tags['ai.operation.name'], 'GET /wiki/Rabbit') self.assertEqual( envelope.time, '2010-10-24T07:28:38.123456Z') self.assertEqual( envelope.data.baseData.id, '6e0c63257de34c92') self.assertEqual( envelope.data.baseData.duration, '0.00:00:00.111') self.assertEqual( envelope.data.baseData.responseCode, '200') self.assertEqual( envelope.data.baseData.name, 'GET /wiki/Rabbit') self.assertEqual( envelope.data.baseData.properties['request.name'], 'GET /wiki/Rabbit') self.assertEqual( envelope.data.baseData.success, True) self.assertEqual( envelope.data.baseData.url, 'https://www.wikipedia.org/wiki/Rabbit') self.assertEqual( envelope.data.baseData.properties['request.url'], 'https://www.wikipedia.org/wiki/Rabbit') self.assertEqual( envelope.data.baseType, 'RequestData') # SpanKind.SERVER HTTP - Failed request envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={ 'component': 'HTTP', 'http.method': 'GET', 'http.path': '/wiki/Rabbit', 'http.route': '/wiki/Rabbit', 'http.url': 'https://www.wikipedia.org/wiki/Rabbit', 'http.status_code': 400, }, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=None, status=Status(0), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.SERVER, )) self.assertEqual( envelope.iKey, '12345678-1234-5678-abcd-12345678abcd') self.assertEqual( envelope.name, 'Microsoft.ApplicationInsights.Request') self.assertEqual( envelope.tags['ai.operation.parentId'], '6e0c63257de34c93') self.assertEqual( envelope.tags['ai.operation.id'], '6e0c63257de34c90bf9efcd03927272e') self.assertEqual( envelope.tags['ai.operation.name'], 'GET /wiki/Rabbit') self.assertEqual( envelope.time, '2010-10-24T07:28:38.123456Z') self.assertEqual( envelope.data.baseData.id, '6e0c63257de34c92') self.assertEqual( envelope.data.baseData.duration, '0.00:00:00.111') self.assertEqual( envelope.data.baseData.responseCode, '400') self.assertEqual( envelope.data.baseData.name, 'GET /wiki/Rabbit') self.assertEqual( envelope.data.baseData.success, False) self.assertEqual( envelope.data.baseData.url, 'https://www.wikipedia.org/wiki/Rabbit') self.assertEqual( envelope.data.baseType, 'RequestData') # SpanKind.SERVER unknown type envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={}, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=None, status=Status(0), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.SERVER, )) self.assertEqual( envelope.iKey, '12345678-1234-5678-abcd-12345678abcd') self.assertEqual( envelope.name, 'Microsoft.ApplicationInsights.Request') self.assertEqual( envelope.tags['ai.operation.parentId'], '6e0c63257de34c93') self.assertEqual( envelope.tags['ai.operation.id'], '6e0c63257de34c90bf9efcd03927272e') self.assertEqual( envelope.time, '2010-10-24T07:28:38.123456Z') self.assertEqual( envelope.data.baseData.id, '6e0c63257de34c92') self.assertEqual( envelope.data.baseData.duration, '0.00:00:00.111') self.assertEqual( envelope.data.baseType, 'RequestData') # SpanKind.UNSPECIFIED envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id=None, attributes={'key1': 'value1'}, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=None, status=Status(0), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.UNSPECIFIED, )) self.assertEqual( envelope.iKey, '12345678-1234-5678-abcd-12345678abcd') self.assertEqual( envelope.name, 'Microsoft.ApplicationInsights.RemoteDependency') self.assertRaises( KeyError, lambda: envelope.tags['ai.operation.parentId']) self.assertEqual( envelope.tags['ai.operation.id'], '6e0c63257de34c90bf9efcd03927272e') self.assertEqual( envelope.time, '2010-10-24T07:28:38.123456Z') self.assertEqual( envelope.data.baseData.name, 'test') self.assertEqual( envelope.data.baseData.duration, '0.00:00:00.111') self.assertEqual( envelope.data.baseData.id, '6e0c63257de34c92') self.assertEqual( envelope.data.baseData.type, 'INPROC') self.assertEqual( envelope.data.baseData.success, True ) self.assertEqual( envelope.data.baseType, 'RemoteDependencyData') # Status server status code attribute envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={ 'http.status_code': 201 }, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=None, status=Status(0), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.SERVER, )) self.assertEqual(envelope.data.baseData.responseCode, "201") self.assertTrue(envelope.data.baseData.success) # Status server status code attribute missing envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={}, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=None, status=Status(1), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.SERVER, )) self.assertFalse(envelope.data.baseData.success) # Server route attribute missing envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={ 'component': 'HTTP', 'http.method': 'GET', 'http.path': '/wiki/Rabbitz', 'http.url': 'https://www.wikipedia.org/wiki/Rabbit', 'http.status_code': 400, }, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=None, status=Status(1), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.SERVER, )) self.assertEqual(envelope.data.baseData.properties['request.name'], 'GET /wiki/Rabbitz') # Server route and path attribute missing envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={ 'component': 'HTTP', 'http.method': 'GET', 'http.url': 'https://www.wikipedia.org/wiki/Rabbit', 'http.status_code': 400, }, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=None, status=Status(1), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.SERVER, )) self.assertIsNone( envelope.data.baseData.properties.get('request.name')) # Status client status code attribute envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={ 'http.status_code': 201 }, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=None, status=Status(0), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.CLIENT, )) self.assertEqual(envelope.data.baseData.resultCode, "201") self.assertTrue(envelope.data.baseData.success) # Status client status code attributes missing envelope = exporter.span_data_to_envelope(SpanData( name='test', context=SpanContext( trace_id='6e0c63257de34c90bf9efcd03927272e', span_id='6e0c63257de34c91', trace_options=TraceOptions('1'), tracestate=Tracestate(), from_header=False, ), span_id='6e0c63257de34c92', parent_span_id='6e0c63257de34c93', attributes={}, start_time='2010-10-24T07:28:38.123456Z', end_time='2010-10-24T07:28:38.234567Z', stack_trace=None, links=None, status=Status(1), annotations=None, message_events=None, same_process_as_parent_span=None, child_span_count=None, span_kind=SpanKind.CLIENT, )) self.assertFalse(envelope.data.baseData.success) exporter._stop()