|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { ERROR_MESSAGE, ERROR_STACK, ERROR_TYPE, IGNORE_OTEL_ERROR } = require('../constants') |
| 4 | +const DatadogSpanContext = require('../opentracing/span_context') |
| 5 | +const TraceState = require('../opentracing/propagation/tracestate') |
| 6 | + |
| 7 | +const id = require('../id') |
| 8 | + |
| 9 | +/** |
| 10 | + * @typedef {{ toTraceId: (get128?: boolean) => string, toSpanId: (get128?: boolean) => string }} DatadogContextLike |
| 11 | + * @typedef {{ _ddContext: import('../opentracing/span_context') }} OtelBridgeSpanContextLike |
| 12 | + * @typedef {{ |
| 13 | + * traceId: string, |
| 14 | + * spanId: string, |
| 15 | + * traceFlags?: number, |
| 16 | + * traceState?: { serialize: () => string } |
| 17 | + * }} OtelSpanContextLike |
| 18 | + * @typedef {DatadogContextLike | OtelBridgeSpanContextLike | OtelSpanContextLike} LinkContextLike |
| 19 | + * @typedef {{ context: LinkContextLike, attributes?: Record<string, unknown> }} OtelLink |
| 20 | + * @typedef {{ |
| 21 | + * name?: string, |
| 22 | + * message?: string, |
| 23 | + * stack?: string, |
| 24 | + * type?: string, |
| 25 | + * escaped?: unknown |
| 26 | + * }} ExceptionLike |
| 27 | + * @typedef {{ |
| 28 | + * addEvent: (name: string, attributes: Record<string, unknown>, timeInput?: unknown) => unknown |
| 29 | + * }} EventTarget |
| 30 | + */ |
| 31 | + |
| 32 | +/** |
| 33 | + * Normalize any Datadog/OTel span-context shape to a `DatadogSpanContext`. |
| 34 | + * |
| 35 | + * @param {LinkContextLike | undefined | null} context |
| 36 | + * @returns {import('../opentracing/span_context') | undefined} |
| 37 | + */ |
| 38 | +function normalizeLinkContext (context) { |
| 39 | + if (!context) return |
| 40 | + |
| 41 | + const bridgeCtx = /** @type {OtelBridgeSpanContextLike} */ (context) |
| 42 | + if (bridgeCtx._ddContext) return bridgeCtx._ddContext |
| 43 | + |
| 44 | + const ddCtx = /** @type {DatadogContextLike} */ (context) |
| 45 | + if (typeof ddCtx.toTraceId === 'function' && typeof ddCtx.toSpanId === 'function') { |
| 46 | + return /** @type {import('../opentracing/span_context')} */ (/** @type {unknown} */ (context)) |
| 47 | + } |
| 48 | + |
| 49 | + const otelCtx = /** @type {OtelSpanContextLike} */ (context) |
| 50 | + if (typeof otelCtx.traceId !== 'string' || typeof otelCtx.spanId !== 'string') return |
| 51 | + |
| 52 | + let sampling |
| 53 | + if (typeof otelCtx.traceFlags === 'number') { |
| 54 | + sampling = { priority: otelCtx.traceFlags & 1 } |
| 55 | + } |
| 56 | + |
| 57 | + let tracestate |
| 58 | + if (otelCtx.traceState?.serialize) { |
| 59 | + tracestate = TraceState.fromString(otelCtx.traceState.serialize()) |
| 60 | + } |
| 61 | + |
| 62 | + return new DatadogSpanContext({ |
| 63 | + traceId: id(otelCtx.traceId, 16), |
| 64 | + spanId: id(otelCtx.spanId, 16), |
| 65 | + sampling, |
| 66 | + tracestate, |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * @param {import('../opentracing/span')} ddSpan |
| 72 | + * @param {string} key |
| 73 | + * @param {unknown} value |
| 74 | + * @returns {void} |
| 75 | + */ |
| 76 | +function setOtelAttribute (ddSpan, key, value) { |
| 77 | + if (key === 'http.response.status_code') { |
| 78 | + ddSpan.setTag('http.status_code', String(value)) |
| 79 | + } |
| 80 | + |
| 81 | + ddSpan.setTag(key, value) |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * @param {import('../opentracing/span')} ddSpan |
| 86 | + * @param {Record<string, unknown>} attributes |
| 87 | + * @returns {void} |
| 88 | + */ |
| 89 | +function setOtelAttributes (ddSpan, attributes) { |
| 90 | + if ('http.response.status_code' in attributes) { |
| 91 | + attributes['http.status_code'] = String(attributes['http.response.status_code']) |
| 92 | + } |
| 93 | + |
| 94 | + ddSpan.addTags(attributes) |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Accepts both `{ context, attributes }` and the deprecated `(context, attrs)` form. |
| 99 | + * |
| 100 | + * @param {import('../opentracing/span')} ddSpan |
| 101 | + * @param {LinkContextLike | OtelLink} link |
| 102 | + * @param {Record<string, unknown>} [attrs] |
| 103 | + * @returns {void} |
| 104 | + */ |
| 105 | +function addOtelLink (ddSpan, link, attrs) { |
| 106 | + // TODO: Drop the (context, attrs) form in v6.0.0. |
| 107 | + const linkObj = link && typeof link === 'object' && 'context' in link |
| 108 | + ? /** @type {OtelLink} */ (link) |
| 109 | + : { context: /** @type {LinkContextLike} */ (link), attributes: attrs ?? {} } |
| 110 | + |
| 111 | + const ddSpanContext = normalizeLinkContext(linkObj.context) |
| 112 | + if (!ddSpanContext) return |
| 113 | + |
| 114 | + ddSpan.addLink({ context: ddSpanContext, attributes: linkObj.attributes }) |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * @param {import('../opentracing/span')} ddSpan |
| 119 | + * @param {EventTarget} eventTarget |
| 120 | + * @param {ExceptionLike} exception |
| 121 | + * @param {unknown} [timeInput] |
| 122 | + * @returns {void} |
| 123 | + */ |
| 124 | +function recordException (ddSpan, eventTarget, exception, timeInput) { |
| 125 | + ddSpan.addTags({ |
| 126 | + [ERROR_TYPE]: exception.name, |
| 127 | + [ERROR_MESSAGE]: exception.message, |
| 128 | + [ERROR_STACK]: exception.stack, |
| 129 | + [IGNORE_OTEL_ERROR]: ddSpan.context()._tags[IGNORE_OTEL_ERROR] ?? true, |
| 130 | + }) |
| 131 | + |
| 132 | + /** @type {Record<string, unknown>} */ |
| 133 | + const attributes = {} |
| 134 | + if (exception.message) attributes['exception.message'] = exception.message |
| 135 | + if (exception.type) attributes['exception.type'] = exception.type |
| 136 | + if (exception.escaped) attributes['exception.escaped'] = exception.escaped |
| 137 | + if (exception.stack) attributes['exception.stacktrace'] = exception.stack |
| 138 | + |
| 139 | + eventTarget.addEvent(exception.name ?? 'Error', attributes, timeInput) |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * First-call-wins; no-op on ended spans. Only `code === 2` emits Datadog error tags. |
| 144 | + * |
| 145 | + * @param {import('../opentracing/span')} ddSpan |
| 146 | + * @param {{ ended: boolean, _hasStatus: boolean }} bridgeSpan |
| 147 | + * @param {{ code?: number, message?: string }} [status] |
| 148 | + * @returns {void} |
| 149 | + */ |
| 150 | +function setStatus (ddSpan, bridgeSpan, { code, message } = {}) { |
| 151 | + if (bridgeSpan.ended || bridgeSpan._hasStatus || !code) return |
| 152 | + |
| 153 | + bridgeSpan._hasStatus = true |
| 154 | + |
| 155 | + if (code === 2) { |
| 156 | + ddSpan.addTags({ |
| 157 | + [ERROR_MESSAGE]: message, |
| 158 | + [IGNORE_OTEL_ERROR]: false, |
| 159 | + }) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +module.exports = { |
| 164 | + addOtelLink, |
| 165 | + normalizeLinkContext, |
| 166 | + recordException, |
| 167 | + setOtelAttribute, |
| 168 | + setOtelAttributes, |
| 169 | + setStatus, |
| 170 | +} |
0 commit comments