Skip to content

Commit 0d82c62

Browse files
committed
feat(midway-component-fetch): breaking change callback types
- ReqCallbackOptions - RespCallbackOptions - ProcessExCallbackOptions add FetchComponentConfig['traceLoggingRespHeaders']
1 parent 5f2445b commit 0d82c62

File tree

5 files changed

+77
-26
lines changed

5 files changed

+77
-26
lines changed

packages/midway-component-fetch/src/config/config.default.ts

+6
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@ export const fetch: FetchComponentConfig = {
99
enableTraceLoggingReqBody: false,
1010
enableTraceLoggingRespData: false,
1111
traceLoggingReqHeaders: [],
12+
traceLoggingRespHeaders: [
13+
'content-type',
14+
'server',
15+
'x-aspnet-version',
16+
'x-powered-by',
17+
],
1218
}
1319

packages/midway-component-fetch/src/config/config.local.ts

+11
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,16 @@ export const fetch: FetchComponentConfig = {
88
enableDefaultCallbacks: false,
99
enableTraceLoggingReqBody: true,
1010
enableTraceLoggingRespData: true,
11+
traceLoggingReqHeaders: [
12+
'authorization',
13+
'user-agent',
14+
],
15+
traceLoggingRespHeaders: [
16+
'authorization',
17+
'user-agent',
18+
'server',
19+
'x-aspnet-version',
20+
'x-powered-by',
21+
],
1122
}
1223

packages/midway-component-fetch/src/lib/fetch.ts

