Skip to content

Commit

Permalink
feat(midway-component-fetch): breaking change callback types
Browse files Browse the repository at this point in the history
- ReqCallbackOptions
- RespCallbackOptions
- ProcessExCallbackOptions

add FetchComponentConfig['traceLoggingRespHeaders']
  • Loading branch information
waitingsong committed Jun 15, 2021
1 parent 5f2445b commit 0d82c62
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 26 deletions.
6 changes: 6 additions & 0 deletions packages/midway-component-fetch/src/config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ export const fetch: FetchComponentConfig = {
enableTraceLoggingReqBody: false,
enableTraceLoggingRespData: false,
traceLoggingReqHeaders: [],
traceLoggingRespHeaders: [
'content-type',
'server',
'x-aspnet-version',
'x-powered-by',
],
}

11 changes: 11 additions & 0 deletions packages/midway-component-fetch/src/config/config.local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,16 @@ export const fetch: FetchComponentConfig = {
enableDefaultCallbacks: false,
enableTraceLoggingReqBody: true,
enableTraceLoggingRespData: true,
traceLoggingReqHeaders: [
'authorization',
'user-agent',
],
traceLoggingRespHeaders: [
'authorization',
'user-agent',
'server',
'x-aspnet-version',
'x-powered-by',
],
}

