-
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.
refactor(cli): localize SDK tracing (#33273)
### Reason for this change The CLI has a global method call tracing feature that is currently only used for SDK calls. Eventually this feature needs to support logging through the IoHost. While this PR doesn't achieve this, it co-locates the tracing feature with SDK logs and explicitly calls out its limitations. We will need a better, different approach once we are looking at the SDK Provider in more detail. ### Description of changes Move the feature to `aws-auth` module. Limit exports and renames for clarity. Limit tracing to member methods as these can add a class logger. Add a manual trace to the one static method this was currently tracig ### Describe any new or updated permissions being added n/a ### Description of how you validated changes Existing unit & integ tests, manual verification ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
8 changed files
with
109 additions
and
78 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
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
This file was deleted.
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