Skip to content

Commit

Permalink
feat(logging): log accept and request_id (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismana authored Jun 26, 2024
1 parent fdf557e commit fd8cb72
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
11 changes: 8 additions & 3 deletions source/image-handler/src/image-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export class ImageRequest {
CacheControl: any;
ETag: any;
headers: any;
private s3: any;
request_id: any;
accept: any;
private s3?: S3;

constructor(s3: S3) {
this.s3 = s3;
Expand Down Expand Up @@ -87,6 +89,9 @@ export class ImageRequest {
}
}

this.request_id = event.headers['X-Amz-Cf-Id'];
this.accept = event.headers.Accept || event.headers.accept;

delete this.s3;

return this;
Expand All @@ -101,7 +106,7 @@ export class ImageRequest {
async getOriginalImage(bucket: string, key: string): Promise<any> {
const imageLocation = { Bucket: bucket, Key: key };
try {
const originalImage: GetObjectCommandOutput = await this.s3.getObject(imageLocation);
const originalImage: GetObjectCommandOutput = await this.s3!.getObject(imageLocation);
const metaData = originalImage['Metadata'];
const isGone = metaData && metaData['buzz-status-code'] && metaData['buzz-status-code'] === '410';

Expand Down Expand Up @@ -310,7 +315,7 @@ export class ImageRequest {
getOutputFormat(event: APIGatewayProxyEventV2): keyof sharp.FormatEnum | null {
const autoWebP = process.env.AUTO_WEBP;
const autoAvif = process.env.AUTO_AVIF;
let accept = (event.headers?.Accept || event.headers?.accept) ?? '';
let accept = event.headers?.Accept || event.headers?.accept;
if (autoAvif === 'Yes' && accept && accept.includes('image/avif')) {
return 'avif';
} else if (autoWebP === 'Yes' && accept && accept.includes('image/webp')) {
Expand Down
3 changes: 2 additions & 1 deletion source/image-handler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export async function handler(event: APIGatewayProxyEventV2): Promise<APIGateway

try {
const request: ImageRequest = await imageRequest.setup(event);
logger.info('Image manipulation request', { ...request, originalImage: undefined });
logger.appendKeys({ ...request, originalImage: undefined });
logger.info('Image manipulation request');

let now = Date.now();
if (request.Expires && request.Expires.getTime() < now) {
Expand Down
4 changes: 0 additions & 4 deletions source/image-handler/src/lib/logging/LogStashFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ class LogStashFormatter extends LogFormatter {
service: attributes.serviceName,
environment: attributes.environment,
awsRegion: attributes.awsRegion,
correlationIds: {
awsRequestId: attributes.lambdaContext?.awsRequestId,
xRayTraceId: attributes.xRayTraceId,
},
lambdaFunction: {
name: attributes.lambdaContext?.functionName,
arn: attributes.lambdaContext?.invokedFunctionArn,
Expand Down

0 comments on commit fd8cb72

Please sign in to comment.