20 changes: 7 additions & 13 deletions packages/midway-component-fetch/src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,13 @@ export class FetchComponent {
opts.headers = this.genReqHeadersFromOptionsAndConfigCallback(opts.headers)

const config = this.fetchConfig

const enableTraceLoggingReqBody = !! config.enableTraceLoggingReqBody
const enableTraceLoggingRespData = !! config.enableTraceLoggingRespData
const traceLoggingReqHeaders = Array.isArray(config.traceLoggingReqHeaders)
? config.traceLoggingReqHeaders
: []
const id = Symbol(opts.url)

if (this.fetchConfig.enableDefaultCallbacks) {
await defaultfetchConfigCallbacks.beforeRequest({
id,
ctx: this.ctx,
enableTraceLoggingReqBody,
traceLoggingReqHeaders,
config: this.fetchConfig,
opts,
})

Expand All @@ -69,8 +62,7 @@ export class FetchComponent {
await config.beforeRequest({
id,
ctx: this.ctx,
enableTraceLoggingReqBody,
traceLoggingReqHeaders,
config: this.fetchConfig,
opts,
})
}
Expand All @@ -82,7 +74,7 @@ export class FetchComponent {
ret = config.processResult({
id,
ctx: this.ctx,
enableTraceLoggingRespData,
config: this.fetchConfig,
opts,
resultData: ret,
})
Expand All @@ -92,7 +84,7 @@ export class FetchComponent {
await config.afterResponse({
id,
ctx: this.ctx,
enableTraceLoggingRespData,
config: this.fetchConfig,
opts,
resultData: ret,
})
Expand All @@ -102,7 +94,7 @@ export class FetchComponent {
await defaultfetchConfigCallbacks.afterResponse({
id,
ctx: this.ctx,
enableTraceLoggingRespData,
config: this.fetchConfig,
opts,
resultData: ret,
})
Expand All @@ -116,6 +108,7 @@ export class FetchComponent {
return defaultfetchConfigCallbacks.processEx({
id,
ctx: this.ctx,
config: this.fetchConfig,
opts,
exception: ex as Error,
})
Expand All @@ -125,6 +118,7 @@ export class FetchComponent {
return config.processEx({
id,
ctx: this.ctx,
config: this.fetchConfig,
opts,
exception: ex as Error,
})
Expand Down
56 changes: 46 additions & 10 deletions packages/midway-component-fetch/src/lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ const beforeRequest: FetchComponentConfig['beforeRequest'] = async (options) =>
const {
id,
ctx,
enableTraceLoggingReqBody,
traceLoggingReqHeaders,
opts,
} = options
const {
enableTraceLoggingReqBody,
traceLoggingReqHeaders,
} = options.config

const time = genISO8601String()
const mem = humanMemoryUsage()
Expand Down Expand Up @@ -55,12 +57,14 @@ const beforeRequest: FetchComponentConfig['beforeRequest'] = async (options) =>
tags[TracerTag.reqQuery] = 'Not logged'
}

traceLoggingReqHeaders.forEach((name) => {
const val = retrieveHeadersItem(opts.headers, name)
if (val) {
tags[`http.${name}`] = val
}
})
if (Array.isArray(traceLoggingReqHeaders)) {
traceLoggingReqHeaders.forEach((name) => {
const val = retrieveHeadersItem(opts.headers, name)
if (typeof val !== 'undefined') {
tags[`http.${name}`] = val
}
})
}

span.addTags(tags)

Expand All @@ -74,7 +78,17 @@ const beforeRequest: FetchComponentConfig['beforeRequest'] = async (options) =>
}

const afterResponse: FetchComponentConfig['afterResponse'] = async (options) => {
const { id, ctx, enableTraceLoggingRespData, opts, resultData } = options
const {
id,
ctx,
opts,
resultData,
} = options
const {
enableTraceLoggingRespData,
traceLoggingRespHeaders,
} = options.config

const time = genISO8601String()
const mem = humanMemoryUsage()
const parentInput: SpanLogInput = {
Expand All @@ -101,6 +115,15 @@ const afterResponse: FetchComponentConfig['afterResponse'] = async (options) =>
tags[TracerTag.respBody] = 'Not logged'
}

if (Array.isArray(traceLoggingRespHeaders)) {
traceLoggingRespHeaders.forEach((name) => {
const val = retrieveHeadersItem(opts.headers, name)
if (typeof val !== 'undefined') {
tags[`http.${name}`] = val
}
})
}

Object.keys(tags).length && span.addTags(tags)

const input: SpanLogInput = {
Expand All @@ -115,7 +138,10 @@ const afterResponse: FetchComponentConfig['afterResponse'] = async (options) =>
}

export const processEx: FetchComponentConfig['processEx'] = (options) => {
const { id, ctx, exception } = options
const { id, ctx, opts, exception } = options
const {
traceLoggingRespHeaders,
} = options.config
const time = genISO8601String()
const mem = humanMemoryUsage()

Expand All @@ -135,6 +161,16 @@ export const processEx: FetchComponentConfig['processEx'] = (options) => {
[TracerTag.logLevel]: 'error',
[TracerTag.svcException]: exception,
}

if (Array.isArray(traceLoggingRespHeaders)) {
traceLoggingRespHeaders.forEach((name) => {
const val = retrieveHeadersItem(opts.headers, name)
if (typeof val !== 'undefined') {
tags[`http.${name}`] = val
}
})
}

span.addTags(tags)

const input: SpanLogInput = {
Expand Down
10 changes: 7 additions & 3 deletions packages/midway-component-fetch/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,31 @@ export interface FetchComponentConfig {
* @example ['authorization', 'user-agent']
*/
traceLoggingReqHeaders?: string[]
/**
* @example ['authorization', 'user-agent', 'server']
*/
traceLoggingRespHeaders?: string[]
}

export interface ReqCallbackOptions {
id: symbol
ctx: Context
enableTraceLoggingReqBody: boolean
traceLoggingReqHeaders: string[]
config: FetchComponentConfig
opts: Options
}

export interface RespCallbackOptions <T = unknown> {
id: symbol
ctx: Context
enableTraceLoggingRespData: boolean
config: FetchComponentConfig
opts: Options
resultData: T
}

export interface ProcessExCallbackOptions {
id: symbol
ctx: Context
config: FetchComponentConfig
opts: Options
exception: Error
}
Expand Down

0 comments on commit 0d82c62

Please sign in to comment.