@@ -20,7 +20,8 @@ import {
2020import { HttpResponse } from '@aws-sdk/protocol-http'
2121import { telemetry } from './telemetry'
2222import { getRequestId } from './errors'
23- import { extensionVersion , getLogger } from '.'
23+ import { extensionVersion } from '.'
24+ import { getLogger } from './logger'
2425
2526export type AwsClient = IClient < any , any , any >
2627interface AwsConfigOptions {
@@ -87,22 +88,15 @@ export class DefaultAWSClientBuilderV3 implements AWSClientBuilderV3 {
8788 }
8889}
8990
90- function getServiceId ( context : { clientName ?: string ; commandName ?: string } ) {
91- return context . clientName ?. toLowerCase ( ) . replace ( / c l i e n t $ / , '' )
91+ export function getServiceId ( context : { clientName ?: string ; commandName ?: string } ) : string {
92+ return context . clientName ?. toLowerCase ( ) . replace ( / c l i e n t $ / , '' ) ?? 'unknown-service'
9293}
9394
94- function isExcludedError ( e : Error , ignoredErrors : ( string | typeof Error ) [ ] ) {
95- return (
96- ignoredErrors ?. find ( ( x ) => e . name === x ) ||
97- ( 'code' in e && ignoredErrors ?. find ( ( x ) => e . code === x ) ) ||
98- ignoredErrors ?. find ( ( x ) => typeof x !== 'string' && e instanceof x )
99- )
100- }
10195/**
10296 * Record request IDs to the current context, potentially overriding the field if
10397 * multiple API calls are made in the same context. We only do failures as successes are generally uninteresting and noisy.
10498 */
105- function recordErrorTelemetry ( err : Error , serviceName ?: string ) {
99+ export function recordErrorTelemetry ( err : Error , serviceName ?: string ) {
106100 interface RequestData {
107101 requestId ?: string
108102 requestServiceType ?: string
@@ -114,7 +108,7 @@ function recordErrorTelemetry(err: Error, serviceName?: string) {
114108 } satisfies RequestData as any )
115109}
116110
117- function omitIfPresent < T extends Record < string , unknown > > ( obj : T , keys : string [ ] ) : T {
111+ export function omitIfPresent < T extends Record < string , unknown > > ( obj : T , keys : string [ ] ) : T {
118112 const objCopy = { ...obj }
119113 for ( const key of keys ) {
120114 if ( key in objCopy ) {
@@ -124,25 +118,32 @@ function omitIfPresent<T extends Record<string, unknown>>(obj: T, keys: string[]
124118 return objCopy
125119}
126120
121+ function logAndThrow ( e : any , serviceId : string , errorMessageAppend : string ) : never {
122+ if ( e instanceof Error ) {
123+ recordErrorTelemetry ( e , serviceId )
124+ const err = { ...e }
125+ delete err [ 'stack' ]
126+ getLogger ( ) . error ( 'API Response %s: %O' , errorMessageAppend , err )
127+ }
128+ throw e
129+ }
130+ /**
131+ * Telemetry logic to be added to all created clients. Adds logging and emitting metric on errors.
132+ */
133+
127134const telemetryMiddleware : DeserializeMiddleware < any , any > =
128135 ( next : DeserializeHandler < any , any > , context : HandlerExecutionContext ) => async ( args : any ) => {
129136 if ( ! HttpResponse . isInstance ( args . request ) ) {
130137 return next ( args )
131138 }
132-
139+ const serviceId = getServiceId ( context as object )
133140 const { hostname, path } = args . request
134- const result = await next ( args ) . catch ( ( e : any ) => {
135- if ( e instanceof Error && ! isExcludedError ( e , [ ] ) ) {
136- recordErrorTelemetry ( e , getServiceId ( context as object ) )
137- const err = { ...e }
138- delete err [ 'stack' ]
139- getLogger ( ) . error ( 'API Response (%s %s): %O' , hostname , path , err )
140- }
141- throw e
142- } )
141+ const logTail = `(${ hostname } ${ path } )`
142+ const result = await next ( args ) . catch ( ( e : any ) => logAndThrow ( e , serviceId , logTail ) )
143143 if ( HttpResponse . isInstance ( result . response ) ) {
144+ // TODO: omit credentials / sensitive info from the logs / telemetry.
144145 const output = omitIfPresent ( result . output , [ ] )
145- getLogger ( ) . debug ( 'API Response (%s %s) : %O' , hostname , path , output )
146+ getLogger ( ) . debug ( 'API Response %s : %O' , logTail , output )
146147 }
147148
148149 return result
0 commit comments