Skip to content

Commit e5e02ed

Browse files
committed
Add more metrics
1 parent bb7b70e commit e5e02ed

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

kmsauth/__init__.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,15 @@ def decrypt_token(self, username, token):
253253
version < self.minimum_token_version):
254254
raise TokenValidationError('Unacceptable token version.')
255255
if self.stats:
256-
parse_duration = (datetime.datetime.utcnow() - time_start).total_seconds() * 1000 # noqa: E501
257-
self.stats.timing('username_parse_duration', parse_duration) # noqa: E501
256+
# Checkpoint 1: After username parsing
257+
checkpoint_1 = (datetime.datetime.utcnow() - time_start).total_seconds() * 1000 # noqa: E501
258+
self.stats.timing('checkpoint_1_after_parse', checkpoint_1) # noqa: E501
259+
258260
self.stats.incr('token_version_{version}')
259261
self.stats.incr(f'cache_key_from_{_from}')
260262
self.stats.incr(f'cache_key_to_{self.to_auth_context}')
261263
self.stats.incr(f'cache_key_user_type_{user_type}')
262264

263-
cache_key_gen_start = datetime.datetime.utcnow()
264265
try:
265266
token_key = '{0}{1}{2}{3}'.format(
266267
hashlib.sha256(ensure_bytes(token)).hexdigest(),
@@ -270,15 +271,18 @@ def decrypt_token(self, username, token):
270271
)
271272
except Exception:
272273
raise TokenValidationError('Authentication error.')
274+
273275
if self.stats:
274-
cache_key_duration = (datetime.datetime.utcnow() - cache_key_gen_start).total_seconds() * 1000 # noqa: E501
275-
self.stats.timing('cache_key_generation_duration', cache_key_duration) # noqa: E501
276+
# Checkpoint 2: After cache key generation
277+
checkpoint_2 = (datetime.datetime.utcnow() - time_start).total_seconds() * 1000 # noqa: E501
278+
self.stats.timing('checkpoint_2_after_cache_key', checkpoint_2) # noqa: E501
276279

277-
cache_lookup_start = datetime.datetime.utcnow()
278280
cache_miss = token_key not in self.TOKENS
281+
279282
if self.stats:
280-
cache_lookup_duration = (datetime.datetime.utcnow() - cache_lookup_start).total_seconds() * 1000 # noqa: E501
281-
self.stats.timing('cache_lookup_duration', cache_lookup_duration) # noqa: E501
283+
# Checkpoint 3: After cache lookup
284+
checkpoint_3 = (datetime.datetime.utcnow() - time_start).total_seconds() * 1000 # noqa: E501
285+
self.stats.timing('checkpoint_3_after_cache_lookup', checkpoint_3) # noqa: E501
282286

