13
13
# limitations under the License.
14
14
15
15
import logging
16
-
17
16
from collections import namedtuple
18
17
18
+ from newrelic .core .attribute_filter import (
19
+ DST_ALL ,
20
+ DST_ERROR_COLLECTOR ,
21
+ DST_SPAN_EVENTS ,
22
+ DST_TRANSACTION_EVENTS ,
23
+ DST_TRANSACTION_SEGMENTS ,
24
+ DST_TRANSACTION_TRACER ,
25
+ )
19
26
from newrelic .packages import six
20
27
21
- from newrelic .core .attribute_filter import (DST_ALL , DST_ERROR_COLLECTOR ,
22
- DST_TRANSACTION_TRACER , DST_TRANSACTION_EVENTS , DST_SPAN_EVENTS ,
23
- DST_TRANSACTION_SEGMENTS )
24
-
25
-
26
28
_logger = logging .getLogger (__name__ )
27
29
28
- _Attribute = namedtuple ('_Attribute' ,
29
- ['name' , 'value' , 'destinations' ])
30
+ _Attribute = namedtuple ("_Attribute" , ["name" , "value" , "destinations" ])
30
31
31
32
# The following destinations are created here, never changed, and only
32
33
# used in create_agent_attributes. It is placed at the module level here
33
34
# as an optimization.
34
35
35
36
# All agent attributes go to transaction traces and error traces by default.
36
37
37
- _DESTINATIONS = (DST_ERROR_COLLECTOR |
38
- DST_TRANSACTION_TRACER |
39
- DST_TRANSACTION_SEGMENTS )
40
- _DESTINATIONS_WITH_EVENTS = (_DESTINATIONS |
41
- DST_TRANSACTION_EVENTS |
42
- DST_SPAN_EVENTS )
38
+ _DESTINATIONS = DST_ERROR_COLLECTOR | DST_TRANSACTION_TRACER | DST_TRANSACTION_SEGMENTS
39
+ _DESTINATIONS_WITH_EVENTS = _DESTINATIONS | DST_TRANSACTION_EVENTS | DST_SPAN_EVENTS
43
40
44
41
# The following subset goes to transaction events by default.
45
42
46
- _TRANSACTION_EVENT_DEFAULT_ATTRIBUTES = set ((
47
- 'host.displayName' ,
48
- 'request.method' ,
49
- 'request.headers.contentType' ,
50
- 'request.headers.contentLength' ,
51
- 'request.uri' ,
52
- 'response.status' ,
53
- 'request.headers.accept' ,
54
- 'response.headers.contentLength' ,
55
- 'response.headers.contentType' ,
56
- 'request.headers.host' ,
57
- 'request.headers.userAgent' ,
58
- 'message.queueName' ,
59
- 'message.routingKey' ,
60
- 'http.url' ,
61
- 'http.statusCode' ,
62
- 'aws.requestId' ,
63
- 'aws.operation' ,
64
- 'aws.lambda.arn' ,
65
- 'aws.lambda.coldStart' ,
66
- 'aws.lambda.eventSource.arn' ,
43
+ _TRANSACTION_EVENT_DEFAULT_ATTRIBUTES = set (
44
+ (
45
+ "host.displayName" ,
46
+ "request.method" ,
47
+ "request.headers.contentType" ,
48
+ "request.headers.contentLength" ,
49
+ "request.uri" ,
50
+ "response.status" ,
51
+ "request.headers.accept" ,
52
+ "response.headers.contentLength" ,
53
+ "response.headers.contentType" ,
54
+ "request.headers.host" ,
55
+ "request.headers.userAgent" ,
56
+ "message.queueName" ,
57
+ "message.routingKey" ,
58
+ "http.url" ,
59
+ "http.statusCode" ,
60
+ "aws.requestId" ,
61
+ "aws.operation" ,
62
+ "aws.lambda.arn" ,
63
+ "aws.lambda.coldStart" ,
64
+ "aws.lambda.eventSource.arn" ,
67
65
"db.collection" ,
68
- ' db.instance' ,
69
- ' db.operation' ,
70
- ' db.statement' ,
71
- ' error.class' ,
72
- ' error.message' ,
73
- ' error.expected' ,
74
- ' peer.hostname' ,
75
- ' peer.address' ,
76
- ' graphql.field.name' ,
77
- ' graphql.field.parentType' ,
78
- ' graphql.field.path' ,
79
- ' graphql.field.returnType' ,
80
- ' graphql.operation.name' ,
81
- ' graphql.operation.type' ,
82
- ' graphql.operation.query' ,
66
+ " db.instance" ,
67
+ " db.operation" ,
68
+ " db.statement" ,
69
+ " error.class" ,
70
+ " error.message" ,
71
+ " error.expected" ,
72
+ " peer.hostname" ,
73
+ " peer.address" ,
74
+ " graphql.field.name" ,
75
+ " graphql.field.parentType" ,
76
+ " graphql.field.path" ,
77
+ " graphql.field.returnType" ,
78
+ " graphql.operation.name" ,
79
+ " graphql.operation.type" ,
80
+ " graphql.operation.query" ,
83
81
"code.filepath" ,
84
82
"code.function" ,
85
83
"code.lineno" ,
86
84
"code.namespace" ,
87
- ))
85
+ )
86
+ )
88
87
89
88
MAX_NUM_USER_ATTRIBUTES = 128
90
89
MAX_ATTRIBUTE_LENGTH = 255
91
- MAX_64_BIT_INT = 2 ** 63 - 1
90
+ MAX_64_BIT_INT = 2 ** 63 - 1
92
91
MAX_LOG_MESSAGE_LENGTH = 32768
93
92
94
93
@@ -109,10 +108,8 @@ class CastingFailureException(Exception):
109
108
110
109
111
110
class Attribute (_Attribute ):
112
-
113
111
def __repr__ (self ):
114
- return "Attribute(name=%r, value=%r, destinations=%r)" % (
115
- self .name , self .value , bin (self .destinations ))
112
+ return "Attribute(name=%r, value=%r, destinations=%r)" % (self .name , self .value , bin (self .destinations ))
116
113
117
114
118
115
def create_attributes (attr_dict , destinations , attribute_filter ):
@@ -142,8 +139,7 @@ def create_agent_attributes(attr_dict, attribute_filter):
142
139
return attributes
143
140
144
141
145
- def resolve_user_attributes (
146
- attr_dict , attribute_filter , target_destination , attr_class = dict ):
142
+ def resolve_user_attributes (attr_dict , attribute_filter , target_destination , attr_class = dict ):
147
143
u_attrs = attr_class ()
148
144
149
145
for attr_name , attr_value in attr_dict .items ():
@@ -158,8 +154,7 @@ def resolve_user_attributes(
158
154
return u_attrs
159
155
160
156
161
- def resolve_agent_attributes (
162
- attr_dict , attribute_filter , target_destination , attr_class = dict ):
157
+ def resolve_agent_attributes (attr_dict , attribute_filter , target_destination , attr_class = dict ):
163
158
a_attrs = attr_class ()
164
159
165
160
for attr_name , attr_value in attr_dict .items ():
@@ -182,10 +177,9 @@ def create_user_attributes(attr_dict, attribute_filter):
182
177
return create_attributes (attr_dict , destinations , attribute_filter )
183
178
184
179
185
- def truncate (
186
- text , maxsize = MAX_ATTRIBUTE_LENGTH , encoding = 'utf-8' , ending = None ):
180
+ def truncate (text , maxsize = MAX_ATTRIBUTE_LENGTH , encoding = "utf-8" , ending = None ):
187
181
188
- # Truncate text so that it's byte representation
182
+ # Truncate text so that its byte representation
189
183
# is no longer than maxsize bytes.
190
184
191
185
# If text is unicode (Python 2 or 3), return unicode.
@@ -198,21 +192,21 @@ def truncate(
198
192
ending = ending and ending .encode (encoding )
199
193
200
194
if ending and truncated != text :
201
- truncated = truncated [:- len (ending )] + ending
195
+ truncated = truncated [: - len (ending )] + ending
202
196
203
197
return truncated
204
198
205
199
206
- def _truncate_unicode (u , maxsize , encoding = ' utf-8' ):
200
+ def _truncate_unicode (u , maxsize , encoding = " utf-8" ):
207
201
encoded = u .encode (encoding )[:maxsize ]
208
- return encoded .decode (encoding , ' ignore' )
202
+ return encoded .decode (encoding , " ignore" )
209
203
210
204
211
205
def _truncate_bytes (s , maxsize ):
212
206
return s [:maxsize ]
213
207
214
208
215
- def check_name_length (name , max_length = MAX_ATTRIBUTE_LENGTH , encoding = ' utf-8' ):
209
+ def check_name_length (name , max_length = MAX_ATTRIBUTE_LENGTH , encoding = " utf-8" ):
216
210
trunc_name = truncate (name , max_length , encoding )
217
211
if name != trunc_name :
218
212
raise NameTooLongException ()
@@ -228,8 +222,7 @@ def check_max_int(value, max_int=MAX_64_BIT_INT):
228
222
raise IntTooLargeException ()
229
223
230
224
231
- def process_user_attribute (
232
- name , value , max_length = MAX_ATTRIBUTE_LENGTH , ending = None ):
225
+ def process_user_attribute (name , value , max_length = MAX_ATTRIBUTE_LENGTH , ending = None ):
233
226
234
227
# Perform all necessary checks on a potential attribute.
235
228
#
@@ -250,23 +243,19 @@ def process_user_attribute(
250
243
value = sanitize (value )
251
244
252
245
except NameIsNotStringException :
253
- _logger .debug ('Attribute name must be a string. Dropping '
254
- 'attribute: %r=%r' , name , value )
246
+ _logger .debug ("Attribute name must be a string. Dropping " "attribute: %r=%r" , name , value )
255
247
return FAILED_RESULT
256
248
257
249
except NameTooLongException :
258
- _logger .debug ('Attribute name exceeds maximum length. Dropping '
259
- 'attribute: %r=%r' , name , value )
250
+ _logger .debug ("Attribute name exceeds maximum length. Dropping " "attribute: %r=%r" , name , value )
260
251
return FAILED_RESULT
261
252
262
253
except IntTooLargeException :
263
- _logger .debug ('Attribute value exceeds maximum integer value. '
264
- 'Dropping attribute: %r=%r' , name , value )
254
+ _logger .debug ("Attribute value exceeds maximum integer value. " "Dropping attribute: %r=%r" , name , value )
265
255
return FAILED_RESULT
266
256
267
257
except CastingFailureException :
268
- _logger .debug ('Attribute value cannot be cast to a string. '
269
- 'Dropping attribute: %r=%r' , name , value )
258
+ _logger .debug ("Attribute value cannot be cast to a string. " "Dropping attribute: %r=%r" , name , value )
270
259
return FAILED_RESULT
271
260
272
261
else :
@@ -278,9 +267,12 @@ def process_user_attribute(
278
267
if isinstance (value , valid_types_text ):
279
268
trunc_value = truncate (value , maxsize = max_length , ending = ending )
280
269
if value != trunc_value :
281
- _logger .debug ('Attribute value exceeds maximum length '
282
- '(%r bytes). Truncating value: %r=%r.' ,
283
- max_length , name , trunc_value )
270
+ _logger .debug (
271
+ "Attribute value exceeds maximum length " "(%r bytes). Truncating value: %r=%r." ,
272
+ max_length ,
273
+ name ,
274
+ trunc_value ,
275
+ )
284
276
285
277
value = trunc_value
286
278
@@ -294,8 +286,7 @@ def sanitize(value):
294
286
#
295
287
# Raise CastingFailureException, if str(value) somehow fails.
296
288
297
- valid_value_types = (six .text_type , six .binary_type , bool , float ,
298
- six .integer_types )
289
+ valid_value_types = (six .text_type , six .binary_type , bool , float , six .integer_types )
299
290
300
291
if not isinstance (value , valid_value_types ):
301
292
original = value
@@ -305,7 +296,8 @@ def sanitize(value):
305
296
except Exception :
306
297
raise CastingFailureException ()
307
298
else :
308
- _logger .debug ('Attribute value is of type: %r. Casting %r to '
309
- 'string: %s' , type (original ), original , value )
299
+ _logger .debug (
300
+ "Attribute value is of type: %r. Casting %r to " "string: %s" , type (original ), original , value
301
+ )
310
302
311
303
return value
0 commit comments