-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into mrgrain/chore/toolkit/emit-more-times
- Loading branch information
Showing
12 changed files
with
190 additions
and
113 deletions.
There are no files selected for viewing
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
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
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,59 @@ | ||
import type { Logger } from '@smithy/types'; | ||
|
||
let ENABLED = false; | ||
let INDENT = 0; | ||
|
||
export function setSdkTracing(enabled: boolean) { | ||
ENABLED = enabled; | ||
} | ||
|
||
/** | ||
* Method decorator to trace a single static or member method, any time it's called | ||
*/ | ||
export function callTrace(fn: string, className?: string, logger?: Logger) { | ||
if (!ENABLED || !logger) { | ||
return; | ||
} | ||
|
||
logger.info(`[trace] ${' '.repeat(INDENT)}${className || '(anonymous)'}#${fn}()`); | ||
} | ||
|
||
/** | ||
* Method decorator to trace a single member method any time it's called | ||
*/ | ||
function traceCall(receiver: object, _propertyKey: string, descriptor: PropertyDescriptor, parentClassName?: string) { | ||
const fn = descriptor.value; | ||
const className = typeof receiver === 'function' ? receiver.name : parentClassName; | ||
|
||
descriptor.value = function (...args: any[]) { | ||
const logger = (this as any).logger; | ||
if (!ENABLED || typeof logger?.info !== 'function') { return fn.apply(this, args); } | ||
|
||
logger.info.apply(logger, [`[trace] ${' '.repeat(INDENT)}${className || this.constructor.name || '(anonymous)'}#${fn.name}()`]); | ||
INDENT += 2; | ||
|
||
const ret = fn.apply(this, args); | ||
if (ret instanceof Promise) { | ||
return ret.finally(() => { | ||
INDENT -= 2; | ||
}); | ||
} else { | ||
INDENT -= 2; | ||
return ret; | ||
} | ||
}; | ||
return descriptor; | ||
} | ||
|
||
/** | ||
* Class decorator, enable tracing for all member methods on this class | ||
* @deprecated this doesn't work well with localized logging instances, don't use | ||
*/ | ||
export function traceMemberMethods(constructor: Function) { | ||
// Instance members | ||
for (const [name, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(constructor.prototype))) { | ||
if (typeof descriptor.value !== 'function') { continue; } | ||
const newDescriptor = traceCall(constructor.prototype, name, descriptor, constructor.name) ?? descriptor; | ||
Object.defineProperty(constructor.prototype, name, newDescriptor); | ||
} | ||
} |
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
Oops, something went wrong.