Skip to content

Commit

Permalink
Make handler always use the callback
Browse files Browse the repository at this point in the history
  • Loading branch information
RafalSumislawski committed Jun 6, 2024
1 parent 0fd5419 commit 5ff365e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ __pycache__/*
build.toml

*.zip
index.d.ts
index.d.ts.map
index.js
index.js.map
tsconfig.tsbuildinfo
2 changes: 1 addition & 1 deletion nodejs/packages/cx-wrapper/aws/aws-user-function.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import {Handler} from "aws-lambda/handler";

export function load(taskRoot?: string, originalHandler?: string): Handler;
export function load(taskRoot?: string, originalHandler?: string): Promise<Handler>;
34 changes: 26 additions & 8 deletions nodejs/packages/cx-wrapper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Callback,
Context,
} from 'aws-lambda';

import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { AwsLambdaInstrumentation } from '@opentelemetry/instrumentation-aws-lambda';
import {
context as otelContext,
Expand All @@ -14,7 +14,6 @@ import {
propagation,
trace,
} from '@opentelemetry/api';

import { AwsLambdaInstrumentationConfig } from '@opentelemetry/instrumentation-aws-lambda';
import {Handler} from "aws-lambda/handler";

Expand Down Expand Up @@ -100,17 +99,36 @@ const lambdaAutoInstrumentConfig: AwsLambdaInstrumentationConfig = {

const instrumentation = new AwsLambdaInstrumentation(lambdaAutoInstrumentConfig)

registerInstrumentations({instrumentations: [instrumentation]})

if (process.env.CX_ORIGINAL_HANDLER === undefined)
throw Error('CX_ORIGINAL_HANDLER is missing');

export const handler = async (event: any, context: Context, callback: Callback) => {
console.log(`Running custom CX handler and redirecting to ${process.env.CX_ORIGINAL_HANDLER}`)

const originalHandler = await load(

export const handler = (event: any, context: Context, callback: Callback) => {
// console.log(`Running custom CX handler and redirecting to ${process.env.CX_ORIGINAL_HANDLER}`)
load(
process.env.LAMBDA_TASK_ROOT,
process.env.CX_ORIGINAL_HANDLER
).then(
originalHandler => {
const patchedHandler = instrumentation.getPatchHandler(originalHandler) as any as Handler;
const maybePromise = patchedHandler(event, context, callback);
// console.log("patchedHandler completed")
if (typeof maybePromise?.then === 'function') {
maybePromise.then(
value => {
callback(null, value)
},
(err: Error | string) => {
callback(err, null)
}
);
}
},
(err: Error | string) => {
callback(err, null)
}
);

const patchedHandler = instrumentation.getPatchHandler(originalHandler) as any as Handler;
patchedHandler(event, context, callback)
}
4 changes: 2 additions & 2 deletions nodejs/packages/cx-wrapper/package-lock.json

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

4 changes: 2 additions & 2 deletions nodejs/packages/layer/package-lock.json

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

2 changes: 1 addition & 1 deletion nodejs/packages/layer/scripts/otel-handler
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ if [[ $OTEL_RESOURCE_ATTRIBUTES != *"service.name="* ]]; then
fi

export CX_ORIGINAL_HANDLER="${_HANDLER}"
export _HANDLER="${MAGIC_HANDLER}"
export _HANDLER="cx-wrapper.handler"

exec "$@"

0 comments on commit 5ff365e

Please sign in to comment.