-
Notifications
You must be signed in to change notification settings - Fork 394
feat(aws): Move tags enablement environment variable to settings #1004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,34 @@ | ||
import json | ||
import unittest | ||
import os | ||
import sys | ||
import unittest | ||
from importlib import reload | ||
from time import time | ||
from unittest.mock import patch, MagicMock | ||
from unittest import mock | ||
from unittest.mock import MagicMock, patch | ||
|
||
from approvaltests.approvals import verify_as_json | ||
|
||
from caching.lambda_cache import LambdaTagsCache | ||
from enhanced_lambda_metrics import ( | ||
parse_metrics_from_report_log, | ||
parse_metrics_from_json_report_log, | ||
parse_lambda_tags_from_arn, | ||
generate_enhanced_lambda_metrics, | ||
create_out_of_memory_enhanced_metric, | ||
generate_enhanced_lambda_metrics, | ||
parse_lambda_tags_from_arn, | ||
parse_metrics_from_json_report_log, | ||
parse_metrics_from_report_log, | ||
) | ||
|
||
from caching.lambda_cache import LambdaTagsCache | ||
|
||
|
||
class TestEnhancedLambdaMetrics(unittest.TestCase): | ||
maxDiff = None | ||
malformed_report = "REPORT invalid report log line" | ||
standard_report = ( | ||
"REPORT RequestId: 8edab1f8-7d34-4a8e-a965-15ccbbb78d4c " | ||
"Duration: 0.62 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 51 MB" | ||
"REPORT RequestId: 8edab1f8-7d34-4a8e-a965-15ccbbb78d4c " | ||
"Duration: 0.62 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 51 MB" | ||
) | ||
cold_start_report = ( | ||
"REPORT RequestId: 8edab1f8-7d34-4a8e-a965-15ccbbb78d4c " | ||
"Duration: 0.81 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 90 MB Init Duration: 1234 ms" | ||
"REPORT RequestId: 8edab1f8-7d34-4a8e-a965-15ccbbb78d4c " | ||
"Duration: 0.81 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 90 MB Init Duration: 1234 ms" | ||
) | ||
report_with_xray = ( | ||
"REPORT RequestId: 814ba7cb-071e-4181-9a09-fa41db5bccad\tDuration: 1711.87 ms\t" | ||
|
@@ -303,7 +305,10 @@ def test_generate_enhanced_lambda_metrics_once_with_missing_arn(self): | |
|
||
@patch("caching.base_tags_cache.send_forwarder_internal_metrics") | ||
@patch("caching.lambda_cache.send_forwarder_internal_metrics") | ||
@patch.dict(os.environ, {"DD_FETCH_LAMBDA_TAGS": "true"}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can use something like
to have local env changes, I think it would also allow not to reload There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's used here for example but it doesn't prevent the use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, at least the local env change part still holds if we're sharing / re-setting the same env vars amongst tests |
||
def test_generate_enhanced_lambda_metrics_refresh_s3_cache(self, mock1, mock2): | ||
reload(sys.modules["settings"]) | ||
|
||
tags_cache = LambdaTagsCache("") | ||
tags_cache.get_cache_from_s3 = MagicMock(return_value=({}, 1000)) | ||
tags_cache.acquire_s3_cache_lock = MagicMock() | ||
|
@@ -334,13 +339,12 @@ def test_generate_enhanced_lambda_metrics_refresh_s3_cache(self, mock1, mock2): | |
"timestamp": 10000, | ||
} | ||
|
||
os.environ["DD_FETCH_LAMBDA_TAGS"] = "True" | ||
generate_enhanced_lambda_metrics(logs_input, tags_cache) | ||
tags_cache.get_cache_from_s3.assert_called_once() | ||
tags_cache.build_tags_cache.assert_called_once() | ||
tags_cache.write_cache_to_s3.assert_called_once() | ||
del os.environ["DD_FETCH_LAMBDA_TAGS"] | ||
|
||
@patch.dict(os.environ, {"DD_FETCH_LAMBDA_TAGS": "true"}) | ||
@patch("caching.lambda_cache.LambdaTagsCache.release_s3_cache_lock") | ||
@patch("caching.lambda_cache.LambdaTagsCache.acquire_s3_cache_lock") | ||
@patch("caching.lambda_cache.LambdaTagsCache.get_resources_paginator") | ||
|
@@ -358,6 +362,8 @@ def test_generate_enhanced_lambda_metrics_client_error( | |
mock_acquire_lock, | ||
mock_release_lock, | ||
): | ||
reload(sys.modules["settings"]) | ||
|
||
mock_acquire_lock.return_value = True | ||
mock_get_s3_cache.return_value = ( | ||
{}, | ||
|
@@ -391,22 +397,23 @@ def test_generate_enhanced_lambda_metrics_client_error( | |
"timestamp": 10000, | ||
} | ||
|
||
os.environ["DD_FETCH_LAMBDA_TAGS"] = "True" | ||
generate_enhanced_lambda_metrics(logs_input, tags_cache) | ||
mock_get_s3_cache.assert_called_once() | ||
mock_get_s3_cache.reset_mock() | ||
mock_get_resources_paginator.assert_called_once() | ||
paginator.paginate.assert_called_once() | ||
assert mock_base_tags_cache_forward_metrics.call_count == 1 | ||
assert mock_lambda_cache_forward_metrics.call_count == 2 | ||
del os.environ["DD_FETCH_LAMBDA_TAGS"] | ||
|
||
@patch("caching.lambda_cache.send_forwarder_internal_metrics") | ||
@patch("caching.base_tags_cache.send_forwarder_internal_metrics") | ||
@patch("caching.lambda_cache.LambdaTagsCache.get_cache_from_s3") | ||
@patch.dict(os.environ, {"DD_FETCH_LAMBDA_TAGS": "true"}) | ||
def test_generate_enhanced_lambda_metrics_timeout( | ||
self, mock_get_s3_cache, mock_forward_metrics, mock_base_forward_metrics | ||
): | ||
reload(sys.modules["settings"]) | ||
|
||
mock_get_s3_cache.return_value = ( | ||
{ | ||
"arn:aws:lambda:us-east-1:0:function:cloudwatch-event": [ | ||
|
@@ -437,17 +444,18 @@ def test_generate_enhanced_lambda_metrics_timeout( | |
"timestamp": 1591714946151, | ||
} | ||
|
||
os.environ["DD_FETCH_LAMBDA_TAGS"] = "True" | ||
generated_metrics = generate_enhanced_lambda_metrics(logs_input, tags_cache) | ||
verify_as_json(generated_metrics) | ||
del os.environ["DD_FETCH_LAMBDA_TAGS"] | ||
|
||
@patch("caching.lambda_cache.send_forwarder_internal_metrics") | ||
@patch("telemetry.send_forwarder_internal_metrics") | ||
@patch("caching.lambda_cache.LambdaTagsCache.get_cache_from_s3") | ||
@patch.dict(os.environ, {"DD_FETCH_LAMBDA_TAGS": "true"}) | ||
def test_generate_enhanced_lambda_metrics_out_of_memory( | ||
self, mock_get_s3_cache, mock_forward_metrics, mock_base_forward_metrics | ||
): | ||
reload(sys.modules["settings"]) | ||
|
||
mock_get_s3_cache.return_value = ( | ||
{ | ||
"arn:aws:lambda:us-east-1:0:function:cloudwatch-event": [ | ||
|
@@ -478,10 +486,8 @@ def test_generate_enhanced_lambda_metrics_out_of_memory( | |
"timestamp": 1591714946151, | ||
} | ||
|
||
os.environ["DD_FETCH_LAMBDA_TAGS"] = "True" | ||
generated_metrics = generate_enhanced_lambda_metrics(logs_input, tags_cache) | ||
verify_as_json(generated_metrics) | ||
del os.environ["DD_FETCH_LAMBDA_TAGS"] | ||
|
||
|
||
if __name__ == "__main__": | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imo, we could just import the setting directly in the cache function an use it. not a strict opinion, but it will be consistent with the other settings consts that we import.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was my first attempt, but with mocking involved, I needed to reload
settings
, thenlambda_cache
(for example), butlambda_cache
was already patched with mocks and tests were deeply broken.Using a getter allows me to only reload settings, and the rest of components will dynamically benefit from it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I added this comment regarding consistency in settings vars imports. Otherwise, it's not really changing anything.
Regarding the patching, I think it's a workaround here to do getters. However, I think we could use a code refactor for reworking patching.
We can check that later