From fb13cfee4bbd7d7e6355e7700f2b217c6df3b16b Mon Sep 17 00:00:00 2001 From: Sym Roe Date: Tue, 14 Nov 2023 18:15:20 +0000 Subject: [PATCH] Add DC logging --- Pipfile | 2 +- Pipfile.lock | 63 +++++++++++++++---------- api_endpoints/v1_postcode_lookup/app.py | 16 +++++++ template.yaml | 6 +++ 4 files changed, 60 insertions(+), 27 deletions(-) diff --git a/Pipfile b/Pipfile index 1bd335c..7c48f9e 100644 --- a/Pipfile +++ b/Pipfile @@ -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 = "*" @@ -50,7 +51,6 @@ python_version = "3.10" allow_prereleases = true [scripts] - collectstatic = "python manage.py collectstatic -c --noinput" pytest = "pytest" coveralls = "coveralls" diff --git a/Pipfile.lock b/Pipfile.lock index e82439d..c4cb43d 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "e60827fbd05293d39cd361c1155ca5f036bc1391e75c43367cf37fc59140e7fa" + "sha256": "ae52fe421dcb791df6e5a9be0a19572fc86fb2475272f51409427e76968c4142" }, "pipfile-spec": 6, "requires": { @@ -30,19 +30,27 @@ }, "boto3": { "hashes": [ - "sha256:98b01bbea27740720a06f7c7bc0132ae4ce902e640aab090cfb99ad3278449c3", - "sha256:adfb915958d7b54d876891ea1599dd83189e35a2442eb41ca52b04ea716180b6" + "sha256:192695305fa65012d21f78ee852b91cb56dd571e84d51fb71f756302bf19d23f", + "sha256:20285ebf4e98b2905a88aeb162b4f77ff908b2e3e31038b3223e593789290aa3" ], "markers": "python_version >= '3.7'", - "version": "==1.28.84" + "version": "==1.29.1" }, "botocore": { "hashes": [ - "sha256:8913bedb96ad0427660dee083aeaa675466eb662bbf1a47781956b5882aadcc5", - "sha256:d65bc05793d1a8a8c191a739f742876b4b403c5c713dc76beef262d18f7984a2" + "sha256:1d9c0ff3eb7828a8bd8c5c7f12cd9d8c05c6fe4c616ef963fdaab538a0da3809", + "sha256:fcf3cc2913afba8e5f7ebcc15e8f6bfae844ab64bf983bf5a6fe3bb54cce239d" ], "markers": "python_version >= '3.7'", - "version": "==1.31.84" + "version": "==1.32.1" + }, + "cachetools": { + "hashes": [ + "sha256:086ee420196f7b2ab9ca2db2520aca326318b68fe5ba8bc4d49cca91add450f2", + "sha256:861f35a13a451f94e301ce2bec7cac63e881232ccce7ed67fab9b5df4d3beaa1" + ], + "markers": "python_version >= '3.7'", + "version": "==5.3.2" }, "certifi": { "hashes": [ @@ -211,6 +219,9 @@ "git": "https://github.com/DemocracyClub/design-system.git", "ref": "09bcc3eb4eccc189b3a19d91dae5b777be52f253" }, + "dc-logging-utils": { + "file": "https://github.com/DemocracyClub/dc_logging/archive/refs/tags/1.0.0.tar.gz" + }, "django": { "hashes": [ "sha256:8e0f1c2c2786b5c0e39fe1afce24c926040fad47c8ea8ad30aaf1188df29fc41", @@ -232,12 +243,12 @@ }, "django-cors-headers": { "hashes": [ - "sha256:25aabc94d4837678c1edf442c7f68a5f5fd151f6767b0e0b01c61a2179d02711", - "sha256:bd36c7aea0d070e462f3383f0dc9ef717e5fdc2b10a99c98c285f16da84ffba2" + "sha256:0b1fd19297e37417fc9f835d39e45c8c642938ddba1acce0c1753d3edef04f36", + "sha256:0bf65ef45e606aff1994d35503e6b677c0b26cafff6506f8fd7187f3be840207" ], "index": "pypi", "markers": "python_version >= '3.8'", - "version": "==4.3.0" + "version": "==4.3.1" }, "django-debug-toolbar": { "hashes": [ @@ -285,12 +296,12 @@ }, "faker": { "hashes": [ - "sha256:171b27ba106cf69e30a91ac471407c2362bd6af27738e2461dc441aeff5eed91", - "sha256:df44b68b9d231e784f4bfe616d781576cfef9f0c5d9a17671bf84dc10d7b44d6" + "sha256:88316cfa7c8be892433bb10b2f1c2a7ce97246e18712680547e2fb1c4bd03912", + "sha256:f9af61c9223e1a3fd01ee2a48265352432f40a4fb21feb274d9d1d97b4943d75" ], "index": "pypi", "markers": "python_version >= '3.8'", - "version": "==20.0.0" + "version": "==20.0.3" }, "idna": { "hashes": [ @@ -525,7 +536,7 @@ "sha256:c97dfde1f7bd43a71c8d2a58e369e9b2bf692d1334ea9f9cae55add7d0dd0f84", "sha256:fdb6d215c776278489906c2f8916e6e7d4f5a9b602ccbcfdf7f016fc8da0596e" ], - "markers": "python_version >= '3.6'", + "markers": "python_version >= '3.7'", "version": "==2.0.7" }, "whitenoise": { @@ -733,7 +744,7 @@ "sha256:637996211036b6385ef91435e4fae22989472f9d571faba8927ba8253acbc330", "sha256:b8c3f85900b9dc423225913c5aace94729fe1fa9763b38939a95226f02d37186" ], - "markers": "python_version < '3.11' and python_version >= '3.7'", + "markers": "python_version >= '3.5'", "version": "==5.1.1" }, "distlib": { @@ -776,12 +787,12 @@ }, "faker": { "hashes": [ - "sha256:171b27ba106cf69e30a91ac471407c2362bd6af27738e2461dc441aeff5eed91", - "sha256:df44b68b9d231e784f4bfe616d781576cfef9f0c5d9a17671bf84dc10d7b44d6" + "sha256:88316cfa7c8be892433bb10b2f1c2a7ce97246e18712680547e2fb1c4bd03912", + "sha256:f9af61c9223e1a3fd01ee2a48265352432f40a4fb21feb274d9d1d97b4943d75" ], "index": "pypi", "markers": "python_version >= '3.8'", - "version": "==20.0.0" + "version": "==20.0.3" }, "filelock": { "hashes": [ @@ -908,11 +919,11 @@ }, "prompt-toolkit": { "hashes": [ - "sha256:99ba3dfb23d5b5af89712f89e60a5f3d9b8b67a9482ca377c5771d0e9047a34b", - "sha256:a371c06bb1d66cd499fecd708e50c0b6ae00acba9822ba33c586e2f16d1b739e" + "sha256:941367d97fc815548822aa26c2a269fdc4eb21e9ec05fc5d447cf09bad5d75f0", + "sha256:f36fe301fafb7470e86aaf90f036eef600a3210be4decf461a5b1ca8403d3cb2" ], "markers": "python_full_version >= '3.7.0'", - "version": "==3.0.40" + "version": "==3.0.41" }, "ptyprocess": { "hashes": [ @@ -1124,7 +1135,7 @@ "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc", "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f" ], - "markers": "python_version < '3.11' and python_version >= '3.7'", + "markers": "python_version < '3.11'", "version": "==2.0.1" }, "traitlets": { @@ -1140,7 +1151,7 @@ "sha256:c97dfde1f7bd43a71c8d2a58e369e9b2bf692d1334ea9f9cae55add7d0dd0f84", "sha256:fdb6d215c776278489906c2f8916e6e7d4f5a9b602ccbcfdf7f016fc8da0596e" ], - "markers": "python_version >= '3.6'", + "markers": "python_version >= '3.7'", "version": "==2.0.7" }, "virtualenv": { @@ -1153,10 +1164,10 @@ }, "wcwidth": { "hashes": [ - "sha256:9a929bd8380f6cd9571a968a9c8f4353ca58d7cd812a4822bba831f8d685b223", - "sha256:a675d1a4a2d24ef67096a04b85b02deeecd8e226f57b5e3a72dbb9ed99d27da8" + "sha256:390c7454101092a6a5e43baad8f83de615463af459201709556b6e4b1c861f97", + "sha256:aec5179002dd0f0d40c456026e74a729661c9d468e1ed64405e3a6c2176ca36f" ], - "version": "==0.2.9" + "version": "==0.2.10" }, "wheel": { "hashes": [ diff --git a/api_endpoints/v1_postcode_lookup/app.py b/api_endpoints/v1_postcode_lookup/app.py index 7dd5540..7d12ec0 100644 --- a/api_endpoints/v1_postcode_lookup/app.py +++ b/api_endpoints/v1_postcode_lookup/app.py @@ -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 @@ -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: diff --git a/template.yaml b/template.yaml index 005967e..c31d3e1 100644 --- a/template.yaml +++ b/template.yaml @@ -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 + Resources: DependenciesLayer: Type: AWS::Serverless::LayerVersion @@ -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