Ejemplo n.º 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.parent_span_id, )
        if sd.span_kind == SpanKind.SERVER:
            envelope.name = 'Microsoft.ApplicationInsights.Request'
            data = Request(
                id='{}'.format(sd.span_id),
                duration=utils.timestamp_to_duration(
                    sd.start_time,
                    sd.end_time,
                ),
                responseCode=str(sd.status.code),
                success=False,  # Modify based off attributes or status
                properties={},
            )
            envelope.data = Data(baseData=data, baseType='RequestData')
            data.name = ''
            if 'http.method' in sd.attributes:
                data.name = sd.attributes['http.method']
            if 'http.route' in sd.attributes:
                data.name = data.name + ' ' + sd.attributes['http.route']
                envelope.tags['ai.operation.name'] = data.name
                data.properties['request.name'] = data.name
            elif 'http.path' in sd.attributes:
                data.properties['request.name'] = data.name + \
                    ' ' + sd.attributes['http.path']
            if 'http.url' in sd.attributes:
                data.url = sd.attributes['http.url']
                data.properties['request.url'] = sd.attributes['http.url']
            if 'http.status_code' in sd.attributes:
                status_code = sd.attributes['http.status_code']
                data.responseCode = str(status_code)
                data.success = (status_code >= 200 and status_code <= 399)
            elif sd.status.code == 0:
                data.success = True
        else:
            envelope.name = \
                'Microsoft.ApplicationInsights.RemoteDependency'
            data = RemoteDependency(
                name=sd.name,  # TODO
                id='{}'.format(sd.span_id),
                resultCode=str(sd.status.code),
                duration=utils.timestamp_to_duration(
                    sd.start_time,
                    sd.end_time,
                ),
                success=False,  # Modify based off attributes or status
                properties={},
            )
            envelope.data = Data(
                baseData=data,
                baseType='RemoteDependencyData',
            )
            if sd.span_kind == SpanKind.CLIENT:
                data.type = sd.attributes.get('component')
                if 'http.url' in sd.attributes:
                    url = sd.attributes['http.url']
                    # TODO: error handling, probably put scheme as well
                    data.data = url
                    parse_url = urlparse(url)
                    # target matches authority (host:port)
                    data.target = parse_url.netloc
                    if 'http.method' in sd.attributes:
                        # name is METHOD/path
                        data.name = sd.attributes['http.method'] \
                            + ' ' + parse_url.path
                if 'http.status_code' in sd.attributes:
                    status_code = sd.attributes["http.status_code"]
                    data.resultCode = str(status_code)
                    data.success = 200 <= status_code < 400
                elif sd.status.code == 0:
                    data.success = True
            else:
                data.type = 'INPROC'
                data.success = True
        # TODO: links, tracestate, tags
        for key in sd.attributes:
            # This removes redundant data from ApplicationInsights
            if key.startswith('http.'):
                continue
            data.properties[key] = sd.attributes[key]
        return envelope
Ejemplo n.º 2
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',
             success=False,
             properties={},
         )
         envelope.data = Data(baseData=data, baseType='RequestData')
         if 'http.method' in sd.attributes:
             data.name = sd.attributes['http.method']
         if 'http.route' in sd.attributes:
             data.name = data.name + ' ' + sd.attributes['http.route']
             envelope.tags['ai.operation.name'] = data.name
         if 'http.url' in sd.attributes:
             data.url = sd.attributes['http.url']
         if 'http.status_code' in sd.attributes:
             status_code = sd.attributes['http.status_code']
             data.responseCode = str(status_code)
             data.success = (status_code >= 200 and status_code <= 399)
     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
             properties={},
         )
         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
     for key in sd.attributes:
         # This removes redundant data from ApplicationInsights
         if key.startswith('http.'):
             continue
         data.properties[key] = sd.attributes[key]
     return envelope
