Skip to content

Commit

Permalink
chore: remove all captureLambdaHandler decorator changes
Browse files Browse the repository at this point in the history
  • Loading branch information
misterjoshua committed Aug 18, 2022
1 parent 14ccc2f commit ae72d52
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 71 deletions.
17 changes: 1 addition & 16 deletions docs/core/tracer.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ Use **`POWERTOOLS_TRACER_CAPTURE_RESPONSE=false`** environment variable to instr

### Disabling response capture for targeted methods and handlers

Use the `captureResponse: false` option in both `tracer.captureLambdaHandler()` and `tracer.captureMethod()` decorators to instruct Tracer **not** to serialize function responses as metadata.
Use the `captureResponse: false` option in `tracer.captureMethod()` decorators to instruct Tracer **not** to serialize function responses as metadata.

=== "method.ts"

Expand All @@ -431,21 +431,6 @@ Use the `captureResponse: false` option in both `tracer.captureLambdaHandler()`
}
```

=== "handler.ts"

```typescript hl_lines="6"
import { Tracer } from '@aws-lambda-powertools/tracer';
import { LambdaInterface } from '@aws-lambda-powertools/commons';

const tracer = new Tracer({ serviceName: 'serverlessAirline' });
class MyHandler implements LambdaInterface {
@tracer.captureLambdaHandler({ captureResponse: false })
async handler(_event: any, _context: any): Promise<void> {
/* ... */
}
}
```

### Disabling exception auto-capture

Use **`POWERTOOLS_TRACER_CAPTURE_ERROR=false`** environment variable to instruct Tracer **not** to serialize exceptions as metadata.
Expand Down
10 changes: 4 additions & 6 deletions packages/tracer/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, TracerCaptureMethodOptions, TracerCaptureLambdaHandlerOptions, MethodDecorator } from './types';
import { HandlerMethodDecorator, TracerOptions, TracerCaptureMethodOptions, MethodDecorator } from './types';
import { ProviderService, ProviderServiceInterface } from './provider';
import { Segment, Subsegment } from 'aws-xray-sdk-core';

Expand Down Expand Up @@ -339,7 +339,7 @@ class Tracer extends Utility implements TracerInterface {
*
* @decorator Class
*/
public captureLambdaHandler(options?: TracerCaptureLambdaHandlerOptions): HandlerMethodDecorator {
public captureLambdaHandler(): HandlerMethodDecorator {
return (_target, _propertyKey, descriptor) => {
/**
* The descriptor.value is the method this decorator decorates, it cannot be undefined.
Expand All @@ -365,10 +365,7 @@ class Tracer extends Utility implements TracerInterface {
let result: unknown;
try {
result = await originalMethod.apply(handlerRef, [ event, context, callback ]);
if (options?.captureResponse ?? true) {
tracerRef.addResponseAsMetadata(result, process.env._HANDLER);
}

tracerRef.addResponseAsMetadata(result, process.env._HANDLER);
} catch (error) {
tracerRef.addErrorAsMetadata(error as Error);
throw error;
Expand Down Expand Up @@ -444,6 +441,7 @@ class Tracer extends Utility implements TracerInterface {

} catch (error) {
tracerRef.addErrorAsMetadata(error as Error);

throw error;
} finally {
subsegment?.close();
Expand Down
19 changes: 0 additions & 19 deletions packages/tracer/src/types/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,6 @@ type TracerCaptureMethodOptions = {
captureResponse?: boolean
};

/**
* Options for the captureLambdaHandler decorator to be used when decorating a method.
*
* Usage:
* @example
* ```typescript
* const tracer = new Tracer();
*
* class MyThing implements LambdaInterface {
* @tracer.captureLambdaHandler({ captureResponse: false })
* async handler(_event: any, _context: any): Promise<void> {}
* }
* ```
*/
type TracerCaptureLambdaHandlerOptions = {
captureResponse?: boolean
};

type HandlerMethodDecorator = (
target: LambdaInterface,
propertyKey: string | symbol,
Expand All @@ -76,7 +58,6 @@ type MethodDecorator = (target: any, propertyKey: string | symbol, descriptor: T

export {
TracerOptions,
TracerCaptureLambdaHandlerOptions,
TracerCaptureMethodOptions,
HandlerMethodDecorator,
MethodDecorator
Expand Down
30 changes: 0 additions & 30 deletions packages/tracer/tests/unit/Tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,36 +648,6 @@ describe('Class: Tracer', () => {

});

test('when used as decorator while captureResponse is set to false, it does not capture the response as metadata', async () => {

// Prepare
const tracer: Tracer = new Tracer();
const newSubsegment: Segment | Subsegment | undefined = new Subsegment('## index.handler');
jest.spyOn(tracer.provider, 'getSegment').mockImplementation(() => newSubsegment);
setContextMissingStrategy(() => null);
const captureAsyncFuncSpy = jest.spyOn(tracer.provider, 'captureAsyncFunc');
class Lambda implements LambdaInterface {

@tracer.captureLambdaHandler({ captureResponse: false })
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
return new Promise((resolve, _reject) => resolve({
foo: 'bar'
} as unknown as TResult));
}

}

// Act
await new Lambda().handler(event, context, () => console.log('Lambda invoked!'));

// Assess
expect(captureAsyncFuncSpy).toHaveBeenCalledTimes(1);
expect('metadata' in newSubsegment).toBe(false);

});

test('when used as decorator and with standard config, it captures the response as metadata', async () => {

// Prepare
Expand Down

0 comments on commit ae72d52

Please sign in to comment.