|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | import newrelic.core.attribute as attribute
|
16 |
| - |
17 |
| -from newrelic.core.attribute_filter import (DST_SPAN_EVENTS, |
18 |
| - DST_TRANSACTION_SEGMENTS) |
| 16 | +from newrelic.core.attribute_filter import DST_SPAN_EVENTS, DST_TRANSACTION_SEGMENTS |
19 | 17 |
|
20 | 18 |
|
21 | 19 | class GenericNodeMixin(object):
|
22 | 20 | @property
|
23 | 21 | def processed_user_attributes(self):
|
24 |
| - if hasattr(self, '_processed_user_attributes'): |
| 22 | + if hasattr(self, "_processed_user_attributes"): |
25 | 23 | return self._processed_user_attributes
|
26 | 24 |
|
27 | 25 | self._processed_user_attributes = u_attrs = {}
|
28 |
| - user_attributes = getattr(self, 'user_attributes', u_attrs) |
| 26 | + user_attributes = getattr(self, "user_attributes", u_attrs) |
29 | 27 | for k, v in user_attributes.items():
|
30 | 28 | k, v = attribute.process_user_attribute(k, v)
|
31 |
| - u_attrs[k] = v |
| 29 | + # Only record the attribute if it passes processing. |
| 30 | + # Failures return (None, None). |
| 31 | + if k: |
| 32 | + u_attrs[k] = v |
32 | 33 | return u_attrs
|
33 | 34 |
|
34 | 35 | def get_trace_segment_params(self, settings, params=None):
|
35 | 36 | _params = attribute.resolve_agent_attributes(
|
36 |
| - self.agent_attributes, |
37 |
| - settings.attribute_filter, |
38 |
| - DST_TRANSACTION_SEGMENTS) |
| 37 | + self.agent_attributes, settings.attribute_filter, DST_TRANSACTION_SEGMENTS |
| 38 | + ) |
39 | 39 |
|
40 | 40 | if params:
|
41 | 41 | _params.update(params)
|
42 | 42 |
|
43 |
| - _params.update(attribute.resolve_user_attributes( |
44 |
| - self.processed_user_attributes, |
45 |
| - settings.attribute_filter, |
46 |
| - DST_TRANSACTION_SEGMENTS)) |
| 43 | + _params.update( |
| 44 | + attribute.resolve_user_attributes( |
| 45 | + self.processed_user_attributes, settings.attribute_filter, DST_TRANSACTION_SEGMENTS |
| 46 | + ) |
| 47 | + ) |
47 | 48 |
|
48 |
| - _params['exclusive_duration_millis'] = 1000.0 * self.exclusive |
| 49 | + _params["exclusive_duration_millis"] = 1000.0 * self.exclusive |
49 | 50 | return _params
|
50 | 51 |
|
51 |
| - def span_event( |
52 |
| - self, |
53 |
| - settings, |
54 |
| - base_attrs=None, |
55 |
| - parent_guid=None, |
56 |
| - attr_class=dict): |
| 52 | + def span_event(self, settings, base_attrs=None, parent_guid=None, attr_class=dict): |
57 | 53 | i_attrs = base_attrs and base_attrs.copy() or attr_class()
|
58 |
| - i_attrs['type'] = 'Span' |
59 |
| - i_attrs['name'] = self.name |
60 |
| - i_attrs['guid'] = self.guid |
61 |
| - i_attrs['timestamp'] = int(self.start_time * 1000) |
62 |
| - i_attrs['duration'] = self.duration |
63 |
| - i_attrs['category'] = 'generic' |
| 54 | + i_attrs["type"] = "Span" |
| 55 | + i_attrs["name"] = self.name |
| 56 | + i_attrs["guid"] = self.guid |
| 57 | + i_attrs["timestamp"] = int(self.start_time * 1000) |
| 58 | + i_attrs["duration"] = self.duration |
| 59 | + i_attrs["category"] = "generic" |
64 | 60 |
|
65 | 61 | if parent_guid:
|
66 |
| - i_attrs['parentId'] = parent_guid |
| 62 | + i_attrs["parentId"] = parent_guid |
67 | 63 |
|
68 | 64 | a_attrs = attribute.resolve_agent_attributes(
|
69 |
| - self.agent_attributes, |
70 |
| - settings.attribute_filter, |
71 |
| - DST_SPAN_EVENTS, |
72 |
| - attr_class=attr_class) |
| 65 | + self.agent_attributes, settings.attribute_filter, DST_SPAN_EVENTS, attr_class=attr_class |
| 66 | + ) |
73 | 67 |
|
74 | 68 | u_attrs = attribute.resolve_user_attributes(
|
75 |
| - self.processed_user_attributes, |
76 |
| - settings.attribute_filter, |
77 |
| - DST_SPAN_EVENTS, |
78 |
| - attr_class=attr_class) |
| 69 | + self.processed_user_attributes, settings.attribute_filter, DST_SPAN_EVENTS, attr_class=attr_class |
| 70 | + ) |
79 | 71 |
|
80 | 72 | # intrinsics, user attrs, agent attrs
|
81 | 73 | return [i_attrs, u_attrs, a_attrs]
|
82 | 74 |
|
83 |
| - def span_events(self, |
84 |
| - settings, base_attrs=None, parent_guid=None, attr_class=dict): |
85 |
| - |
86 |
| - yield self.span_event( |
87 |
| - settings, |
88 |
| - base_attrs=base_attrs, |
89 |
| - parent_guid=parent_guid, |
90 |
| - attr_class=attr_class) |
| 75 | + def span_events(self, settings, base_attrs=None, parent_guid=None, attr_class=dict): |
| 76 | + yield self.span_event(settings, base_attrs=base_attrs, parent_guid=parent_guid, attr_class=attr_class) |
91 | 77 |
|
92 | 78 | for child in self.children:
|
93 | 79 | for event in child.span_events(
|
94 |
| - settings, |
95 |
| - base_attrs=base_attrs, |
96 |
| - parent_guid=self.guid, |
97 |
| - attr_class=attr_class): |
| 80 | + settings, base_attrs=base_attrs, parent_guid=self.guid, attr_class=attr_class |
| 81 | + ): |
98 | 82 | yield event
|
99 | 83 |
|
100 | 84 |
|
101 | 85 | class DatastoreNodeMixin(GenericNodeMixin):
|
102 |
| - |
103 | 86 | @property
|
104 | 87 | def name(self):
|
105 | 88 | product = self.product
|
106 | 89 | target = self.target
|
107 |
| - operation = self.operation or 'other' |
| 90 | + operation = self.operation or "other" |
108 | 91 |
|
109 | 92 | if target:
|
110 |
| - name = 'Datastore/statement/%s/%s/%s' % (product, target, |
111 |
| - operation) |
| 93 | + name = "Datastore/statement/%s/%s/%s" % (product, target, operation) |
112 | 94 | else:
|
113 |
| - name = 'Datastore/operation/%s/%s' % (product, operation) |
| 95 | + name = "Datastore/operation/%s/%s" % (product, operation) |
114 | 96 |
|
115 | 97 | return name
|
116 | 98 |
|
117 | 99 | @property
|
118 | 100 | def db_instance(self):
|
119 |
| - if hasattr(self, '_db_instance'): |
| 101 | + if hasattr(self, "_db_instance"): |
120 | 102 | return self._db_instance
|
121 | 103 |
|
122 | 104 | db_instance_attr = None
|
123 | 105 | if self.database_name:
|
124 |
| - _, db_instance_attr = attribute.process_user_attribute( |
125 |
| - 'db.instance', self.database_name) |
| 106 | + _, db_instance_attr = attribute.process_user_attribute("db.instance", self.database_name) |
126 | 107 |
|
127 | 108 | self._db_instance = db_instance_attr
|
128 | 109 | return db_instance_attr
|
129 | 110 |
|
130 | 111 | def span_event(self, *args, **kwargs):
|
131 |
| - self.agent_attributes['db.instance'] = self.db_instance |
| 112 | + self.agent_attributes["db.instance"] = self.db_instance |
132 | 113 | attrs = super(DatastoreNodeMixin, self).span_event(*args, **kwargs)
|
133 | 114 | i_attrs = attrs[0]
|
134 | 115 | a_attrs = attrs[2]
|
135 | 116 |
|
136 |
| - i_attrs['category'] = 'datastore' |
137 |
| - i_attrs['component'] = self.product |
138 |
| - i_attrs['span.kind'] = 'client' |
| 117 | + i_attrs["category"] = "datastore" |
| 118 | + i_attrs["component"] = self.product |
| 119 | + i_attrs["span.kind"] = "client" |
139 | 120 |
|
140 | 121 | if self.instance_hostname:
|
141 |
| - _, a_attrs['peer.hostname'] = attribute.process_user_attribute( |
142 |
| - 'peer.hostname', self.instance_hostname) |
| 122 | + _, a_attrs["peer.hostname"] = attribute.process_user_attribute("peer.hostname", self.instance_hostname) |
143 | 123 | else:
|
144 |
| - a_attrs['peer.hostname'] = 'Unknown' |
| 124 | + a_attrs["peer.hostname"] = "Unknown" |
145 | 125 |
|
146 |
| - peer_address = '%s:%s' % ( |
147 |
| - self.instance_hostname or 'Unknown', |
148 |
| - self.port_path_or_id or 'Unknown') |
| 126 | + peer_address = "%s:%s" % (self.instance_hostname or "Unknown", self.port_path_or_id or "Unknown") |
149 | 127 |
|
150 |
| - _, a_attrs['peer.address'] = attribute.process_user_attribute( |
151 |
| - 'peer.address', peer_address) |
| 128 | + _, a_attrs["peer.address"] = attribute.process_user_attribute("peer.address", peer_address) |
152 | 129 |
|
153 | 130 | return attrs
|
0 commit comments