Skip to content

feat: add tracer #107

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

Merged
merged 31 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
eef22bd
WIP - Tracer basic implementation
dreamorosi Jun 24, 2021
008d657
Reorder Tracer methods
dreamorosi Jun 25, 2021
ed12331
Added stub methods for captureAWS & Clients
dreamorosi Jun 25, 2021
d0a2e6c
Removed lodash
dreamorosi Jun 25, 2021
302324d
Fixed SAM & Chalice variable comparison
dreamorosi Jun 25, 2021
fb4341b
Added tracing provider
dreamorosi Jun 25, 2021
aaa79fa
Added capture lambda decorator
dreamorosi Jun 27, 2021
9a48076
Unit tests + format
dreamorosi Jul 2, 2021
4973531
Removed package-lock, added npm-shrinkwrap
dreamorosi Jul 20, 2021
0c26e5d
WIP: capture clients
dreamorosi Jul 23, 2021
23d77eb
Updated license in package
dreamorosi Jul 27, 2021
9dca301
Simplified unit tests
dreamorosi Jul 27, 2021
dcddcff
Added more tests
dreamorosi Jul 27, 2021
37408c5
WIP examples
dreamorosi Jul 27, 2021
ea91c19
WIP method decorator
dreamorosi Jul 28, 2021
ca2f843
Enable tracing only when running in Lambda
dreamorosi Jul 29, 2021
3cf7230
Removed redundant env restore commands in tests
dreamorosi Jul 29, 2021
6f206f5
Fixed ERR_UNHANDLED_REJECTION
dreamorosi Jul 29, 2021
7b7f1b9
Removed debug logs
dreamorosi Jul 30, 2021
7922ce8
Updated packages & config
dreamorosi Dec 6, 2021
8b525e0
Documentation
dreamorosi Dec 6, 2021
fea7f0d
Typing & Tests
dreamorosi Dec 6, 2021
f2325b5
Updated package & config
dreamorosi Dec 7, 2021
3c449ea
Added capture method
dreamorosi Dec 7, 2021
85dc25a
Changed TracerInterface
dreamorosi Dec 8, 2021
a8f2eef
Removed Chalice references
dreamorosi Dec 10, 2021
160779f
Sorted imports
dreamorosi Dec 10, 2021
b060b8c
Fixed type for return of captureLambdaHandler
dreamorosi Dec 10, 2021
306a948
Linting tests
dreamorosi Dec 10, 2021
dbada68
Fixed linting & added TODOs
dreamorosi Dec 13, 2021
8a5bcc0
Applied fix to PowertoolLogFormatter see #304
dreamorosi Dec 14, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/tracing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# `tracer`

## Usage

```bash

npm run test

npm run example:hello-world
npm run example:capture-lambda-handler-response-decorator
npm run example:capture-lambda-handler-error-decorator

```

### Getting started

```typescript
// Import the library
import { Tracer } from "../src";
// When going public, it will be something like: import { Tracer } from '@aws-lambda-powertools/tracer';
```
53 changes: 53 additions & 0 deletions packages/tracing/examples/capture-clients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Populate runtime
require('./../tests/helpers/populateEnvironmentVariables');

// Additional runtime variables
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
process.env.AWS_XRAY_DEBUG_MODE = 'TRUE';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { TracingNamespace as dummyTracingNamespace } from './utils/namespaces/hello-world';
import { Handler } from 'aws-lambda';
import { Tracer } from '../src';

const tracer = new Tracer();

class DummyServiceV2 {
private customRequestHandler?: null | unknown;

public act(): void {
return;
}

public customizeRequests(callback: unknown): void {
if (!callback) {
this.customRequestHandler = null;
} else if (typeof callback === 'function') {
this.customRequestHandler = callback;
} else {
throw new Error('Invalid callback type \'' + typeof callback + '\' provided in customizeRequests');
}
}
}

class DummyServiceV3 {
private customRequestHandler?: null | unknown;

public act(): void {
return;
}

}

const lambdaHandler: Handler = async () => {
const dummyServiceV2 = tracer.captureAWSClient(new DummyServiceV2());
dummyServiceV2?.act();

const dummyServiceV3 = tracer.captureAWSv3Client(new DummyServiceV3());
dummyServiceV3?.act();
};

