Skip to content

Commit e3f2de1

Browse files
committed
Reorder inheritance on span nodes (#1539)
* Reorder inheritance on span nodes Core tracing will be adding a bunch of logic to the span_event method which is called via super on inheriting classes before those inheriting classes add additional attributes. In order for the core tracing logic to work correctly, this inheritance call order has to be reversed; the inheriting classes must first add the attributes and THEN call super. * Remove *args, **kwargs * [MegaLinter] Apply linters fixes --------- Co-authored-by: hmstepanek <[email protected]>
1 parent 92698ac commit e3f2de1

File tree

5 files changed

+27
-33
lines changed

5 files changed

+27
-33
lines changed

newrelic/core/external_node.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,15 @@ def trace_node(self, stats, root, connections):
169169
start_time=start_time, end_time=end_time, name=name, params=params, children=children, label=None
170170
)
171171

172-
def span_event(self, *args, **kwargs):
172+
def span_event(self, settings, base_attrs=None, parent_guid=None, attr_class=dict):
173173
self.agent_attributes["http.url"] = self.http_url
174-
attrs = super().span_event(*args, **kwargs)
175-
i_attrs = attrs[0]
176174

175+
i_attrs = (base_attrs and base_attrs.copy()) or attr_class()
177176
i_attrs["category"] = "http"
178177
i_attrs["span.kind"] = "client"
179-
_, i_attrs["component"] = attribute.process_user_attribute("component", self.library)
178+
i_attrs["component"] = self.library
180179

181180
if self.method:
182-
_, i_attrs["http.method"] = attribute.process_user_attribute("http.method", self.method)
181+
i_attrs["http.method"] = self.method
183182

184-
return attrs
183+
return super().span_event(settings, base_attrs=i_attrs, parent_guid=parent_guid, attr_class=attr_class)

newrelic/core/function_node.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,8 @@ def trace_node(self, stats, root, connections):
114114
start_time=start_time, end_time=end_time, name=name, params=params, children=children, label=self.label
115115
)
116116

117-
def span_event(self, *args, **kwargs):
118-
attrs = super().span_event(*args, **kwargs)
119-
i_attrs = attrs[0]
120-
117+
def span_event(self, settings, base_attrs=None, parent_guid=None, attr_class=dict):
118+
i_attrs = (base_attrs and base_attrs.copy()) or attr_class()
121119
i_attrs["name"] = f"{self.group}/{self.name}"
122120

123-
return attrs
121+
return super().span_event(settings, base_attrs=i_attrs, parent_guid=parent_guid, attr_class=attr_class)

newrelic/core/loop_node.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ def trace_node(self, stats, root, connections):
7979
start_time=start_time, end_time=end_time, name=name, params=params, children=children, label=None
8080
)
8181

82-
def span_event(self, *args, **kwargs):
83-
attrs = super().span_event(*args, **kwargs)
84-
i_attrs = attrs[0]
85-
82+
def span_event(self, settings, base_attrs=None, parent_guid=None, attr_class=dict):
83+
i_attrs = (base_attrs and base_attrs.copy()) or attr_class()
8684
i_attrs["name"] = f"EventLoop/Wait/{self.name}"
8785

88-
return attrs
86+
return super().span_event(settings, base_attrs=i_attrs, parent_guid=parent_guid, attr_class=attr_class)

newrelic/core/node_mixin.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ def get_trace_segment_params(self, settings, params=None):
5252
def span_event(self, settings, base_attrs=None, parent_guid=None, attr_class=dict):
5353
i_attrs = (base_attrs and base_attrs.copy()) or attr_class()
5454
i_attrs["type"] = "Span"
55-
i_attrs["name"] = self.name
55+
i_attrs["name"] = i_attrs.get("name") or self.name
5656
i_attrs["guid"] = self.guid
5757
i_attrs["timestamp"] = int(self.start_time * 1000)
5858
i_attrs["duration"] = self.duration
59-
i_attrs["category"] = "generic"
59+
i_attrs["category"] = i_attrs.get("category") or "generic"
6060

6161
if parent_guid:
6262
i_attrs["parentId"] = parent_guid
@@ -108,37 +108,36 @@ def db_instance(self):
108108
self._db_instance = db_instance_attr
109109
return db_instance_attr
110110

111-
def span_event(self, *args, **kwargs):
112-
self.agent_attributes["db.instance"] = self.db_instance
113-
attrs = super().span_event(*args, **kwargs)
114-
i_attrs = attrs[0]
115-
a_attrs = attrs[2]
111+
def span_event(self, settings, base_attrs=None, parent_guid=None, attr_class=dict):
112+
a_attrs = self.agent_attributes
113+
a_attrs["db.instance"] = self.db_instance
114+
i_attrs = (base_attrs and base_attrs.copy()) or attr_class()
116115

117116
i_attrs["category"] = "datastore"
118117
i_attrs["span.kind"] = "client"
119118

120119
if self.product:
121-
i_attrs["component"] = a_attrs["db.system"] = attribute.process_user_attribute("db.system", self.product)[1]
120+
i_attrs["component"] = a_attrs["db.system"] = self.product
122121
if self.operation:
123-
a_attrs["db.operation"] = attribute.process_user_attribute("db.operation", self.operation)[1]
122+
a_attrs["db.operation"] = self.operation
124123
if self.target:
125-
a_attrs["db.collection"] = attribute.process_user_attribute("db.collection", self.target)[1]
124+
a_attrs["db.collection"] = self.target
126125

127126
if self.instance_hostname:
128-
peer_hostname = attribute.process_user_attribute("peer.hostname", self.instance_hostname)[1]
127+
peer_hostname = self.instance_hostname
129128
else:
130129
peer_hostname = "Unknown"
131130

132131
a_attrs["peer.hostname"] = a_attrs["server.address"] = peer_hostname
133132

134133
peer_address = f"{peer_hostname}:{self.port_path_or_id or 'Unknown'}"
135134

136-
a_attrs["peer.address"] = attribute.process_user_attribute("peer.address", peer_address)[1]
135+
a_attrs["peer.address"] = peer_address
137136

138137
# Attempt to treat port_path_or_id as an integer, fallback to not including it
139138
try:
140139
a_attrs["server.port"] = int(self.port_path_or_id)
141140
except Exception:
142141
pass
143142

144-
return attrs
143+
return super().span_event(settings, base_attrs=i_attrs, parent_guid=parent_guid, attr_class=attr_class)

newrelic/core/root_node.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@
3737

3838

3939
class RootNode(_RootNode, GenericNodeMixin):
40-
def span_event(self, *args, **kwargs):
41-
span = super().span_event(*args, **kwargs)
42-
i_attrs = span[0]
40+
def span_event(self, settings, base_attrs=None, parent_guid=None, attr_class=dict):
41+
i_attrs = (base_attrs and base_attrs.copy()) or attr_class()
4342
i_attrs["transaction.name"] = self.path
4443
i_attrs["nr.entryPoint"] = True
4544
if self.trusted_parent_span:
4645
i_attrs["trustedParentId"] = self.trusted_parent_span
4746
if self.tracing_vendors:
4847
i_attrs["tracingVendors"] = self.tracing_vendors
49-
return span
48+
49+
return super().span_event(settings, base_attrs=i_attrs, parent_guid=parent_guid, attr_class=attr_class)
5050

5151
def trace_node(self, stats, root, connections):
5252
name = self.path

0 commit comments

Comments
 (0)