Skip to content

Commit

Permalink
fix: getting logging information from lambda (#6477)
Browse files Browse the repository at this point in the history
* fix: getting logging information from lambda

* update formatting

* add unit tests

* add integration tests

* bump python versions that is used in the test templates
  • Loading branch information
mndeveci authored Jan 2, 2024
1 parent e3a3972 commit 39fe8cd
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 11 deletions.
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
chevron~=0.12
click~=8.1
Flask<3.1
boto3>=1.26.109,<2
boto3>=1.29.2,<2
jmespath~=1.0.1
ruamel_yaml~=0.18.5
PyYAML~=6.0,>=6.0.1
Expand Down
4 changes: 3 additions & 1 deletion samcli/lib/observability/cw_logs/cw_log_group_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def for_lambda_function(boto_client_provider: BotoProviderType, function_name: s
"""
log_group_name = ""
try:
function_configuration = boto_client_provider("lambda").get_function_configuration(function_name)
function_configuration = boto_client_provider("lambda").get_function_configuration(
FunctionName=function_name
)
logging_config = function_configuration.get("LoggingConfig")
if logging_config:
log_group_name = logging_config.get("LogGroup")
Expand Down
1 change: 1 addition & 0 deletions tests/integration/logs/test_logs_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def _check_logs(self, cmd_list: List, log_strings: List[str], output: str = "tex
REGULAR_STACK_FUNCTION_LIST = [
"ApiGwFunction",
"SfnFunction",
"FunctionWithCustomLoggingConfig",
]
REGULAR_STACK_APIGW_LIST = [
"HelloWorldServerlessApi",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def handler(event, context):
print("Hello world from ChildStack/FunctionWithCustomLoggingConfig function")
print("this should be filtered ChildStackFunctionWithCustomLoggingConfig")
return {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def handler(event, context):
print("Hello world from ChildStack/GrandChildStack/FunctionWithCustomLoggingConfig function")
print("this should be filtered ChildStackGrandChildStackFunctionWithCustomLoggingConfig")
return {}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Resources:
Properties:
CodeUri: apigw-function/
Handler: app.handler
Runtime: python3.9
Runtime: python3.12
Tracing: Active
Events:
HelloWorld:
Expand All @@ -81,7 +81,7 @@ Resources:
Properties:
CodeUri: sfn-function/
Handler: app.handler
Runtime: python3.9
Runtime: python3.12
Tracing: Active

HelloWorldServerlessApi:
Expand Down Expand Up @@ -113,6 +113,17 @@ Resources:
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"

FunctionWithCustomLoggingConfig:
Type: AWS::Serverless::Function
Properties:
CodeUri: function-with-custom-logging/
Handler: app.handler
Runtime: python3.12
Tracing: Active
LoggingConfig:
LogFormat: JSON
LogGroup: !Sub /aws/lambda/${AWS::StackName}

Outputs:
HelloWorldServerlessApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Resources:
Properties:
CodeUri: apigw-function/
Handler: app.handler
Runtime: python3.9
Runtime: python3.12
Tracing: Active
Events:
HelloWorld:
Expand All @@ -81,7 +81,7 @@ Resources:
Properties:
CodeUri: sfn-function/
Handler: app.handler
Runtime: python3.9
Runtime: python3.12
Tracing: Active

HelloWorldServerlessApi:
Expand Down Expand Up @@ -113,6 +113,17 @@ Resources:
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"

FunctionWithCustomLoggingConfig:
Type: AWS::Serverless::Function
Properties:
CodeUri: function-with-custom-logging/
Handler: app.handler
Runtime: python3.12
Tracing: Active
LoggingConfig:
LogFormat: JSON
LogGroup: !Sub /aws/lambda/${AWS::StackName}

GrandChildStack:
Type: AWS::Serverless::Application
Properties:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def handler(event, context):
print("Hello world from FunctionWithCustomLoggingConfig function")
print("this should be filtered FunctionWithCustomLoggingConfig")
return {}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Resources:
Properties:
CodeUri: apigw-function/
Handler: app.handler
Runtime: python3.9
Runtime: python3.12
Tracing: Active
Events:
HelloWorld:
Expand All @@ -81,7 +81,7 @@ Resources:
Properties:
CodeUri: sfn-function/
Handler: app.handler
Runtime: python3.9
Runtime: python3.12
Tracing: Active

HelloWorldServerlessApi:
Expand Down Expand Up @@ -113,6 +113,17 @@ Resources:
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"

FunctionWithCustomLoggingConfig:
Type: AWS::Serverless::Function
Properties:
CodeUri: function-with-custom-logging/
Handler: app.handler
Runtime: python3.12
Tracing: Active
LoggingConfig:
LogFormat: JSON
LogGroup: !Sub /aws/lambda/${AWS::StackName}

ChildStack:
Type: AWS::Serverless::Application
Properties:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def handler(event, context):
print("Hello world from FunctionWithCustomLoggingConfig function")
print("this should be filtered FunctionWithCustomLoggingConfig")
return {}
15 changes: 13 additions & 2 deletions tests/integration/testdata/logs/python-apigw-sfn/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Resources:
Properties:
CodeUri: apigw-function/
Handler: app.handler
Runtime: python3.9
Runtime: python3.12
Tracing: Active
Events:
HelloWorld:
Expand All @@ -81,7 +81,7 @@ Resources:
Properties:
CodeUri: sfn-function/
Handler: app.handler
Runtime: python3.9
Runtime: python3.12
Tracing: Active

HelloWorldServerlessApi:
Expand Down Expand Up @@ -113,6 +113,17 @@ Resources:
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"

FunctionWithCustomLoggingConfig:
Type: AWS::Serverless::Function
Properties:
CodeUri: function-with-custom-logging/
Handler: app.handler
Runtime: python3.12
Tracing: Active
LoggingConfig:
LogFormat: JSON
LogGroup: !Sub /aws/lambda/${AWS::StackName}

Outputs:
HelloWorldServerlessApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from unittest import TestCase
from unittest.mock import Mock, ANY
from unittest.mock import Mock, ANY, call

from parameterized import parameterized

Expand All @@ -26,6 +26,9 @@ def test_must_return_custom_log_group_name(self):
result = LogGroupProvider.for_lambda_function(given_client_provider, "my_function_name")

self.assertEqual(expected, result)
given_client_provider.assert_has_calls(
[call("lambda").get_function_configuration(FunctionName="my_function_name")]
)

def test_must_return_default_log_group_name_with_exception_raised(self):
expected = "/aws/lambda/my_function_name"
Expand Down

0 comments on commit 39fe8cd

Please sign in to comment.