Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 3b48294

Browse files
authored
Fix HTTP attributes for deps, fix success and response/resultCode for Azure Exporter (#827)
1 parent 22c1a28 commit 3b48294

File tree

8 files changed

+338
-46
lines changed

8 files changed

+338
-46
lines changed

contrib/opencensus-ext-azure/opencensus/ext/azure/common/utils.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@
2323
from opencensus.common.version import __version__ as opencensus_version
2424
from opencensus.ext.azure.common.version import __version__ as ext_version
2525

26-
try:
27-
from urllib.parse import urlparse
28-
except ImportError:
29-
from urlparse import urlparse
30-
31-
3226
azure_monitor_context = {
3327
'ai.cloud.role': os.path.basename(sys.argv[0]) or 'Python Application',
3428
'ai.cloud.roleInstance': platform.node(),
@@ -64,10 +58,6 @@ def timestamp_to_iso_str(timestamp):
6458
return to_iso_str(datetime.datetime.utcfromtimestamp(timestamp))
6559

6660

67-
def url_to_dependency_name(url):
68-
return urlparse(url).netloc
69-
70-
7161
# Validate UUID format
7262
# Specs taken from https://tools.ietf.org/html/rfc4122
7363
uuid_regex_pattern = re.compile('^[0-9a-f]{8}-'

contrib/opencensus-ext-azure/opencensus/ext/azure/trace_exporter/__init__.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
from opencensus.ext.azure.common.transport import TransportMixin
2828
from opencensus.trace.span import SpanKind
2929

30+
try:
31+
from urllib.parse import urlparse
32+
except ImportError:
33+
from urlparse import urlparse
34+
3035
logger = logging.getLogger(__name__)
3136

3237
__all__ = ['AzureExporter']
@@ -55,66 +60,86 @@ def span_data_to_envelope(self, sd):
5560
tags=dict(utils.azure_monitor_context),
5661
time=sd.start_time,
5762
)
63+
5864
envelope.tags['ai.operation.id'] = sd.context.trace_id
5965
if sd.parent_span_id:
60-
envelope.tags['ai.operation.parentId'] = '|{}.{}.'.format(
61-
sd.context.trace_id,
66+
envelope.tags['ai.operation.parentId'] = '{}'.format(
6267
sd.parent_span_id,
6368
)
6469
if sd.span_kind == SpanKind.SERVER:
6570
envelope.name = 'Microsoft.ApplicationInsights.Request'
6671
data = Request(
67-
id='|{}.{}.'.format(sd.context.trace_id, sd.span_id),
72+
id='{}'.format(sd.span_id),
6873
duration=utils.timestamp_to_duration(
6974
sd.start_time,
7075
sd.end_time,
7176
),
72-
responseCode='0',
73-
success=False,
77+
responseCode=str(sd.status.code),
78+
success=False, # Modify based off attributes or status
7479
properties={},
7580
)
7681
envelope.data = Data(baseData=data, baseType='RequestData')
82+
data.name = ''
7783
if 'http.method' in sd.attributes:
7884
data.name = sd.attributes['http.method']
7985
if 'http.route' in sd.attributes:
8086
data.name = data.name + ' ' + sd.attributes['http.route']
8187
envelope.tags['ai.operation.name'] = data.name
88+
data.properties['request.name'] = data.name
89+
elif 'http.path' in sd.attributes:
90+
data.properties['request.name'] = data.name + \
91+
' ' + sd.attributes['http.path']
8292
if 'http.url' in sd.attributes:
8393
data.url = sd.attributes['http.url']
94+
data.properties['request.url'] = sd.attributes['http.url']
8495
if 'http.status_code' in sd.attributes:
8596
status_code = sd.attributes['http.status_code']
8697
data.responseCode = str(status_code)
8798
data.success = (
8899
status_code >= 200 and status_code <= 399
89100
)
101+
elif sd.status.code == 0:
102+
data.success = True
90103
else:
91104
envelope.name = \
92105
'Microsoft.ApplicationInsights.RemoteDependency'
93106
data = RemoteDependency(
94107
name=sd.name, # TODO
95-
id='|{}.{}.'.format(sd.context.trace_id, sd.span_id),
96-
resultCode='0', # TODO
108+
id='{}'.format(sd.span_id),
109+
resultCode=str(sd.status.code),
97110
duration=utils.timestamp_to_duration(
98111
sd.start_time,
99112
sd.end_time,
100113
),
101-
success=True, # TODO
114+
success=False, # Modify based off attributes or status
102115
properties={},
103116
)
104117
envelope.data = Data(
105118
baseData=data,
106119
baseType='RemoteDependencyData',
107120
)
108121
if sd.span_kind == SpanKind.CLIENT:
109-
data.type = 'HTTP' # TODO
122+
data.type = sd.attributes.get('component')
110123
if 'http.url' in sd.attributes:
111124
url = sd.attributes['http.url']
112125
# TODO: error handling, probably put scheme as well
113-
data.name = utils.url_to_dependency_name(url)
126+
data.data = url
127+
parse_url = urlparse(url)
128+
# target matches authority (host:port)
129+
data.target = parse_url.netloc
130+
if 'http.method' in sd.attributes:
131+
# name is METHOD/path
132+
data.name = sd.attributes['http.method'] \
133+
+ ' ' + parse_url.path
114134
if 'http.status_code' in sd.attributes:
115-
data.resultCode = str(sd.attributes['http.status_code'])
135+
status_code = sd.attributes["http.status_code"]
136+
data.resultCode = str(status_code)
137+
data.success = 200 <= status_code < 400
138+
elif sd.status.code == 0:
139+
data.success = True
116140
else:
117141
data.type = 'INPROC'
142+
data.success = True
118143
# TODO: links, tracestate, tags
119144
for key in sd.attributes:
120145
# This removes redundant data from ApplicationInsights

0 commit comments

Comments
 (0)