+7-13
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,13 @@ export class FetchComponent {
4242
opts.headers = this.genReqHeadersFromOptionsAndConfigCallback(opts.headers)
4343

4444
const config = this.fetchConfig
45-
46-
const enableTraceLoggingReqBody = !! config.enableTraceLoggingReqBody
47-
const enableTraceLoggingRespData = !! config.enableTraceLoggingRespData
48-
const traceLoggingReqHeaders = Array.isArray(config.traceLoggingReqHeaders)
49-
? config.traceLoggingReqHeaders
50-
: []
5145
const id = Symbol(opts.url)
5246

5347
if (this.fetchConfig.enableDefaultCallbacks) {
5448
await defaultfetchConfigCallbacks.beforeRequest({
5549
id,
5650
ctx: this.ctx,
57-
enableTraceLoggingReqBody,
58-
traceLoggingReqHeaders,
51+
config: this.fetchConfig,
5952
opts,
6053
})
6154

@@ -69,8 +62,7 @@ export class FetchComponent {
6962
await config.beforeRequest({
7063
id,
7164
ctx: this.ctx,
72-
enableTraceLoggingReqBody,
73-
traceLoggingReqHeaders,
65+
config: this.fetchConfig,
7466
opts,
7567
})
7668
}
@@ -82,7 +74,7 @@ export class FetchComponent {
8274
ret = config.processResult({
8375
id,
8476
ctx: this.ctx,
85-
enableTraceLoggingRespData,
77+
config: this.fetchConfig,
8678
opts,
8779
resultData: ret,
8880
})
@@ -92,7 +84,7 @@ export class FetchComponent {
9284
await config.afterResponse({
9385
id,
9486
ctx: this.ctx,
95-
enableTraceLoggingRespData,
87+
config: this.fetchConfig,
9688
opts,
9789
resultData: ret,
9890
})
@@ -102,7 +94,7 @@ export class FetchComponent {
10294
await defaultfetchConfigCallbacks.afterResponse({
10395
id,
10496
ctx: this.ctx,
105-
enableTraceLoggingRespData,
97+
config: this.fetchConfig,
10698
opts,
10799
resultData: ret,
108100
})
@@ -116,6 +108,7 @@ export class FetchComponent {
116108
return defaultfetchConfigCallbacks.processEx({
117109
id,
118110
ctx: this.ctx,
111+
config: this.fetchConfig,
119112
opts,
120113
exception: ex as Error,
121114
})
@@ -125,6 +118,7 @@ export class FetchComponent {
125118
return config.processEx({
126119
id,
127120
ctx: this.ctx,
121+
config: this.fetchConfig,
128122
opts,
129123
exception: ex as Error,
130124
})

packages/midway-component-fetch/src/lib/helper.ts

+46-10
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ const beforeRequest: FetchComponentConfig['beforeRequest'] = async (options) =>
2323
const {
2424
id,
2525
ctx,
26-
enableTraceLoggingReqBody,
27-
traceLoggingReqHeaders,
2826
opts,
2927
} = options
28+
const {
29+
enableTraceLoggingReqBody,
30+
traceLoggingReqHeaders,
31+
} = options.config
3032

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

58-
traceLoggingReqHeaders.forEach((name) => {
59-
const val = retrieveHeadersItem(opts.headers, name)
60-
if (val) {
61-
tags[`http.${name}`] = val
62-
}
63-
})
60+
if (Array.isArray(traceLoggingReqHeaders)) {
61+
traceLoggingReqHeaders.forEach((name) => {
62+
const val = retrieveHeadersItem(opts.headers, name)
63+
if (typeof val !== 'undefined') {
64+
tags[`http.${name}`] = val
65+
}
66+
})
67+
}
6468

6569
span.addTags(tags)
6670

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

7680
const afterResponse: FetchComponentConfig['afterResponse'] = async (options) => {
77-
const { id, ctx, enableTraceLoggingRespData, opts, resultData } = options
81+
const {
82+
id,
83+
ctx,
84+
opts,
85+
resultData,
86+
} = options
87+
const {
88+
enableTraceLoggingRespData,
89+
traceLoggingRespHeaders,
90+
} = options.config
91+
7892
const time = genISO8601String()
7993
const mem = humanMemoryUsage()
8094
const parentInput: SpanLogInput = {
@@ -101,6 +115,15 @@ const afterResponse: FetchComponentConfig['afterResponse'] = async (options) =>
101115
tags[TracerTag.respBody] = 'Not logged'
102116
}
103117

118+
if (Array.isArray(traceLoggingRespHeaders)) {
119+
traceLoggingRespHeaders.forEach((name) => {
120+
const val = retrieveHeadersItem(opts.headers, name)
121+
if (typeof val !== 'undefined') {
122+
tags[`http.${name}`] = val
123+
}
124+
})
125+
}
126+
104127
Object.keys(tags).length && span.addTags(tags)
105128

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

117140
export const processEx: FetchComponentConfig['processEx'] = (options) => {
118-
const { id, ctx, exception } = options
141+
const { id, ctx, opts, exception } = options
142+
const {
143+
traceLoggingRespHeaders,
144+
} = options.config
119145
const time = genISO8601String()
120146
const mem = humanMemoryUsage()
121147

@@ -135,6 +161,16 @@ export const processEx: FetchComponentConfig['processEx'] = (options) => {
135161
[TracerTag.logLevel]: 'error',
136162
[TracerTag.svcException]: exception,
137163
}
164+
165+
if (Array.isArray(traceLoggingRespHeaders)) {
166+
traceLoggingRespHeaders.forEach((name) => {
167+
const val = retrieveHeadersItem(opts.headers, name)
168+
if (typeof val !== 'undefined') {
169+
tags[`http.${name}`] = val
170+
}
171+
})
172+
}
173+
138174
span.addTags(tags)
139175

140176
const input: SpanLogInput = {

packages/midway-component-fetch/src/lib/types.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,31 @@ export interface FetchComponentConfig {
4747
* @example ['authorization', 'user-agent']
4848
*/
4949
traceLoggingReqHeaders?: string[]
50+
/**
51+
* @example ['authorization', 'user-agent', 'server']
52+
*/
53+
traceLoggingRespHeaders?: string[]
5054
}
5155

5256
export interface ReqCallbackOptions {
5357
id: symbol
5458
ctx: Context
55-
enableTraceLoggingReqBody: boolean
56-
traceLoggingReqHeaders: string[]
59+
config: FetchComponentConfig
5760
opts: Options
5861
}
5962

6063
export interface RespCallbackOptions <T = unknown> {
6164
id: symbol
6265
ctx: Context
63-
enableTraceLoggingRespData: boolean
66+
config: FetchComponentConfig
6467
opts: Options
6568
resultData: T
6669
}
6770

6871
export interface ProcessExCallbackOptions {
6972
id: symbol
7073
ctx: Context
74+
config: FetchComponentConfig
7175
opts: Options
7276
exception: Error
7377
}

0 commit comments

Comments
 (0)