-
Notifications
You must be signed in to change notification settings - Fork 161
feat(tracer): allow disabling result capture for decorators and middleware #1065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cc59160
6eedbbe
14ccc2f
ae72d52
81fd698
e456670
dd42e52
cb4de92
2c84e5f
9597970
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ import { Handler } from 'aws-lambda'; | |||||
import { AsyncHandler, SyncHandler, Utility } from '@aws-lambda-powertools/commons'; | ||||||
import { TracerInterface } from '.'; | ||||||
import { ConfigServiceInterface, EnvironmentVariablesService } from './config'; | ||||||
import { HandlerMethodDecorator, TracerOptions, MethodDecorator } from './types'; | ||||||
import { HandlerMethodDecorator, TracerOptions, HandlerOptions, MethodDecorator } from './types'; | ||||||
import { ProviderService, ProviderServiceInterface } from './provider'; | ||||||
import { Segment, Subsegment } from 'aws-xray-sdk-core'; | ||||||
|
||||||
|
@@ -339,7 +339,7 @@ class Tracer extends Utility implements TracerInterface { | |||||
* | ||||||
* @decorator Class | ||||||
*/ | ||||||
public captureLambdaHandler(): HandlerMethodDecorator { | ||||||
public captureLambdaHandler(options?: HandlerOptions): HandlerMethodDecorator { | ||||||
return (_target, _propertyKey, descriptor) => { | ||||||
/** | ||||||
* The descriptor.value is the method this decorator decorates, it cannot be undefined. | ||||||
|
@@ -365,7 +365,10 @@ class Tracer extends Utility implements TracerInterface { | |||||
let result: unknown; | ||||||
try { | ||||||
result = await originalMethod.apply(handlerRef, [ event, context, callback ]); | ||||||
tracerRef.addResponseAsMetadata(result, process.env._HANDLER); | ||||||
if (options?.captureResponse ?? true) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we convert all the instances of this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can make readability improvements here. But, before we do that, I'd like to point out that before this PR,
I believe backward compatible logic here would be either For comparison's sake: To make the code more readable, in these cases in the CDK project, we often do two things: We put our defaults higher up in a block of code, assigning them earlier, more conspicuously as default values, where the function of nullish coalescing ( We also typically provide an empty object for optional arguments so we can avoid the optional chaining ( If we took these approaches, perhaps the code could look like this: public captureLambdaHandler(options: TracerCaptureLambdaHandlerOptions = {}): HandlerMethodDecorator {
// Capture response by default.
const captureResponse = options.captureResponse ?? true;
// ...
if (captureResponse) {
// ...
}
// ...
} Let me know what you think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the explanation, I now understand your original proposal & I agree with the syntax :) I'm going to leave this open so that others can benefit from this explanation. |
||||||
tracerRef.addResponseAsMetadata(result, process.env._HANDLER); | ||||||
} | ||||||
|
||||||
} catch (error) { | ||||||
tracerRef.addErrorAsMetadata(error as Error); | ||||||
throw error; | ||||||
|
@@ -416,7 +419,7 @@ class Tracer extends Utility implements TracerInterface { | |||||
* | ||||||
* @decorator Class | ||||||
*/ | ||||||
public captureMethod(): MethodDecorator { | ||||||
public captureMethod(options?: HandlerOptions): MethodDecorator { | ||||||
return (_target, _propertyKey, descriptor) => { | ||||||
// The descriptor.value is the method this decorator decorates, it cannot be undefined. | ||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||||||
|
@@ -435,7 +438,9 @@ class Tracer extends Utility implements TracerInterface { | |||||
let result; | ||||||
try { | ||||||
result = await originalMethod.apply(this, [...args]); | ||||||
tracerRef.addResponseAsMetadata(result, originalMethod.name); | ||||||
if (options?.captureResponse ?? true) { | ||||||
tracerRef.addResponseAsMetadata(result, originalMethod.name); | ||||||
} | ||||||
} catch (error) { | ||||||
tracerRef.addErrorAsMetadata(error as Error); | ||||||
|
||||||
dreamorosi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
Uh oh!
There was an error while loading. Please reload this page.