Skip to content

Commit 90396a1

Browse files
committed
Add more metric for cache hit debug
1 parent 2f31039 commit 90396a1

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

kmsauth/__init__.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ def decrypt_token(self, username, token):
296296
# Decrypt doesn't take KeyId as an argument. We need to verify
297297
# the correct key was used to do the decryption.
298298
# Annoyingly, the KeyId from the data is actually an arn.
299+
key_validation_start = datetime.datetime.utcnow()
299300
key_arn = data['KeyId']
300301
if user_type == 'service':
301302
if not self._valid_service_auth_key(key_arn):
@@ -311,9 +312,16 @@ def decrypt_token(self, username, token):
311312
raise TokenValidationError(
312313
'Authentication error. Unsupported user_type.'
313314
)
315+
if self.stats:
316+
key_validation_duration = (datetime.datetime.utcnow() - key_validation_start).total_seconds() * 1000 # noqa: E501
317+
self.stats.timing('key_validation_duration', key_validation_duration) # noqa: E501
318+
json_start = datetime.datetime.utcnow()
314319
plaintext = data['Plaintext']
315320
payload = json.loads(plaintext)
316321
key_alias = self._get_key_alias_from_cache(key_arn)
322+
if self.stats:
323+
json_duration = (datetime.datetime.utcnow() - json_start).total_seconds() * 1000 # noqa: E501
324+
self.stats.timing('json_processing_duration', json_duration)
317325
ret = {'payload': payload, 'key_alias': key_alias}
318326
except TokenValidationError:
319327
raise
@@ -337,6 +345,7 @@ def decrypt_token(self, username, token):
337345
now = datetime.datetime.utcnow()
338346
if self.stats:
339347
self.stats.timing('decrypt_token_validation_duration', (now - time_start).total_seconds() * 1000) # noqa: E501
348+
time_validation_start = datetime.datetime.utcnow()
340349
try:
341350
not_before = datetime.datetime.strptime(
342351
ret['payload']['not_before'],
@@ -364,9 +373,12 @@ def decrypt_token(self, username, token):
364373
raise TokenValidationError(
365374
'Authentication error. Invalid time validity for token.'
366375
)
376+
if self.stats:
377+
time_validation_duration = (datetime.datetime.utcnow() - time_validation_start).total_seconds() * 1000 # noqa: E501
378+
self.stats.timing('time_validation_duration', time_validation_duration) # noqa: E501
367379

368380
self.TOKENS[token_key] = ret
369-
duration = (datetime.datetime.utcnow() - now).total_seconds() * 1000
381+
duration = (datetime.datetime.utcnow() - now).total_seconds() * 1000 # noqa: E501
370382
if self.stats:
371383
self.stats.timing('decrypt_token_duration_post_validation', duration) # noqa: E501
372384
self.stats.incr('token_cache_set')

0 commit comments

Comments
 (0)