MCPcopy
hub / github.com/midwayjs/midway / bindTraceContext

Method bindTraceContext

packages/consul/src/manager.ts:66–128  ·  view source on GitHub ↗
(client: any, clientName: string)

Source from the content-addressed store, hash-verified

64 }
65
66 protected bindTraceContext(client: any, clientName: string) {
67 if (!client || !this.traceService) {
68 return;
69 }
70
71 const rawRequest = client.request?.bind(client);
72 if (!rawRequest) {
73 return;
74 }
75
76 client.request = (...args) => {
77 const hasCallback = args.some(arg => typeof arg === 'function');
78 if (hasCallback) {
79 return rawRequest(...args);
80 }
81
82 const requestOptions = args?.[0] ?? {};
83 const isWatch = Boolean(
84 requestOptions?.watch ??
85 requestOptions?.qs?.watch ??
86 requestOptions?.query?.watch
87 );
88 if (isWatch) {
89 return rawRequest(...args);
90 }
91
92 const requestMethod = requestOptions?.method ?? 'request';
93 const rawCarrier =
94 typeof this.traceInjector === 'function'
95 ? this.traceInjector({
96 request: requestOptions,
97 custom: {
98 clientName,
99 requestMethod: String(requestMethod),
100 },
101 })
102 : {};
103 const carrier =
104 rawCarrier && typeof rawCarrier === 'object' ? rawCarrier : {};
105 return this.traceService.runWithExitSpan(
106 `consul.${String(requestMethod).toLowerCase()}`,
107 {
108 enable: this.traceEnabled !== false,
109 carrier,
110 attributes: {
111 'midway.protocol': 'consul',
112 'midway.consul.client': clientName,
113 'midway.consul.method': String(requestMethod),
114 },
115 meta: this.traceMetaResolver,
116 metaArgs: {
117 carrier,
118 request: requestOptions,
119 custom: {
120 clientName,
121 requestMethod: String(requestMethod),
122 },
123 },

Callers 2

createClientMethod · 0.95
trace.test.tsFile · 0.45

Calls 2

runWithExitSpanMethod · 0.80
bindMethod · 0.65

Tested by

no test coverage detected