From 25c281ce4e5222cc8c8e789716397af26985a529 Mon Sep 17 00:00:00 2001 From: Matthias Naber Date: Fri, 12 Jul 2024 13:51:08 +0200 Subject: [PATCH] fine-tuned logging --- source/image-handler/src/image-request.ts | 8 +------ source/image-handler/src/index.ts | 2 +- .../src/lib/LogstashFormatter.ts | 24 ++++++++++++++++++- .../terraform/.terraform.lock.hcl | 19 --------------- source/image-handler/terraform/main.tf | 2 +- 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/source/image-handler/src/image-request.ts b/source/image-handler/src/image-request.ts index 66d800e62..5e73e763e 100644 --- a/source/image-handler/src/image-request.ts +++ b/source/image-handler/src/image-request.ts @@ -15,13 +15,7 @@ import { import { ThumborMapper } from './thumbor-mapper'; import { GetObjectCommand, GetObjectCommandInput, GetObjectCommandOutput, S3 } from '@aws-sdk/client-s3'; import { APIGatewayProxyEventV2 } from 'aws-lambda'; -import { Logger } from '@aws-lambda-powertools/logger'; -import { LogStashFormatter } from './lib/LogstashFormatter'; - -const logger = new Logger({ - serviceName: process.env.AWS_LAMBDA_FUNCTION_NAME ?? '', - logFormatter: new LogStashFormatter(), -}); +import { logger } from './index'; type OriginalImageInfo = Partial<{ contentType: string; diff --git a/source/image-handler/src/index.ts b/source/image-handler/src/index.ts index c530bc4ce..9b1ea2095 100755 --- a/source/image-handler/src/index.ts +++ b/source/image-handler/src/index.ts @@ -11,7 +11,7 @@ import { LogStashFormatter } from './lib/LogstashFormatter'; const s3Client = new S3(); -const logger = new Logger({ +export const logger = new Logger({ serviceName: process.env.AWS_LAMBDA_FUNCTION_NAME ?? '', logFormatter: new LogStashFormatter(), }); diff --git a/source/image-handler/src/lib/LogstashFormatter.ts b/source/image-handler/src/lib/LogstashFormatter.ts index cbfa80760..ab5f09f7d 100644 --- a/source/image-handler/src/lib/LogstashFormatter.ts +++ b/source/image-handler/src/lib/LogstashFormatter.ts @@ -5,6 +5,9 @@ type LogStashLog = LogAttributes & { '@timestamp': string; }; +const allowed_keys = ['rawPath', 'headers', 'http']; +const allowed_headers = ['accept', 'x-amz-cf-id', 'user-agent', 'host', 'origin', 'x-amzn-trace-id']; + class LogStashFormatter extends LogFormatter { public formatAttributes(attributes: UnformattedAttributes, additionalLogAttributes: LogAttributes): LogItem { const baseAttributes: LogStashLog = { @@ -24,7 +27,26 @@ class LogStashFormatter extends LogFormatter { }, }; const logItem = new LogItem({ attributes: baseAttributes }); - logItem.addAttributes(additionalLogAttributes); // add any attributes not explicitly defined + if (additionalLogAttributes) { + if (additionalLogAttributes.hasOwnProperty('imageRequestInfo')) { + let additionalLogAttribute = additionalLogAttributes['imageRequestInfo']; + additionalLogAttribute['originalImage'] = undefined; + } + if (additionalLogAttributes.hasOwnProperty('headers')) { + let headers = additionalLogAttributes['headers']; + additionalLogAttributes['headers'] = allowed_headers.reduce((acc, key) => { + acc[key] = headers[key]; + return acc; + }, {}); + } + + additionalLogAttributes = allowed_keys.reduce((acc, key) => { + acc[key] = additionalLogAttributes[key]; + return acc; + }, {}); + + logItem.addAttributes(additionalLogAttributes); // add any attributes not explicitly defined + } return logItem; } diff --git a/source/image-handler/terraform/.terraform.lock.hcl b/source/image-handler/terraform/.terraform.lock.hcl index 1bc0d468b..184073f86 100644 --- a/source/image-handler/terraform/.terraform.lock.hcl +++ b/source/image-handler/terraform/.terraform.lock.hcl @@ -26,25 +26,6 @@ provider "registry.terraform.io/hashicorp/aws" { ] } -provider "registry.terraform.io/hashicorp/http" { - version = "3.4.3" - hashes = [ - "h1:Ep4kCumou6eEyPkFJFAfuzd7IAsYM7xMAdDaFTwdDZ8=", - "zh:001e12b8079955a9fa7f8fcd515ae665b2e1087107fd337c4b872e88a86d540b", - "zh:0874fb3f870b2ac24c967a9685f2da641079589024109340389694696301a85b", - "zh:3b5e533c3d2859575945568aad0aac66b71bfc709706231fc2de94e01ca76d7f", - "zh:622ee28d42ed9d4b1399dde377db515e62cac08bd65bb2455068621f7a42d90d", - "zh:6dea688d78840a3f678e06ee602d37c766ce2ee625dcdce0c6658116ebcbde8e", - "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", - "zh:7f57a1436a464bc2e1698457b402ff0fd98ef9e7dcf6707d6bd0debc67fad164", - "zh:829d89d82e6fc3c89714950dc8afa51d622bb8e4f4bd5c73037505fb55a67834", - "zh:e453202d09b62531ed3278926307d315276e05784e7c6448a2c21c6a2da6e48f", - "zh:e76edc035240b4ad9334b4a0282b44a086e001df3007a2fc51f6262c4db032d1", - "zh:eeb0379da9093e155a193f666079de6baf8ed02855bf2a443448903f7cfef378", - "zh:fcb00eeb665ccae383645173d8e10c3071946396629a7797db39c798997f21b0", - ] -} - provider "registry.terraform.io/hashicorp/null" { version = "3.2.2" hashes = [ diff --git a/source/image-handler/terraform/main.tf b/source/image-handler/terraform/main.tf index 9b14b6003..9f8536e09 100644 --- a/source/image-handler/terraform/main.tf +++ b/source/image-handler/terraform/main.tf @@ -125,7 +125,7 @@ resource "aws_lambda_layer_version" "sharp" { s3_key = aws_s3_bucket_object.sharp.key s3_object_version = aws_s3_bucket_object.sharp.version_id skip_destroy = true - source_code_hash = aws_s3_bucket_object.sharp.etag + source_code_hash = filebase64sha256("${path.module}/lambda-layer.zip") compatible_runtimes = ["nodejs16.x", "nodejs18.x", "nodejs20.x"] compatible_architectures = ["arm64"]