|
7 | 7 | Attributes, |
8 | 8 | Context, |
9 | 9 | Span, |
| 10 | + Exception, |
| 11 | + SpanStatusCode, |
10 | 12 | } from '@opentelemetry/api' |
11 | 13 | import { getActiveConfig } from '../config.js' |
12 | 14 | import { wrap } from '../wrap.js' |
@@ -180,21 +182,28 @@ export function instrumentClientFetch( |
180 | 182 | const method = request.method.toUpperCase() |
181 | 183 | const spanName = typeof attrs?.['name'] === 'string' ? attrs?.['name'] : `fetch ${method} ${host}` |
182 | 184 | const promise = tracer.startActiveSpan(spanName, options, async (span) => { |
183 | | - const includeTraceContext = |
184 | | - typeof config.includeTraceContext === 'function' |
185 | | - ? config.includeTraceContext(request) |
186 | | - : config.includeTraceContext |
187 | | - if (includeTraceContext ?? true) { |
188 | | - propagation.inject(api_context.active(), request.headers, { |
189 | | - set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)), |
190 | | - }) |
| 185 | + try { |
| 186 | + const includeTraceContext = |
| 187 | + typeof config.includeTraceContext === 'function' |
| 188 | + ? config.includeTraceContext(request) |
| 189 | + : config.includeTraceContext |
| 190 | + if (includeTraceContext ?? true) { |
| 191 | + propagation.inject(api_context.active(), request.headers, { |
| 192 | + set: (h, k, v) => h.set(k, typeof v === 'string' ? v : String(v)), |
| 193 | + }) |
| 194 | + } |
| 195 | + span.setAttributes(gatherRequestAttributes(request)) |
| 196 | + if (request.cf) span.setAttributes(gatherOutgoingCfAttributes(request.cf)) |
| 197 | + const response = await Reflect.apply(target, thisArg, [request]) |
| 198 | + span.setAttributes(gatherResponseAttributes(response)) |
| 199 | + return response |
| 200 | + } catch (error: unknown) { |
| 201 | + span.recordException(error as Exception) |
| 202 | + span.setStatus({ code: SpanStatusCode.ERROR }) |
| 203 | + throw error |
| 204 | + } finally { |
| 205 | + span.end() |
191 | 206 | } |
192 | | - span.setAttributes(gatherRequestAttributes(request)) |
193 | | - if (request.cf) span.setAttributes(gatherOutgoingCfAttributes(request.cf)) |
194 | | - const response = await Reflect.apply(target, thisArg, [request]) |
195 | | - span.setAttributes(gatherResponseAttributes(response)) |
196 | | - span.end() |
197 | | - return response |
198 | 207 | }) |
199 | 208 | return promise |
200 | 209 | }, |
|
0 commit comments