Skip to content

Commit

Permalink
capture request info in pre-request hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankcorn committed Apr 3, 2024
1 parent ea44c4d commit 00d720e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

* AWS SDK Instrumentation - capture request info in pre-request hook

## [0.5.5] - 2024-03-28

* Fix: don't swallow lambda errors
Expand Down
6 changes: 3 additions & 3 deletions meta.wrapper.json
Original file line number Diff line number Diff line change
Expand Up @@ -3586,7 +3586,7 @@
"format": "cjs"
},
"src/lambda-wrapper.ts": {
"bytes": 753,
"bytes": 1357,
"imports": [
{
"path": "node_modules/@opentelemetry/instrumentation-aws-sdk/build/src/index.js",
Expand Down Expand Up @@ -4654,10 +4654,10 @@
"bytesInOutput": 262747
},
"src/lambda-wrapper.ts": {
"bytesInOutput": 430
"bytesInOutput": 823
}
},
"bytes": 487187
"bytes": 487580
}
}
}
28 changes: 21 additions & 7 deletions src/lambda-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,30 @@ import { flatten } from "flat";

import { BaselimeSDK, BetterHttpInstrumentation } from "@baselime/node-opentelemetry";

const blockedRequestOperations = [
{ service: 'S3', operation: 'PutObject' },
{ service: 'Kinesis', operation: 'PutRecord' }
]

const blockedResponseOperations = [
{ service: 'S3', operation: 'GetObject' },
]

const instrumentations = [
new AwsInstrumentation({
suppressInternalInstrumentation: process.env.AWS_SDK_INTERNALS === 'true' ? false : true,
responseHook: (span, { response }) => {
if (response) {
const awsApiReqData = {
request: response.request,
responseHook(span, { response }) {
if (response && !blockedResponseOperations.some(({ service, operation }) => response.request.serviceName === service && response.request.commandName === operation)) {
span.setAttributes(flatten({
response: response.data,
};
span.setAttributes(flatten(awsApiReqData));
}))
}
},
preRequestHook(span, request) {
if (!blockedRequestOperations.some(({ service, operation }) => request.request.serviceName === service && request.request.commandName === operation)) {
span.setAttributes(flatten({
request: request.request,
}))
}
}
}),
Expand All @@ -23,4 +37,4 @@ const instrumentations = [
})
]

new BaselimeSDK({ instrumentations }).start();
new BaselimeSDK({ instrumentations, service: process.env.AWS_LAMBDA_FUNCTION_NAME }).start();

0 comments on commit 00d720e

Please sign in to comment.