1
1
import json
2
- import unittest
3
2
import os
3
+ import sys
4
+ import unittest
5
+ from importlib import reload
4
6
from time import time
5
- from unittest .mock import patch , MagicMock
6
7
from unittest import mock
8
+ from unittest .mock import MagicMock , patch
9
+
7
10
from approvaltests .approvals import verify_as_json
8
11
12
+ from caching .lambda_cache import LambdaTagsCache
9
13
from enhanced_lambda_metrics import (
10
- parse_metrics_from_report_log ,
11
- parse_metrics_from_json_report_log ,
12
- parse_lambda_tags_from_arn ,
13
- generate_enhanced_lambda_metrics ,
14
14
create_out_of_memory_enhanced_metric ,
15
+ generate_enhanced_lambda_metrics ,
16
+ parse_lambda_tags_from_arn ,
17
+ parse_metrics_from_json_report_log ,
18
+ parse_metrics_from_report_log ,
15
19
)
16
20
17
- from caching .lambda_cache import LambdaTagsCache
18
-
19
21
20
22
class TestEnhancedLambdaMetrics (unittest .TestCase ):
21
23
maxDiff = None
22
24
malformed_report = "REPORT invalid report log line"
23
25
standard_report = (
24
- "REPORT RequestId: 8edab1f8-7d34-4a8e-a965-15ccbbb78d4c "
25
- "Duration: 0.62 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 51 MB"
26
+ "REPORT RequestId: 8edab1f8-7d34-4a8e-a965-15ccbbb78d4c "
27
+ "Duration: 0.62 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 51 MB"
26
28
)
27
29
cold_start_report = (
28
- "REPORT RequestId: 8edab1f8-7d34-4a8e-a965-15ccbbb78d4c "
29
- "Duration: 0.81 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 90 MB Init Duration: 1234 ms"
30
+ "REPORT RequestId: 8edab1f8-7d34-4a8e-a965-15ccbbb78d4c "
31
+ "Duration: 0.81 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 90 MB Init Duration: 1234 ms"
30
32
)
31
33
report_with_xray = (
32
34
"REPORT RequestId: 814ba7cb-071e-4181-9a09-fa41db5bccad\t Duration: 1711.87 ms\t "
@@ -303,7 +305,10 @@ def test_generate_enhanced_lambda_metrics_once_with_missing_arn(self):
303
305
304
306
@patch ("caching.base_tags_cache.send_forwarder_internal_metrics" )
305
307
@patch ("caching.lambda_cache.send_forwarder_internal_metrics" )
308
+ @patch .dict (os .environ , {"DD_FETCH_LAMBDA_TAGS" : "true" })
306
309
def test_generate_enhanced_lambda_metrics_refresh_s3_cache (self , mock1 , mock2 ):
310
+ reload (sys .modules ["settings" ])
311
+
307
312
tags_cache = LambdaTagsCache ("" )
308
313
tags_cache .get_cache_from_s3 = MagicMock (return_value = ({}, 1000 ))
309
314
tags_cache .acquire_s3_cache_lock = MagicMock ()
@@ -334,13 +339,12 @@ def test_generate_enhanced_lambda_metrics_refresh_s3_cache(self, mock1, mock2):
334
339
"timestamp" : 10000 ,
335
340
}
336
341
337
- os .environ ["DD_FETCH_LAMBDA_TAGS" ] = "True"
338
342
generate_enhanced_lambda_metrics (logs_input , tags_cache )
339
343
tags_cache .get_cache_from_s3 .assert_called_once ()
340
344
tags_cache .build_tags_cache .assert_called_once ()
341
345
tags_cache .write_cache_to_s3 .assert_called_once ()
342
- del os .environ ["DD_FETCH_LAMBDA_TAGS" ]
343
346
347
+ @patch .dict (os .environ , {"DD_FETCH_LAMBDA_TAGS" : "true" })
344
348
@patch ("caching.lambda_cache.LambdaTagsCache.release_s3_cache_lock" )
345
349
@patch ("caching.lambda_cache.LambdaTagsCache.acquire_s3_cache_lock" )
346
350
@patch ("caching.lambda_cache.LambdaTagsCache.get_resources_paginator" )
@@ -358,6 +362,8 @@ def test_generate_enhanced_lambda_metrics_client_error(
358
362
mock_acquire_lock ,
359
363
mock_release_lock ,
360
364
):
365
+ reload (sys .modules ["settings" ])
366
+
361
367
mock_acquire_lock .return_value = True
362
368
mock_get_s3_cache .return_value = (
363
369
{},
@@ -391,22 +397,23 @@ def test_generate_enhanced_lambda_metrics_client_error(
391
397
"timestamp" : 10000 ,
392
398
}
393
399
394
- os .environ ["DD_FETCH_LAMBDA_TAGS" ] = "True"
395
400
generate_enhanced_lambda_metrics (logs_input , tags_cache )
396
401
mock_get_s3_cache .assert_called_once ()
397
402
mock_get_s3_cache .reset_mock ()
398
403
mock_get_resources_paginator .assert_called_once ()
399
404
paginator .paginate .assert_called_once ()
400
405
assert mock_base_tags_cache_forward_metrics .call_count == 1
401
406
assert mock_lambda_cache_forward_metrics .call_count == 2
402
- del os .environ ["DD_FETCH_LAMBDA_TAGS" ]
403
407
404
408
@patch ("caching.lambda_cache.send_forwarder_internal_metrics" )
405
409
@patch ("caching.base_tags_cache.send_forwarder_internal_metrics" )
406
410
@patch ("caching.lambda_cache.LambdaTagsCache.get_cache_from_s3" )
411
+ @patch .dict (os .environ , {"DD_FETCH_LAMBDA_TAGS" : "true" })
407
412
def test_generate_enhanced_lambda_metrics_timeout (
408
413
self , mock_get_s3_cache , mock_forward_metrics , mock_base_forward_metrics
409
414
):
415
+ reload (sys .modules ["settings" ])
416
+
410
417
mock_get_s3_cache .return_value = (
411
418
{
412
419
"arn:aws:lambda:us-east-1:0:function:cloudwatch-event" : [
@@ -437,17 +444,18 @@ def test_generate_enhanced_lambda_metrics_timeout(
437
444
"timestamp" : 1591714946151 ,
438
445
}
439
446
440
- os .environ ["DD_FETCH_LAMBDA_TAGS" ] = "True"
441
447
generated_metrics = generate_enhanced_lambda_metrics (logs_input , tags_cache )
442
448
verify_as_json (generated_metrics )
443
- del os .environ ["DD_FETCH_LAMBDA_TAGS" ]
444
449
445
450
@patch ("caching.lambda_cache.send_forwarder_internal_metrics" )
446
451
@patch ("telemetry.send_forwarder_internal_metrics" )
447
452
@patch ("caching.lambda_cache.LambdaTagsCache.get_cache_from_s3" )
453
+ @patch .dict (os .environ , {"DD_FETCH_LAMBDA_TAGS" : "true" })
448
454
def test_generate_enhanced_lambda_metrics_out_of_memory (
449
455
self , mock_get_s3_cache , mock_forward_metrics , mock_base_forward_metrics
450
456
):
457
+ reload (sys .modules ["settings" ])
458
+
451
459
mock_get_s3_cache .return_value = (
452
460
{
453
461
"arn:aws:lambda:us-east-1:0:function:cloudwatch-event" : [
@@ -478,10 +486,8 @@ def test_generate_enhanced_lambda_metrics_out_of_memory(
478
486
"timestamp" : 1591714946151 ,
479
487
}
480
488
481
- os .environ ["DD_FETCH_LAMBDA_TAGS" ] = "True"
482
489
generated_metrics = generate_enhanced_lambda_metrics (logs_input , tags_cache )
483
490
verify_as_json (generated_metrics )
484
- del os .environ ["DD_FETCH_LAMBDA_TAGS" ]
485
491
486
492
487
493
if __name__ == "__main__" :
0 commit comments