dummyTracingNamespace(tracer, () => {
lambdaHandler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Populate runtime
require('./../tests/helpers/populateEnvironmentVariables');

// Additional runtime variables
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
process.env.AWS_XRAY_DEBUG_MODE = 'TRUE';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { TracingNamespace as dummyTracingNamespace } from './utils/namespaces/hello-world';
import { LambdaInterface } from './utils/lambda/LambdaInterface';
import { Tracer } from '../src';
import { Callback, Context } from 'aws-lambda/handler';

const tracer = new Tracer();

class Lambda implements LambdaInterface {

@tracer.captureLambdaHanlder()
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {

tracer.putAnnotation('StringAnnotation', 'AnnotationValue');

throw new Error('foo bar');
}

}

dummyTracingNamespace(tracer, () => {
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Populate runtime
require('./../tests/helpers/populateEnvironmentVariables');

// Additional runtime variables
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
process.env.AWS_XRAY_DEBUG_MODE = 'TRUE';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { TracingNamespace as dummyTracingNamespace } from './utils/namespaces/hello-world';
import { LambdaInterface } from './utils/lambda/LambdaInterface';
import { Tracer } from '../src';
import { Callback, Context } from 'aws-lambda/handler';

const tracer = new Tracer();

class Lambda implements LambdaInterface {

@tracer.captureLambdaHanlder()
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {

tracer.putAnnotation('StringAnnotation', 'AnnotationValue');

return new Promise((resolve, _reject) => resolve({
foo: 'bar'
} as unknown as TResult));
}

}

dummyTracingNamespace(tracer, () => {
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
});
31 changes: 31 additions & 0 deletions packages/tracing/examples/hello-world.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Populate runtime
require('./../tests/helpers/populateEnvironmentVariables');

// Additional runtime variables
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
process.env.AWS_XRAY_DEBUG_MODE = 'TRUE';

import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { TracingNamespace as dummyTracingNamespace } from './utils/namespaces/hello-world';
import { Handler } from 'aws-lambda';
import { Tracer } from '../src';

const tracer = new Tracer();

const lambdaHandler: Handler = async () => {
tracer.putAnnotation('StringAnnotation', 'AnnotationValue');
tracer.putAnnotation('NumberAnnotation', 1234);
tracer.putAnnotation('BooleanAnnotationKey', true);
tracer.putMetadata('MetadataKey', {});
tracer.putMetadata('MetadataKey', {}, 'SomeNameSpace');

return {
foo: 'bar'
};

};

dummyTracingNamespace(tracer, () => {
lambdaHandler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
});
9 changes: 9 additions & 0 deletions packages/tracing/examples/utils/lambda/LambdaInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Handler } from 'aws-lambda';

interface LambdaInterface {
handler: Handler
}

export {
LambdaInterface
};
1 change: 1 addition & 0 deletions packages/tracing/examples/utils/lambda/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './LambdaInterface';
16 changes: 16 additions & 0 deletions packages/tracing/examples/utils/namespaces/hello-world.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Segment } from 'aws-xray-sdk-core';
import { Tracer } from 'Tracer';

const TracingNamespace = (tracer: Tracer, handler: () => void): void => {
const ns = tracer.provider.getNamespace();
ns.run(() => {
const segment = new Segment('facade', process.env._X_AMZN_TRACE_ID || null);
tracer.provider.setSegment(segment);
handler();
// segment.close();
});
};

export {
TracingNamespace
};
43 changes: 43 additions & 0 deletions packages/tracing/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = {
displayName: {
name: 'AWS Lambda Powertools utility: TRACER',
color: 'cyan',
},
'preset': 'ts-jest',
'transform': {
'^.+\\.ts?$': 'ts-jest',
},
moduleFileExtensions: [ 'js', 'ts' ],
'collectCoverageFrom': [
'**/src/**/*.ts',
'!**/node_modules/**',
],
'testMatch': ['**/?(*.)+(spec|test).ts'],
'roots': [
'<rootDir>/src',
'<rootDir>/tests',
],
'testPathIgnorePatterns': [
'/node_modules/',
],
'testEnvironment': 'node',
'coveragePathIgnorePatterns': [
'/node_modules/',
],
'coverageThreshold': {
'global': {
'statements': 100,
'branches': 100,
'functions': 100,
'lines': 100,
},
},
'coverageReporters': [
'json-summary',
'text',
'lcov'
],
'setupFiles': [
'<rootDir>/tests/helpers/populateEnvironmentVariables.ts'
]
};
Loading