def test_request_to_dict(self): data = protocol.Request() to_dict = { "ver": 2, "id": "", "duration": "", "responseCode": "", "success": True, "source": None, "name": None, "url": None, "properties": None, "measurements": None, } self.assertEqual(data.to_dict(), to_dict)
def _span_to_envelope(self, span: Span) -> protocol.Envelope: if not span: return None envelope = protocol.Envelope( ikey=self.options.instrumentation_key, tags=dict(utils.azure_monitor_context), time=ns_to_iso_str(span.start_time), ) envelope.tags["ai.operation.id"] = "{:032x}".format( span.context.trace_id) parent = span.parent if isinstance(parent, Span): parent = parent.context if parent: envelope.tags["ai.operation.parentId"] = "{:016x}".format( parent.span_id) if span.kind in (SpanKind.CONSUMER, SpanKind.SERVER): envelope.name = "Microsoft.ApplicationInsights.Request" data = protocol.Request( id="{:016x}".format(span.context.span_id), duration=utils.ns_to_duration(span.end_time - span.start_time), response_code=str(span.status.canonical_code.value), success=span.status.canonical_code == StatusCanonicalCode. OK, # Modify based off attributes or Status properties={}, ) envelope.data = protocol.Data(base_data=data, base_type="RequestData") if "http.method" in span.attributes: data.name = span.attributes["http.method"] if "http.route" in span.attributes: data.name = data.name + " " + span.attributes["http.route"] envelope.tags["ai.operation.name"] = data.name data.properties["request.name"] = data.name elif "http.path" in span.attributes: data.properties["request.name"] = ( data.name + " " + span.attributes["http.path"]) if "http.url" in span.attributes: data.url = span.attributes["http.url"] data.properties["request.url"] = span.attributes["http.url"] if "http.status_code" in span.attributes: status_code = span.attributes["http.status_code"] data.response_code = str(status_code) data.success = 200 <= status_code < 400 else: envelope.name = "Microsoft.ApplicationInsights.RemoteDependency" data = protocol.RemoteDependency( name=span.name, id="{:016x}".format(span.context.span_id), result_code=str(span.status.canonical_code.value), duration=utils.ns_to_duration(span.end_time - span.start_time), success=span.status.canonical_code == StatusCanonicalCode. OK, # Modify based off attributes or Status properties={}, ) envelope.data = protocol.Data(base_data=data, base_type="RemoteDependencyData") if span.kind in (SpanKind.CLIENT, SpanKind.PRODUCER): if ("component" in span.attributes and span.attributes["component"] == "http"): data.type = "HTTP" if "http.url" in span.attributes: url = span.attributes["http.url"] # data is the url data.data = url parse_url = urlparse(url) # TODO: error handling, probably put scheme as well # target matches authority (host:port) data.target = parse_url.netloc if "http.method" in span.attributes: # name is METHOD/path data.name = (span.attributes["http.method"] + "/" + parse_url.path) if "http.status_code" in span.attributes: status_code = span.attributes["http.status_code"] data.result_code = str(status_code) data.success = 200 <= status_code < 400 else: # SpanKind.INTERNAL data.type = "InProc" data.success = True for key in span.attributes: # This removes redundant data from ApplicationInsights if key.startswith("http."): continue data.properties[key] = span.attributes[key] if span.links: links = [] for link in span.links: operation_id = "{:032x}".format(link.context.trace_id) span_id = "{:016x}".format(link.context.span_id) links.append({"operation_Id": operation_id, "id": span_id}) data.properties["_MS.links"] = json.dumps(links) # TODO: tracestate, tags return envelope
def test_request(self): data = protocol.Request() self.assertEqual(data.ver, 2)
def span_to_envelope(self, span): # noqa pylint: disable=too-many-branches envelope = protocol.Envelope( iKey=self.options.instrumentation_key, tags=dict(util.azure_monitor_context), time=ns_to_iso_str(span.start_time), ) envelope.tags["ai.operation.id"] = "{:032x}".format( span.context.trace_id) parent = span.parent if isinstance(parent, Span): parent = parent.context if parent: envelope.tags[ "ai.operation.parentId"] = "|{:032x}.{:016x}.".format( parent.trace_id, parent.span_id) if span.kind in (SpanKind.CONSUMER, SpanKind.SERVER): envelope.name = "Microsoft.ApplicationInsights.Request" data = protocol.Request( id="|{:032x}.{:016x}.".format(span.context.trace_id, span.context.span_id), duration=self.ns_to_duration(span.end_time - span.start_time), responseCode="0", success=False, properties={}, ) envelope.data = protocol.Data(baseData=data, baseType="RequestData") if "http.method" in span.attributes: data.name = span.attributes["http.method"] if "http.route" in span.attributes: data.name = data.name + " " + span.attributes["http.route"] envelope.tags["ai.operation.name"] = data.name if "http.url" in span.attributes: data.url = span.attributes["http.url"] if "http.status_code" in span.attributes: status_code = span.attributes["http.status_code"] data.responseCode = str(status_code) data.success = 200 <= status_code < 400 else: envelope.name = "Microsoft.ApplicationInsights.RemoteDependency" data = protocol.RemoteDependency( name=span.name, id="|{:032x}.{:016x}.".format(span.context.trace_id, span.context.span_id), resultCode="0", # TODO duration=self.ns_to_duration(span.end_time - span.start_time), success=True, # TODO properties={}, ) envelope.data = protocol.Data(baseData=data, baseType="RemoteDependencyData") if span.kind in (SpanKind.CLIENT, SpanKind.PRODUCER): data.type = "HTTP" # TODO if "http.url" in span.attributes: url = span.attributes["http.url"] # TODO: error handling, probably put scheme as well data.name = urlparse(url).netloc if "http.status_code" in span.attributes: data.resultCode = str(span.attributes["http.status_code"]) else: # SpanKind.INTERNAL data.type = "InProc" for key in span.attributes: data.properties[key] = span.attributes[key] if span.links: links = [] for link in span.links: links.append({ "operation_Id": "{:032x}".format(link.context.trace_id), "id": "|{:032x}.{:016x}.".format(link.context.trace_id, link.context.span_id), }) data.properties["_MS.links"] = json.dumps(links) print(data.properties["_MS.links"]) # TODO: tracestate, tags return envelope