generated from MapColonies/ts-npm-package-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tracing): add utils for using spans (#58)
* feat(tracing): add utils for using spans * docs: add comments for typedoc * feat(tracing): add decorators for tracing * fix: cr * fix: lint * fix: export utils and decorators * fix: cr * fix: pass span instance to callback
- Loading branch information
Showing
8 changed files
with
4,339 additions
and
947 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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,56 @@ | ||
import { Tracer } from '@opentelemetry/api'; | ||
import { asyncCallWithSpan, callWithSpan } from '../utils/tracing'; | ||
|
||
/** | ||
* Decorator that creates a trace span for the decorated method logic. | ||
* using the typescript decorators stage 2. | ||
* requires the "experimentalDecorators" compiler option to be true. | ||
* @param _target the class prototype | ||
* @param propertyKey the name of the decorated method | ||
* @param descriptor the method descriptor | ||
* @returns the decorated descriptor | ||
*/ | ||
export function withSpan<This extends { tracer: Tracer }, Args extends unknown[], Return>( | ||
_target: This, | ||
propertyKey: string | symbol, | ||
descriptor: TypedPropertyDescriptor<(this: This, ...args: Args) => Return> | ||
): TypedPropertyDescriptor<(this: This, ...args: Args) => Return> { | ||
const originalMethod = descriptor.value; | ||
|
||
if (originalMethod === undefined) { | ||
throw new Error('Decorated method is undefined'); | ||
} | ||
|
||
descriptor.value = function (this: This, ...args: Args): Return { | ||
return callWithSpan(() => originalMethod.call(this, ...args), this.tracer, String(propertyKey)); | ||
}; | ||
|
||
return descriptor; | ||
} | ||
|
||
/** | ||
* Decorator that creates a trace span for the decorated async method logic. | ||
* using the typescript decorators stage 2. | ||
* requires the "experimentalDecorators" compiler option to be true. | ||
* @param _target the class prototype | ||
* @param propertyKey the name of the decorated async method | ||
* @param descriptor the async method descriptor | ||
* @returns the decorated descriptor | ||
*/ | ||
export function withSpanAsync<This extends { tracer: Tracer }, Args extends unknown[], Return>( | ||
_target: This, | ||
propertyKey: string | symbol, | ||
descriptor: TypedPropertyDescriptor<(this: This, ...args: Args) => Promise<Return>> | ||
): TypedPropertyDescriptor<(this: This, ...args: Args) => Promise<Return>> { | ||
const originalMethod = descriptor.value; | ||
|
||
if (originalMethod === undefined) { | ||
throw new Error('Decorated method is undefined'); | ||
} | ||
|
||
descriptor.value = async function (this: This, ...args: Args): Promise<Return> { | ||
return asyncCallWithSpan(async () => originalMethod.call(this, ...args), this.tracer, String(propertyKey)); | ||
}; | ||
|
||
return descriptor; | ||
} |
This file contains 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,36 @@ | ||
import { Tracer } from '@opentelemetry/api'; | ||
import { asyncCallWithSpan, callWithSpan } from '../utils/tracing'; | ||
|
||
/** | ||
* Decorator that creates a trace span for the decorated method logic. | ||
* using the typescript decorators stage 3, which available in typescript v5 and above. | ||
* requires the "experimentalDecorators" compiler option to be false. | ||
* @param target the method to decorate | ||
* @param context the class method decorator context | ||
* @returns the decorated method | ||
*/ | ||
export function withSpan<This extends { tracer: Tracer }, Args extends unknown[], Return>( | ||
target: (this: This, ...args: Args) => Return, | ||
context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return> | ||
) { | ||
return function (this: This, ...args: Args): Return { | ||
return callWithSpan(() => target.call(this, ...args), this.tracer, String(context.name)); | ||
}; | ||
} | ||
|
||
/** | ||
* Decorator that creates a trace span for the decorated async method logic. | ||
* using the typescript decorators stage 3, which available in typescript v5 and above. | ||
* requires the "experimentalDecorators" compiler option to be false. | ||
* @param target the async method to decorate | ||
* @param context the class method decorator context | ||
* @returns the decorated async method | ||
*/ | ||
export function withSpanAsync<This extends { tracer: Tracer }, Args extends unknown[], Return>( | ||
target: (this: This, ...args: Args) => Promise<Return>, | ||
context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Promise<Return>> | ||
) { | ||
return async function (this: This, ...args: Args): Promise<Return> { | ||
return asyncCallWithSpan(async () => target.call(this, ...args), this.tracer, String(context.name)); | ||
}; | ||
} |
This file contains 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export { Tracing } from './tracing'; | ||
export { getTraceContexHeaderMiddleware } from './middleware/traceOnHeaderMiddleware'; | ||
export { contexBindingHelper, ignoreIncomingRequestUrl, ignoreOutgoingRequestPath } from './utils/tracing'; | ||
export * from './utils/tracing'; | ||
export { getOtelMixin } from './mixin'; | ||
export { logMethod } from './loggerHook'; | ||
export { withSpan, withSpanAsync } from './decorators/v5'; | ||
export { withSpan as withSpanV4, withSpanAsync as withSpanAsyncV4 } from './decorators/v4'; |
This file contains 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
This file contains 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