Skip to content

Add handlerWrapper configuration option to AWS Lambda instrumentationΒ #3029

@msonnb

Description

@msonnb

Is your feature request related to a problem? Please describe

Currently, the AWS Lambda instrumentation provides requestHook and responseHook options for customizing span attributes and handling request/response lifecycle events. However, there's no way to wrap the entire patched handler.

At Sentry, we provide a custom wrapHandler function that

  • converts callback-based handlers to promise-based ones
  • inspects the return value of the handler
  • sets a timeout to warn when a handler is near it's max. execution time limit
  • and more...

Ideally we'd want to leverage OpenTelemetry's existing auto-wrapping setup, which is why the following API would be very useful:

Describe the solution you'd like to see

Add a handlerWrapper (name open to bikeshedding) option to AwsLambdaInstrumentationConfig:

export type HandlerWrapper = <T extends Handler>(handler: T) => T;

export interface AwsLambdaInstrumentationConfig extends InstrumentationConfig {
  requestHook?: RequestHook;
  responseHook?: ResponseHook;
  eventContextExtractor?: EventContextExtractor;
  lambdaHandler?: string;
  lambdaStartTime?: number;
  // New option
  handlerWrapper?: HandlerWrapper;
}

The implementation would modify the _getHandler method to apply the wrapper after creating the patched handler:

private _getHandler<T extends Handler>(handlerLoadStartTime: number) {
  return (original: T): T => {
    const patchedHandler = this._getPatchHandler(original, handlerLoadStartTime);
    const { handlerWrapper } = this.getConfig();

    if (handlerWrapper) {
      return handlerWrapper(patchedHandler) as T;
    }

    return patchedHandler as T;
  };
}

Describe alternatives you've considered

We currently maintain a vendored version of the AWS Lambda instrumentation that adds Sentry's wrapHandler directly around patchedHandler and thought that this feature might be useful to others as well.

Tip: React with πŸ‘ to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions