-
Notifications
You must be signed in to change notification settings - Fork 156
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
feat: add tracer #107
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
eef22bd
WIP - Tracer basic implementation
dreamorosi 008d657
Reorder Tracer methods
dreamorosi ed12331
Added stub methods for captureAWS & Clients
dreamorosi d0a2e6c
Removed lodash
dreamorosi 302324d
Fixed SAM & Chalice variable comparison
dreamorosi fb4341b
Added tracing provider
dreamorosi aaa79fa
Added capture lambda decorator
dreamorosi 9a48076
Unit tests + format
dreamorosi 4973531
Removed package-lock, added npm-shrinkwrap
dreamorosi 0c26e5d
WIP: capture clients
dreamorosi 23d77eb
Updated license in package
dreamorosi 9dca301
Simplified unit tests
dreamorosi dcddcff
Added more tests
dreamorosi 37408c5
WIP examples
dreamorosi ea91c19
WIP method decorator
dreamorosi ca2f843
Enable tracing only when running in Lambda
dreamorosi 3cf7230
Removed redundant env restore commands in tests
dreamorosi 6f206f5
Fixed ERR_UNHANDLED_REJECTION
dreamorosi 7b7f1b9
Removed debug logs
dreamorosi 7922ce8
Updated packages & config
dreamorosi 8b525e0
Documentation
dreamorosi fea7f0d
Typing & Tests
dreamorosi f2325b5
Updated package & config
dreamorosi 3c449ea
Added capture method
dreamorosi 85dc25a
Changed TracerInterface
dreamorosi a8f2eef
Removed Chalice references
dreamorosi 160779f
Sorted imports
dreamorosi b060b8c
Fixed type for return of captureLambdaHandler
dreamorosi 306a948
Linting tests
dreamorosi dbada68
Fixed linting & added TODOs
dreamorosi 8a5bcc0
Applied fix to PowertoolLogFormatter see #304
dreamorosi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
dreamorosi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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!')); | ||
}); |
31 changes: 31 additions & 0 deletions
31
packages/tracing/examples/capture-lambda-handler-error-decorator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!')); | ||
}); |
33 changes: 33 additions & 0 deletions
33
packages/tracing/examples/capture-lambda-handler-response-decorator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!')); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!')); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './LambdaInterface'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
] | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.