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 21, 2023
1 parent 5bcdb75 commit e55c566
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 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.2.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
15 changes: 13 additions & 2 deletions Pipfile.lock

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

14 changes: 14 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,22 @@

init_sentry()

if logger_arn := os.environ.get("LOGGER_ARN"):
POSTCODE_LOGGER = DCWidePostcodeLoggingClient(function_arn=logger_arn)
else:
POSTCODE_LOGGER = DCWidePostcodeLoggingClient(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
1 change: 1 addition & 0 deletions api_endpoints/v1_postcode_lookup/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ sentry-sdk[starlette]==1.19.1
starlette==0.26.1
mangum==0.17.0
requests==2.31.0
https://github.com/DemocracyClub/dc_logging/archive/refs/tags/1.0.2.tar.gz
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 e55c566

Please sign in to comment.