283287
if cache_miss:
284288
if self.stats:
@@ -302,6 +306,9 @@ def decrypt_token(self, username, token):
302306
CiphertextBlob=token,
303307
EncryptionContext=context
304308
)
309+
# Checkpoint 4: After KMS decrypt
310+
checkpoint_4 = (datetime.datetime.utcnow() - time_start).total_seconds() * 1000 # noqa: E501
311+
self.stats.timing('checkpoint_4_after_kms_decrypt', checkpoint_4) # noqa: E501
305312
else:
306313
data = self.kms_client.decrypt(
307314
CiphertextBlob=token,
@@ -310,7 +317,6 @@ def decrypt_token(self, username, token):
310317
# Decrypt doesn't take KeyId as an argument. We need to verify
311318
# the correct key was used to do the decryption.
312319
# Annoyingly, the KeyId from the data is actually an arn.
313-
key_validation_start = datetime.datetime.utcnow()
314320
key_arn = data['KeyId']
315321
if user_type == 'service':
316322
if not self._valid_service_auth_key(key_arn):
@@ -327,15 +333,16 @@ def decrypt_token(self, username, token):
327333
'Authentication error. Unsupported user_type.'
328334
)
329335
if self.stats:
330-
key_validation_duration = (datetime.datetime.utcnow() - key_validation_start).total_seconds() * 1000 # noqa: E501
331-
self.stats.timing('key_validation_duration', key_validation_duration) # noqa: E501
332-
json_start = datetime.datetime.utcnow()
336+
# Checkpoint 5: After key validation
337+
checkpoint_5 = (datetime.datetime.utcnow() - time_start).total_seconds() * 1000 # noqa: E501
338+
self.stats.timing('checkpoint_5_after_key_validation', checkpoint_5) # noqa: E501
333339
plaintext = data['Plaintext']
334340
payload = json.loads(plaintext)
335341
key_alias = self._get_key_alias_from_cache(key_arn)
336342
if self.stats:
337-
json_duration = (datetime.datetime.utcnow() - json_start).total_seconds() * 1000 # noqa: E501
338-
self.stats.timing('json_processing_duration', json_duration)
343+
# Checkpoint 6: After JSON processing
344+
checkpoint_6 = (datetime.datetime.utcnow() - time_start).total_seconds() * 1000 # noqa: E501
345+
self.stats.timing('checkpoint_6_after_json_processing', checkpoint_6) # noqa: E501
339346
ret = {'payload': payload, 'key_alias': key_alias}
340347
except TokenValidationError:
341348
raise
@@ -352,13 +359,13 @@ def decrypt_token(self, username, token):
352359
'Authentication error. General error.'
353360
)
354361
else:
355-
cache_hit_start = datetime.datetime.utcnow()
356362
if self.stats:
357363
self.stats.incr('token_cache_hit')
358364
ret = self.TOKENS[token_key]
359365
if self.stats:
360-
cache_hit_duration = (datetime.datetime.utcnow() - cache_hit_start).total_seconds() * 1000 # noqa: E501
361-
self.stats.timing('cache_hit_lookup_duration', cache_hit_duration) # noqa: E501
366+
# Checkpoint 7: After cache hit
367+
checkpoint_7 = (datetime.datetime.utcnow() - time_start).total_seconds() * 1000 # noqa: E501
368+
self.stats.timing('checkpoint_7_after_cache_hit', checkpoint_7) # noqa: E501
362369

363370
now = datetime.datetime.utcnow()
364371
if self.stats:
@@ -367,7 +374,6 @@ def decrypt_token(self, username, token):
367374
self.stats.timing('pre_time_validation_duration', pre_time_validation_duration) # noqa: E501
368375
# Original total validation duration metric
369376
self.stats.timing('decrypt_token_validation_duration', (now - time_start).total_seconds() * 1000) # noqa: E501
370-
time_validation_start = datetime.datetime.utcnow()
371377
try:
372378
not_before = datetime.datetime.strptime(
373379
ret['payload']['not_before'],
@@ -396,8 +402,9 @@ def decrypt_token(self, username, token):
396402
'Authentication error. Invalid time validity for token.'
397403
)
398404
if self.stats:
399-
time_validation_duration = (datetime.datetime.utcnow() - time_validation_start).total_seconds() * 1000 # noqa: E501
400-
self.stats.timing('time_validation_duration', time_validation_duration) # noqa: E501
405+
# Checkpoint 8: After time validation
406+
checkpoint_8 = (datetime.datetime.utcnow() - time_start).total_seconds() * 1000 # noqa: E501
407+
self.stats.timing('checkpoint_8_after_time_validation', checkpoint_8) # noqa: E501
401408

402409
cache_set_start = datetime.datetime.utcnow()
403410
self.TOKENS[token_key] = ret

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from setuptools import setup, find_packages
1515

16-
VERSION = "0.6.4.dev4"
16+
VERSION = "0.6.4.dev5"
1717

1818
requirements = [
1919
# Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK)

0 commit comments

Comments
 (0)