Ejemplo n.º 3
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.parent_span_id,
            )
        if sd.span_kind == SpanKind.SERVER:
            if ERROR_MESSAGE in sd.attributes:
                envelope.name = 'Microsoft.ApplicationInsights.Exception'
                data = ExceptionData(
                    exceptions=[{
                        'id': 1,
                        'outerId': '{}'.format(sd.span_id),
                        'typeName': sd.attributes.get(ERROR_NAME, ''),
                        'message': sd.attributes[ERROR_MESSAGE],
                        'hasFullStack': STACKTRACE in sd.attributes,
                        'parsedStack': sd.attributes.get(STACKTRACE, None)
                    }],
                )
                envelope.data = Data(baseData=data, baseType='ExceptionData')
                yield envelope

            envelope.name = 'Microsoft.ApplicationInsights.Request'
            data = Request(
                id='{}'.format(sd.span_id),
                duration=utils.timestamp_to_duration(
                    sd.start_time,
                    sd.end_time,
                ),
                responseCode=str(sd.status.code),
                success=False,  # Modify based off attributes or status
                properties={},
            )
            envelope.data = Data(baseData=data, baseType='RequestData')
            data.name = ''
            if HTTP_METHOD in sd.attributes:
                data.name = sd.attributes[HTTP_METHOD]
            if HTTP_ROUTE in sd.attributes:
                data.name = data.name + ' ' + sd.attributes[HTTP_ROUTE]
                envelope.tags['ai.operation.name'] = data.name
                data.properties['request.name'] = data.name
            elif HTTP_PATH in sd.attributes:
                data.properties['request.name'] = data.name + \
                    ' ' + sd.attributes[HTTP_PATH]
            if HTTP_URL in sd.attributes:
                data.url = sd.attributes[HTTP_URL]
                data.properties['request.url'] = sd.attributes[HTTP_URL]
            if HTTP_STATUS_CODE in sd.attributes:
                status_code = sd.attributes[HTTP_STATUS_CODE]
                data.responseCode = str(status_code)
                data.success = (
                    status_code >= 200 and status_code <= 399
                )
            elif sd.status.code == 0:
                data.success = True
        else:
            envelope.name = \
                'Microsoft.ApplicationInsights.RemoteDependency'
            data = RemoteDependency(
                name=sd.name,  # TODO
                id='{}'.format(sd.span_id),
                resultCode=str(sd.status.code),
                duration=utils.timestamp_to_duration(
                    sd.start_time,
                    sd.end_time,
                ),
                success=False,  # Modify based off attributes or status
                properties={},
            )
            envelope.data = Data(
                baseData=data,
                baseType='RemoteDependencyData',
            )
            if sd.span_kind == SpanKind.CLIENT:
                data.type = sd.attributes.get('component')
                if HTTP_URL in sd.attributes:
                    url = sd.attributes[HTTP_URL]
                    # TODO: error handling, probably put scheme as well
                    data.data = url
                    parse_url = urlparse(url)
                    # target matches authority (host:port)
                    data.target = parse_url.netloc
                    if HTTP_METHOD in sd.attributes:
                        # name is METHOD/path
                        data.name = sd.attributes[HTTP_METHOD] \
                            + ' ' + parse_url.path
                if HTTP_STATUS_CODE in sd.attributes:
                    status_code = sd.attributes[HTTP_STATUS_CODE]
                    data.resultCode = str(status_code)
                    data.success = 200 <= status_code < 400
                elif sd.status.code == 0:
                    data.success = True
            else:
                data.type = 'INPROC'
                data.success = True
        if sd.links:
            links = []
            for link in sd.links:
                links.append(
                    {"operation_Id": link.trace_id, "id": link.span_id})
            data.properties["_MS.links"] = json.dumps(links)
        # TODO: tracestate, tags
        for key in sd.attributes:
            # This removes redundant data from ApplicationInsights
            if key.startswith('http.'):
                continue
            data.properties[key] = sd.attributes[key]
        yield envelope