@@ -136,15 +136,15 @@ def __call__(self, environ, start_response):
136
136
req = Request (environ )
137
137
138
138
# determine initiator based on token context
139
- initiator_project_id = environ . get ( 'HTTP_X_PROJECT_ID' , taxonomy . UNKNOWN )
140
- initiator_project_name = environ . get ( 'HTTP_X_PROJECT_NAME' , taxonomy . UNKNOWN )
141
- initiator_project_domain_id = environ . get ( 'HTTP_X_PROJECT_DOMAIN_ID' , taxonomy . UNKNOWN )
142
- initiator_project_domain_name = environ . get ( 'HTTP_X_DOMAIN_NAME' , taxonomy . UNKNOWN )
143
- initiator_domain_id = environ . get ( 'HTTP_X_DOMAIN_ID' , taxonomy . UNKNOWN )
144
- initiator_domain_name = environ . get ( 'HTTP_X_DOMAIN_NAME' , taxonomy . UNKNOWN )
145
- initiator_user_id = environ . get ( 'HTTP_X_USER_ID' , taxonomy . UNKNOWN )
146
- initiator_user_domain_id = environ . get ( 'HTTP_X_USER_DOMAIN_ID' , taxonomy . UNKNOWN )
147
- initiator_user_domain_name = environ . get ( 'HTTP_X_USER_DOMAIN_NAME' , taxonomy . UNKNOWN )
139
+ initiator_project_id = self . get_safe_from_environ ( environ , 'HTTP_X_PROJECT_ID' )
140
+ initiator_project_name = self . get_safe_from_environ ( environ , 'HTTP_X_PROJECT_NAME' )
141
+ initiator_project_domain_id = self . get_safe_from_environ ( environ , 'HTTP_X_PROJECT_DOMAIN_ID' )
142
+ initiator_project_domain_name = self . get_safe_from_environ ( environ , 'HTTP_X_DOMAIN_NAME' )
143
+ initiator_domain_id = self . get_safe_from_environ ( environ , 'HTTP_X_DOMAIN_ID' )
144
+ initiator_domain_name = self . get_safe_from_environ ( environ , 'HTTP_X_DOMAIN_NAME' )
145
+ initiator_user_id = self . get_safe_from_environ ( environ , 'HTTP_X_USER_ID' )
146
+ initiator_user_domain_id = self . get_safe_from_environ ( environ , 'HTTP_X_USER_DOMAIN_ID' )
147
+ initiator_user_domain_name = self . get_safe_from_environ ( environ , 'HTTP_X_USER_DOMAIN_NAME' )
148
148
initiator_host_address = req .client_addr or taxonomy .UNKNOWN
149
149
150
150
# determine target based on request path or keystone.token_info
@@ -252,14 +252,36 @@ def _start_response_wrapper(status, headers, exc_info=None):
252
252
labels .append ("status:{0}" .format (status_code ))
253
253
detail_labels .append ("status:{0}" .format (status_code ))
254
254
255
- self .metric_client .timing ('api_requests_duration_seconds' , int (round (1000 * (time .time () - start ))),
256
- tags = labels )
255
+ self .metric_client .timing (
256
+ 'api_requests_duration_seconds' , int (round (1000 * (time .time () - start ))), tags = labels
257
+ )
257
258
self .metric_client .increment ('api_requests_total' , tags = detail_labels )
258
259
except Exception as e :
259
260
self .logger .debug ("failed to submit metrics for %s: %s" % (str (labels ), str (e )))
260
261
finally :
261
262
self .metric_client .close_buffer ()
262
263
264
+ def get_safe_from_environ (self , environ , key , default = taxonomy .UNKNOWN ):
265
+ """
266
+ get value for a key from the environ dict ensuring it's never None or an empty string
267
+
268
+ :param environ: the request environ
269
+ :param key: the key in the environ dictionary
270
+ :param default: return value if key not found
271
+ :return: the value to the key or default
272
+ """
273
+ val = default
274
+ try :
275
+ v = environ .get (key , default )
276
+ if v and v != "" :
277
+ val = v
278
+
279
+ except Exception as e :
280
+ self .logger .debug ("error getting '{0}' from environ: {1}" .format (key , e ))
281
+
282
+ finally :
283
+ return val
284
+
263
285
def get_target_project_uid_from_path (self , path ):
264
286
"""
265
287
get the project uid from the path, which should look like
0 commit comments