Skip to content

Commit

Permalink
fine-tuned logging
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismana committed Jul 12, 2024
1 parent 33af85f commit 25c281c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
8 changes: 1 addition & 7 deletions source/image-handler/src/image-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion source/image-handler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
Expand Down
24 changes: 23 additions & 1 deletion source/image-handler/src/lib/LogstashFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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;
}
Expand Down
19 changes: 0 additions & 19 deletions source/image-handler/terraform/.terraform.lock.hcl

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

2 changes: 1 addition & 1 deletion source/image-handler/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 25c281c

Please sign in to comment.