Example #1
0
 def span_data_to_envelope(self, sd):
     envelope = Envelope(
         iKey=self.options.instrumentation_key,
         tags=dict(utils.azure_monitor_context),
         time=sd.start_time,
     )
     envelope.tags['ai.operation.id'] = sd.context.trace_id
     if sd.parent_span_id:
         envelope.tags['ai.operation.parentId'] = '|{}.{}.'.format(
             sd.context.trace_id,
             sd.parent_span_id,
         )
     if sd.span_kind == SpanKind.SERVER:
         envelope.name = 'Microsoft.ApplicationInsights.Request'
         data = Request(
             id='|{}.{}.'.format(sd.context.trace_id, sd.span_id),
             duration=utils.timestamp_to_duration(
                 sd.start_time,
                 sd.end_time,
             ),
             responseCode='0',  # TODO
             success=True,  # TODO
         )
         envelope.data = Data(baseData=data, baseType='RequestData')
         if 'http.method' in sd.attributes:
             data.name = sd.attributes['http.method']
         if 'http.url' in sd.attributes:
             data.name = data.name + ' ' + sd.attributes['http.url']
             data.url = sd.attributes['http.url']
         if 'http.status_code' in sd.attributes:
             data.responseCode = str(sd.attributes['http.status_code'])
     else:
         envelope.name = \
             'Microsoft.ApplicationInsights.RemoteDependency'
         data = RemoteDependency(
             name=sd.name,  # TODO
             id='|{}.{}.'.format(sd.context.trace_id, sd.span_id),
             resultCode='0',  # TODO
             duration=utils.timestamp_to_duration(
                 sd.start_time,
                 sd.end_time,
             ),
             success=True,  # TODO
         )
         envelope.data = Data(
             baseData=data,
             baseType='RemoteDependencyData',
         )
         if sd.span_kind == SpanKind.CLIENT:
             data.type = 'HTTP'  # TODO
             if 'http.url' in sd.attributes:
                 url = sd.attributes['http.url']
                 # TODO: error handling, probably put scheme as well
                 data.name = utils.url_to_dependency_name(url)
             if 'http.status_code' in sd.attributes:
                 data.resultCode = str(sd.attributes['http.status_code'])
         else:
             data.type = 'INPROC'
     # TODO: links, tracestate, tags, attrs
     return envelope
Example #2
0
 def test_url_to_dependency_name(self):
     self.assertEqual(
         utils.url_to_dependency_name(
             'https://www.wikipedia.org/wiki/Rabbit'), 'www.wikipedia.org')