Skip to content

Commit

Permalink
Improve logging of API call Lambdas (#145)
Browse files Browse the repository at this point in the history
- Log in JSON format
- Log errors at the error level
- Log exceptions in exception handlers
  • Loading branch information
diogocp authored Sep 19, 2024
1 parent f4eec95 commit ed7b680
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 7 additions & 5 deletions aws_quickstart/datadog_agentless_scanning.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,9 @@ Resources:
Description: "A function to call the Datadog Agentless API."
Role: !GetAtt LambdaExecutionRoleDatadogAgentlessAPICall.Arn
Handler: "index.handler"
LoggingConfig:
ApplicationLogLevel: "INFO"
LogFormat: "JSON"
Runtime: "python3.11"
Timeout: 30
Code:
Expand All @@ -977,7 +980,6 @@ Resources:
import urllib.parse
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)
API_CALL_SOURCE_HEADER_VALUE = "cfn-agentless-quick-start"
Expand Down Expand Up @@ -1043,7 +1045,7 @@ Resources:
"Message": "Datadog AWS Agentless Scanning Integration created successfully.",
})
else:
LOGGER.info('Failed - exception thrown during processing.')
LOGGER.error('Failed - unexpected status code: %d', response.getcode())
send_response(event, context, "FAILED", {
"Message": "Http response: {}".format(response.msg)})
Expand All @@ -1061,16 +1063,16 @@ Resources:
"Message": "Datadog AWS Agentless Scanning Integration deleted successfully.",
})
else:
LOGGER.info('Failed - exception thrown during processing.')
LOGGER.error('Failed - unexpected status code: %d', response.getcode())
send_response(event, context, "FAILED", {
"Message": "Http response: {}".format(response.msg)})
else:
LOGGER.info('Failed - received unexpected request.')
LOGGER.error('Failed - received unexpected request: %s', event['RequestType'])
send_response(event, context, "FAILED",
{"Message": "Unexpected event received from CloudFormation"})
except Exception as e: # pylint: disable=W0702
LOGGER.info('Failed - exception thrown during processing.')
LOGGER.exception('Failed - exception thrown during processing.')
send_response(event, context, "FAILED", {
"Message": "Exception during processing: {}".format(e)})
Expand Down
12 changes: 7 additions & 5 deletions aws_quickstart/datadog_integration_api_call_v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ Resources:
Description: "A function to call the Datadog API."
Role: !GetAtt LambdaExecutionRoleDatadogAPICall.Arn
Handler: "index.handler"
LoggingConfig:
ApplicationLogLevel: "INFO"
LogFormat: "JSON"
Runtime: "python3.11"
Timeout: 30
Code:
Expand All @@ -98,7 +101,6 @@ Resources:
import urllib.parse
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)
API_CALL_SOURCE_HEADER_VALUE = "cfn-quick-start"
Expand Down Expand Up @@ -155,7 +157,7 @@ Resources:
"ExternalId": json_response["external_id"],
})
else:
LOGGER.info('Failed - exception thrown during processing.')
LOGGER.error('Failed - unexpected status code: %d', response.getcode())
send_response(event, context, "FAILED", {
"Message": "Http response: {}".format(response.msg)})
Expand All @@ -173,16 +175,16 @@ Resources:
"Message": "Datadog AWS Integration deleted successfully.",
})
else:
LOGGER.info('Failed - exception thrown during processing.')
LOGGER.error('Failed - unexpected status code: %d', response.getcode())
send_response(event, context, "FAILED", {
"Message": "Http response: {}".format(response.msg)})
else:
LOGGER.info('Failed - received unexpected request.')
LOGGER.error('Failed - received unexpected request: %s', event['RequestType'])
send_response(event, context, "FAILED",
{"Message": "Unexpected event received from CloudFormation"})
except Exception as e: # pylint: disable=W0702
LOGGER.info('Failed - exception thrown during processing.')
LOGGER.exception('Failed - exception thrown during processing.')
send_response(event, context, "FAILED", {
"Message": "Exception during processing: {}".format(e)})
Expand Down

0 comments on commit ed7b680

Please sign in to comment.