Skip to content

Commit

Permalink
Add DC logging
Browse files Browse the repository at this point in the history
  • Loading branch information
symroe committed Nov 16, 2023
1 parent 9de3d62 commit fb13cfe
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ djangorestframework = "*"
certifi = ">2022.9.24"
django-cors-headers = "*"
typing-extensions = ">=3.7.4"
dc-logging-utils = {file = "https://github.com/DemocracyClub/dc_logging/archive/refs/tags/1.0.0.tar.gz"}

[dev-packages]
pre-commit = "*"
Expand All @@ -50,7 +51,6 @@ python_version = "3.10"
allow_prereleases = true

[scripts]

collectstatic = "python manage.py collectstatic -c --noinput"
pytest = "pytest"
coveralls = "coveralls"
Expand Down
63 changes: 37 additions & 26 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions api_endpoints/v1_postcode_lookup/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os

from dc_logging_client import DCWidePostcodeLoggingClient
from mangum import Mangum
from middleware import MIDDLEWARE
from starlette.applications import Starlette
Expand All @@ -13,9 +14,24 @@

init_sentry()

if logger_arn := os.environ.get("LOGGER_ARN"):
POSTCODE_LOGGER = DCWidePostcodeLoggingClient(function_arn=logger_arn)
else:
POSTCODE_LOGGER = DCWidePostcodeLoggingClient(
function_arn="fake", fake=True
)


def get_postcode_response(request: Request):
postcode = request.path_params["postcode"]

# Log this request
POSTCODE_LOGGER.entry_class(
postcode=postcode,
dc_product=POSTCODE_LOGGER.dc_product.ec_api,
calls_devs_dc_api=True,
)

try:
response = client.get_postcode_response(request, postcode)
except DevsDCException as error:
Expand Down
6 changes: 6 additions & 0 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ Parameters:
Description: "The domain the app is on."
Type: String

AppLoggerArn:
Default: AppLoggerArn
Description: "The ARN of the Lambda function to log to"
Type: AWS::SSM::Parameter::Value<String>

Resources:
DependenciesLayer:
Type: AWS::Serverless::LayerVersion
Expand Down Expand Up @@ -110,6 +115,7 @@ Resources:
DC_API_TOKEN: !Ref AppDCAPIToken
SAM_LAMBDA_CONFIG_ENV: !Ref AppSamLambdaConfigEnv
APP_DOMAIN: !Ref AppDomain
LOGGER_ARN: !Ref AppLoggerArn
Events:
HTTPRequests:
Type: Api
Expand Down

0 comments on commit fb13cfe

Please sign in to comment.