-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathexternal_node.py
More file actions
184 lines (135 loc) · 5.82 KB
/
external_node.py
File metadata and controls
184 lines (135 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import urllib.parse as urlparse
from collections import namedtuple
import newrelic.core.trace_node
from newrelic.core import attribute
from newrelic.core.metric import TimeMetric
from newrelic.core.node_mixin import GenericNodeMixin
_ExternalNode = namedtuple(
"_ExternalNode",
[
"library",
"url",
"method",
"children",
"start_time",
"end_time",
"duration",
"exclusive",
"params",
"guid",
"agent_attributes",
"user_attributes",
],
)
class ExternalNode(_ExternalNode, GenericNodeMixin):
cross_process_id = None
external_txn_name = None
@property
def details(self):
if hasattr(self, "_details"):
return self._details
try:
self._details = urlparse.urlparse(self.url or "")
except Exception:
self._details = urlparse.urlparse("http://unknown.url")
return self._details
@property
def name(self):
return f"External/{self.netloc}/{self.library}/{self.method or ''}"
@property
def url_with_path(self):
details = self.details
url = urlparse.urlunsplit((details.scheme, details.netloc, details.path, "", ""))
return url
@property
def http_url(self):
if hasattr(self, "_http_url"):
return self._http_url
_, url_attr = attribute.process_user_attribute("http.url", self.url_with_path)
self._http_url = url_attr
return url_attr
@property
def netloc(self):
hostname = self.details.hostname or "unknown"
try:
scheme = self.details.scheme.lower()
port = self.details.port
except Exception:
scheme = None
port = None
if (scheme, port) in (("http", 80), ("https", 443)):
port = None
netloc = port and (f"{hostname}:{port}") or hostname
return netloc
def time_metrics(self, stats, root, parent):
"""Return a generator yielding the timed metrics for this
external node as well as all the child nodes.
"""
yield TimeMetric(name="External/all", scope="", duration=self.duration, exclusive=self.exclusive)
if root.type == "WebTransaction":
yield TimeMetric(name="External/allWeb", scope="", duration=self.duration, exclusive=self.exclusive)
else:
yield TimeMetric(name="External/allOther", scope="", duration=self.duration, exclusive=self.exclusive)
netloc = self.netloc
try:
# Remove cross_process_id from the params dict otherwise it shows
# up in the UI.
self.cross_process_id = self.params.pop("cross_process_id")
self.external_txn_name = self.params.pop("external_txn_name")
except KeyError:
self.cross_process_id = None
self.external_txn_name = None
name = f"External/{netloc}/all"
yield TimeMetric(name=name, scope="", duration=self.duration, exclusive=self.exclusive)
if self.cross_process_id is None:
method = self.method or ""
name = f"External/{netloc}/{self.library}/{method}"
yield TimeMetric(name=name, scope="", duration=self.duration, exclusive=self.exclusive)
yield TimeMetric(name=name, scope=root.path, duration=self.duration, exclusive=self.exclusive)
else:
name = f"ExternalTransaction/{netloc}/{self.cross_process_id}/{self.external_txn_name}"
yield TimeMetric(name=name, scope="", duration=self.duration, exclusive=self.exclusive)
yield TimeMetric(name=name, scope=root.path, duration=self.duration, exclusive=self.exclusive)
name = f"ExternalApp/{netloc}/{self.cross_process_id}/all"
yield TimeMetric(name=name, scope="", duration=self.duration, exclusive=self.exclusive)
def trace_node(self, stats, root, connections):
netloc = self.netloc
method = self.method or ""
if self.cross_process_id is None:
name = f"External/{netloc}/{self.library}/{method}"
else:
name = f"ExternalTransaction/{netloc}/{self.cross_process_id}/{self.external_txn_name}"
name = root.string_table.cache(name)
start_time = newrelic.core.trace_node.node_start_time(root, self)
end_time = newrelic.core.trace_node.node_end_time(root, self)
children = []
root.trace_node_count += 1
# Agent attributes
self.agent_attributes["http.url"] = self.http_url
params = self.get_trace_segment_params(root.settings, params=self.params)
return newrelic.core.trace_node.TraceNode(
start_time=start_time, end_time=end_time, name=name, params=params, children=children, label=None
)
def span_event(self, *args, **kwargs):
self.agent_attributes["http.url"] = self.http_url
attrs = super(ExternalNode, self).span_event(*args, **kwargs)
i_attrs = attrs[0]
i_attrs["category"] = "http"
i_attrs["span.kind"] = "client"
_, i_attrs["component"] = attribute.process_user_attribute("component", self.library)
if self.method:
_, i_attrs["http.method"] = attribute.process_user_attribute("http.method", self.method)
return attrs