def from_header(self, binary): """Generate a SpanContext object using the trace context header. The value of enabled parsed from header is int. Need to convert to bool. :type binary: bytes :param binary: Trace context header which was extracted from the request headers. :rtype: :class:`~opencensus.trace.span_context.SpanContext` :returns: SpanContext generated from the trace context header. """ # If cannot parse, return a new SpanContext and ignore the context # from binary. try: data = Header._make(struct.unpack(BINARY_FORMAT, binary)) except struct.error: logging.warn( 'Cannot parse the incoming binary data {}, ' 'wrong format. Total bytes length should be {}.'.format( binary, FORMAT_LENGTH)) return SpanContext(from_header=False) # data.trace_id is in bytes with length 16, hexlify it to hex bytes # with length 32, then decode it to hex string using utf-8. trace_id = str(binascii.hexlify(data.trace_id).decode(UTF8)) span_id = data.span_id trace_options = TraceOptions(data.trace_option) span_context = SpanContext(trace_id=trace_id, span_id=span_id, trace_options=trace_options, from_header=True) return span_context
def from_carrier(self, carrier): """Generate a SpanContext object using the information in the carrier. :type carrier: dict :param carrier: The carrier which has the trace_id, span_id, options information for creating a SpanContext. :rtype: :class:`~opencensus.trace.span_context.SpanContext` :returns: SpanContext generated from the carrier. """ trace_id = None span_id = None trace_options = None for key in carrier: key = key.lower() if key == _TRACE_ID_KEY: trace_id = carrier[key] if key == _SPAN_ID_KEY: span_id = carrier[key] if key == _TRACE_OPTIONS_KEY: trace_options = bool(carrier[key]) if trace_options is None: trace_options = DEFAULT_TRACE_OPTIONS return SpanContext(trace_id=trace_id, span_id=span_id, trace_options=TraceOptions(trace_options), from_header=True)
def test__str__(self): span_context = self._make_one( trace_id=self.trace_id, span_id=self.span_id, trace_options=TraceOptions('1')) header_expected = '6e0c63257de34c92bf9efcd03927272e' \ '/6e0c63257de34c92;o=1' header = span_context.__str__() self.assertEqual(header_expected, header)
def test__repr__(self): span_context = self._make_one(trace_id=self.trace_id, span_id=self.span_id, trace_options=TraceOptions('1'), tracestate=Tracestate()) expected_repr = 'SpanContext(' \ 'trace_id=6e0c63257de34c92bf9efcd03927272e, ' \ 'span_id=6e0c63257de34c92, ' \ 'trace_options=TraceOptions(enabled=True), ' \ 'tracestate=Tracestate())' self.assertEqual(expected_repr, span_context.__repr__())
def from_headers(self, headers): """Generate a SpanContext object using the W3C Distributed Tracing headers. :type headers: dict :param headers: HTTP request headers. :rtype: :class:`~opencensus.trace.span_context.SpanContext` :returns: SpanContext generated from the trace context header. """ if headers is None: return SpanContext() header = headers.get(_TRACEPARENT_HEADER_NAME) if header is None: return SpanContext() match = re.search(_TRACEPARENT_HEADER_FORMAT_RE, header) if not match: return SpanContext() version = match.group(1) trace_id = match.group(2) span_id = match.group(3) trace_options = match.group(4) if trace_id == '0' * 32 or span_id == '0' * 16: return SpanContext() if version == '00': if match.group(5): return SpanContext() if version == 'ff': return SpanContext() span_context = SpanContext( trace_id=trace_id, span_id=span_id, trace_options=TraceOptions(trace_options), from_header=True) header = headers.get(_TRACESTATE_HEADER_NAME) if header is None: return span_context try: tracestate = TracestateStringFormatter().from_string(header) if tracestate.is_valid(): span_context.tracestate = \ TracestateStringFormatter().from_string(header) except ValueError: pass return span_context
def test__str__(self): from opencensus.trace.trace_options import TraceOptions trace_id = '6e0c63257de34c92bf9efcd03927272e' span_id = 1234 span_context = self._make_one( trace_id=trace_id, span_id=span_id, trace_options=TraceOptions('1')) header_expected = '6e0c63257de34c92bf9efcd03927272e/1234;o=1' header = span_context.__str__() self.assertEqual(header_expected, header)
def from_header(self, header): """Generate a SpanContext object using the trace context header. :type header: str :param header: Trace context header which was extracted from the HTTP request headers. :rtype: :class:`~opencensus.trace.span_context.SpanContext` :returns: SpanContext generated from the trace context header. """ if header is None: return SpanContext() try: match = re.search(_TRACE_CONTEXT_HEADER_RE, header) except TypeError: logging.warning( 'Header should be str, got {}. Cannot parse the header.' .format(header.__class__.__name__)) raise if match: version = match.group(1) if version == '00': trace_id = match.group(3) span_id = match.group(5) trace_options = match.group(7) if trace_options is None: trace_options = 1 # Need to convert span_id from hex string to int span_context = SpanContext( trace_id=trace_id, span_id=span_id, trace_options=TraceOptions(trace_options), from_header=True) return span_context else: logging.warning( 'Header format version {} is not supported, generate a new' 'context instead.'.format(version)) else: logging.warning( 'Cannot parse the header {}, generate a new context instead.' .format(header)) return SpanContext()
def from_header(self, header): """Generate a SpanContext object using the trace context header. The value of enabled parsed from header is int. Need to convert to bool. :type header: str :param header: Trace context header which was extracted from the HTTP request headers. :rtype: :class:`~opencensus.trace.span_context.SpanContext` :returns: SpanContext generated from the trace context header. """ if header is None: return SpanContext() try: match = re.search(_TRACE_CONTEXT_HEADER_RE, header) except TypeError: logging.warning( 'Header should be str, got %s. Cannot parse the header.', header.__class__.__name__) raise if match: trace_id = match.group(1) span_id = match.group(3) trace_options = match.group(5) if trace_options is None: trace_options = 1 if span_id: span_id = '{:016x}'.format(int(span_id)) span_context = SpanContext( trace_id=trace_id, span_id=span_id, trace_options=TraceOptions(trace_options), from_header=True) return span_context else: logging.warning( 'Cannot parse the header %s, generate a new context instead.', header) return SpanContext()
def test_to_header(self): from opencensus.trace.span_context import SpanContext from opencensus.trace.trace_options import TraceOptions span_context = mock.Mock(spec=SpanContext) trace_id = 'a0b72ca15c1a4bd18962d0ac59dc90b9' span_id = 7433567179112518326 trace_options = '1' span_context.trace_id = trace_id span_context.span_id = span_id span_context.trace_options = TraceOptions(trace_options) propagator = binary_format.BinaryFormatPropagator() binary_header = propagator.to_header(span_context) expected_binary_header = b'\x00\x00\xa0\xb7,\xa1\\\x1aK\xd1\x89b\xd0' \ b'\xacY\xdc\x90\xb9\x01g)U\xf6\xf5\x01\x12' \ b'\xb6\x02\x01' self.assertEqual(expected_binary_header, binary_header)
def from_header(self, header=None): """Generate a SpanContext object using the trace context header. The value of enabled parsed from header is int. Need to convert to bool. :type header: str :param header: Trace context header which was extracted from the HTTP request headers. :rtype: :class:`~opencensus.trace.span_context.SpanContext` :returns: SpanContext generated from the trace context header. """ if header is None: return SpanContext() try: match = re.match(_UBER_HEADER_RE, header) except TypeError: logging.warning( 'Header should be str, got {}. Cannot parse the header.'. format(header.__class__.__name__)) raise if match is not None: trace_id = match.group(1) span_id = match.group(2) trace_options = match.group(4) span_context = SpanContext(trace_id=trace_id.zfill(32), span_id=span_id.zfill(16), trace_options=TraceOptions( str(int('0x' + trace_options, 16))), from_header=True) return span_context else: logging.warning( 'Cannot parse the header {}, generate a new context instead.'. format(header)) return SpanContext()
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()
def test_span_data_to_envelope(self): from opencensus.ext.azure.common import Options from opencensus.trace.span import SpanKind from opencensus.trace.span_context import SpanContext from opencensus.trace.span_data import SpanData from opencensus.trace.trace_options import TraceOptions from opencensus.trace.tracestate import Tracestate exporter = trace_exporter.AzureExporter( Options( instrumentation_key='12345678-1234-5678-abcd-12345678abcd', storage_path=os.path.join(TEST_FOLDER, 'bar'), ), ) # 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={ '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=None, status=None, time_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'], '|6e0c63257de34c90bf9efcd03927272e.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, 'www.wikipedia.org') self.assertEqual( envelope.data.baseData.id, '|6e0c63257de34c90bf9efcd03927272e.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.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=None, time_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'], '|6e0c63257de34c90bf9efcd03927272e.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, '|6e0c63257de34c90bf9efcd03927272e.6e0c63257de34c92.') 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 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.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=None, status=None, time_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'], '|6e0c63257de34c90bf9efcd03927272e.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, '|6e0c63257de34c90bf9efcd03927272e.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 https://www.wikipedia.org/wiki/Rabbit') 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=None, time_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'], '|6e0c63257de34c90bf9efcd03927272e.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, '|6e0c63257de34c90bf9efcd03927272e.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=None, time_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, '|6e0c63257de34c90bf9efcd03927272e.6e0c63257de34c92.') self.assertEqual( envelope.data.baseData.type, 'INPROC') self.assertEqual( envelope.data.baseType, 'RemoteDependencyData')
def from_headers(self, headers): """Generate a SpanContext object from B3 propagation headers. :type headers: dict :param headers: HTTP request headers. :rtype: :class:`~opencensus.trace.span_context.SpanContext` :returns: SpanContext generated from B3 propagation headers. """ if headers is None: return SpanContext(from_header=False) trace_id, span_id, sampled = None, None, None state = headers.get(_STATE_HEADER_KEY) if state: fields = state.split('-', 4) if len(fields) == 1: sampled = fields[0] elif len(fields) == 2: trace_id, span_id = fields elif len(fields) == 3: trace_id, span_id, sampled = fields elif len(fields) == 4: trace_id, span_id, sampled, _parent_span_id = fields else: return SpanContext(from_header=False) else: trace_id = headers.get(_TRACE_ID_KEY) span_id = headers.get(_SPAN_ID_KEY) sampled = headers.get(_SAMPLED_KEY) if sampled is not None: # The specification encodes an enabled tracing decision as "1". # In the wild pre-standard implementations might still send "true". # "d" is set in the single header case when debugging is enabled. sampled = sampled.lower() in ('1', 'd', 'true') else: # If there's no incoming sampling decision, it was deferred to us. # Even though we set it to False here, we might still sample # depending on the tracer configuration. sampled = False trace_options = TraceOptions() trace_options.set_enabled(sampled) # TraceId and SpanId headers both have to exist if not trace_id or not span_id: return SpanContext(trace_options=trace_options) # Convert 64-bit trace ids to 128-bit if len(trace_id) == 16: trace_id = '0' * 16 + trace_id span_context = SpanContext(trace_id=trace_id, span_id=span_id, trace_options=trace_options, from_header=True) return span_context
def nope(*args, **kwargs): trace_options = TraceOptions() trace_options.set_enabled(False) return SpanContext(trace_options=trace_options)