|
8 | 8 | from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult |
9 | 9 | from opentelemetry.sdk.util import ns_to_iso_str |
10 | 10 | from opentelemetry.trace import Span, SpanKind |
11 | | -from opentelemetry.trace.status import StatusCode |
12 | 11 |
|
13 | 12 | from microsoft.opentelemetry.exporter.azuremonitor import utils |
14 | 13 | from microsoft.opentelemetry.exporter.azuremonitor._generated.models import ( |
@@ -78,75 +77,119 @@ def convert_span_to_envelope(span: Span) -> TelemetryItem: |
78 | 77 | if span.kind in (SpanKind.CONSUMER, SpanKind.SERVER): |
79 | 78 | envelope.name = "Microsoft.ApplicationInsights.Request" |
80 | 79 | data = RequestData( |
| 80 | + name=span.name, |
81 | 81 | id="{:016x}".format(span.context.span_id), |
82 | 82 | duration=utils.ns_to_duration(span.end_time - span.start_time), |
83 | 83 | response_code=str(span.status.status_code.value), |
84 | | - success=span.status.status_code |
85 | | - == StatusCode.OK, # Modify based off attributes or Status |
| 84 | + success=span.status.is_ok, |
86 | 85 | properties={}, |
87 | 86 | ) |
88 | 87 | envelope.data = MonitorBase(base_data=data, base_type="RequestData") |
89 | | - if "http.method" in span.attributes: |
90 | | - data.name = span.attributes["http.method"] |
| 88 | + if "http.method" in span.attributes: # HTTP |
91 | 89 | if "http.route" in span.attributes: |
92 | | - data.name = data.name + " " + span.attributes["http.route"] |
93 | | - envelope.tags["ai.operation.name"] = data.name |
94 | | - data.properties["request.name"] = data.name |
| 90 | + envelope.tags["ai.operation.name"] = span.attributes["http.route"] |
95 | 91 | elif "http.path" in span.attributes: |
96 | | - data.properties["request.name"] = ( |
97 | | - data.name + " " + span.attributes["http.path"] |
98 | | - ) |
99 | | - if "http.url" in span.attributes: |
100 | | - data.url = span.attributes["http.url"] |
101 | | - data.properties["request.url"] = span.attributes["http.url"] |
102 | | - if "http.status_code" in span.attributes: |
103 | | - status_code = span.attributes["http.status_code"] |
104 | | - data.response_code = str(status_code) |
105 | | - data.success = 200 <= status_code < 400 |
| 92 | + envelope.tags["ai.operation.name"] = span.attributes["http.path"] |
| 93 | + else: |
| 94 | + envelope.tags["ai.operation.name"] = span.name |
| 95 | + |
| 96 | + if "http.url" in span.attributes: |
| 97 | + data.url = span.attributes["http.url"] |
| 98 | + data.properties["request.url"] = span.attributes["http.url"] |
| 99 | + if "http.status_code" in span.attributes: |
| 100 | + status_code = span.attributes["http.status_code"] |
| 101 | + data.response_code = str(status_code) |
| 102 | + elif "messaging.system" in span.attributes: # Messaging |
| 103 | + envelope.tags["ai.operation.name"] = span.name |
| 104 | + |
| 105 | + if "messaging.destination" in span.attributes: |
| 106 | + if "net.peer.name" in span.attributes: |
| 107 | + data.properties["source"] = "{}/{}".format( |
| 108 | + span.attributes["net.peer.name"], |
| 109 | + span.attributes["messaging.destination"], |
| 110 | + ) |
| 111 | + elif "net.peer.ip" in span.attributes: |
| 112 | + data.properties["source"] = "{}/{}".format( |
| 113 | + span.attributes["net.peer.ip"], |
| 114 | + span.attributes["messaging.destination"], |
| 115 | + ) |
| 116 | + else: |
| 117 | + data.properties["source"] = span.attributes["messaging.destination"] |
106 | 118 | else: |
107 | 119 | envelope.name = "Microsoft.ApplicationInsights.RemoteDependency" |
108 | 120 | data = RemoteDependencyData( |
109 | 121 | name=span.name, |
110 | 122 | id="{:016x}".format(span.context.span_id), |
111 | 123 | result_code=str(span.status.status_code.value), |
112 | 124 | duration=utils.ns_to_duration(span.end_time - span.start_time), |
113 | | - success=span.status.status_code |
114 | | - == StatusCode.OK, # Modify based off attributes or Status |
| 125 | + success=span.status.is_ok, |
115 | 126 | properties={}, |
116 | 127 | ) |
117 | 128 | envelope.data = MonitorBase( |
118 | 129 | base_data=data, base_type="RemoteDependencyData" |
119 | 130 | ) |
120 | 131 | if span.kind in (SpanKind.CLIENT, SpanKind.PRODUCER): |
121 | | - if ( |
122 | | - "component" in span.attributes |
123 | | - and span.attributes["component"] == "http" |
124 | | - ): |
125 | | - # TODO: check other component types (e.g. db) |
| 132 | + if "http.method" in span.attributes: # HTTP |
126 | 133 | data.type = "HTTP" |
127 | | - if "http.url" in span.attributes: |
128 | | - url = span.attributes["http.url"] |
129 | | - # data is the url |
130 | | - data.data = url |
131 | | - parse_url = urlparse(url) |
132 | | - # TODO: error handling, probably put scheme as well |
133 | | - # target matches authority (host:port) |
134 | | - data.target = parse_url.netloc |
135 | | - if "http.method" in span.attributes: |
136 | | - # name is METHOD/path |
137 | | - data.name = ( |
138 | | - span.attributes["http.method"] + "/" + parse_url.path |
| 134 | + if "net.peer.port" in span.attributes: |
| 135 | + name = "" |
| 136 | + if "net.peer.name" in span.attributes: |
| 137 | + name = span.attributes["net.peer.name"] |
| 138 | + elif "net.peer.ip" in span.attributes: |
| 139 | + name = str(span.attributes["net.peer.ip"]) |
| 140 | + data.target = "{}:{}".format( |
| 141 | + name, |
| 142 | + str(span.attributes["net.peer.port"]), |
139 | 143 | ) |
140 | | - if "http.status_code" in span.attributes: |
141 | | - status_code = span.attributes["http.status_code"] |
142 | | - data.result_code = str(status_code) |
143 | | - data.success = 200 <= status_code < 400 |
| 144 | + elif "http.url" in span.attributes: |
| 145 | + url = span.attributes["http.url"] |
| 146 | + # data is the url |
| 147 | + data.data = url |
| 148 | + parse_url = urlparse(url) |
| 149 | + # target matches authority (host:port) |
| 150 | + data.target = parse_url.netloc |
| 151 | + if "http.status_code" in span.attributes: |
| 152 | + status_code = span.attributes["http.status_code"] |
| 153 | + data.result_code = str(status_code) |
| 154 | + elif "db.system" in span.attributes: # Database |
| 155 | + data.type = span.attributes["db.system"] |
| 156 | + # data is the full statement |
| 157 | + if "db.statement" in span.attributes: |
| 158 | + data.data = span.attributes["db.statement"] |
| 159 | + if "db.name" in span.attributes: |
| 160 | + data.target = span.attributes["db.name"] |
| 161 | + else: |
| 162 | + data.target = span.attributes["db.system"] |
| 163 | + elif "rpc.system" in span.attributes: # Rpc |
| 164 | + data.type = "rpc.system" |
| 165 | + if "rpc.service" in span.attributes: |
| 166 | + data.target = span.attributes["rpc.service"] |
| 167 | + else: |
| 168 | + data.target = span.attributes["rpc.system"] |
| 169 | + elif "messaging.system" in span.attributes: # Messaging |
| 170 | + data.type = "Queue Message | {}" \ |
| 171 | + .format(span.attributes["messaging.system"]) |
| 172 | + if "net.peer.ip" in span.attributes and \ |
| 173 | + "messaging.destination" in span.attributes: |
| 174 | + data.target = "{}/{}".format( |
| 175 | + span.attributes["net.peer.ip"], |
| 176 | + span.attributes["messaging.destination"] |
| 177 | + ) |
| 178 | + else: |
| 179 | + data.target = span.attributes["messaging.system"] |
| 180 | + else: |
| 181 | + # TODO: Azure specific types |
| 182 | + data.type = "N/A" |
144 | 183 | else: # SpanKind.INTERNAL |
145 | 184 | data.type = "InProc" |
146 | 185 | data.success = True |
147 | 186 | for key in span.attributes: |
148 | | - # This removes redundant data from ApplicationInsights |
149 | | - if key.startswith("http."): |
| 187 | + # Remove Opentelemetry related span attributes from custom dimensions |
| 188 | + if key.startswith("http.") or \ |
| 189 | + key.startswith("db.") or \ |
| 190 | + key.startswith("rpc.") or \ |
| 191 | + key.startswith("net.") or \ |
| 192 | + key.startswith("messaging."): |
150 | 193 | continue |
151 | 194 | data.properties[key] = span.attributes[key] |
152 | 195 | if span.links: |
|
0 commit comments