|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const log = require('../log') |
| 4 | +const { truncateSpan, normalizeSpan } = require('./tags-processors') |
| 5 | + |
| 6 | +/** |
| 7 | + * Formats a span for JSON encoding. |
| 8 | + * @param {object} span - The span to format |
| 9 | + * @returns {object} The formatted span |
| 10 | + */ |
| 11 | +function formatSpan (span) { |
| 12 | + span = normalizeSpan(truncateSpan(span, false)) |
| 13 | + |
| 14 | + if (span.span_events) { |
| 15 | + span.meta.events = JSON.stringify(span.span_events) |
| 16 | + delete span.span_events |
| 17 | + } |
| 18 | + |
| 19 | + return span |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Converts a span to JSON-serializable format. |
| 24 | + * IDs are converted to lowercase hex strings. Start time is converted from |
| 25 | + * nanoseconds to seconds for the intake format. |
| 26 | + * @param {object} span - The formatted span |
| 27 | + * @returns {object} JSON-serializable span object |
| 28 | + */ |
| 29 | +function spanToJSON (span) { |
| 30 | + const result = { |
| 31 | + trace_id: span.trace_id.toString(16).toLowerCase(), |
| 32 | + span_id: span.span_id.toString(16).toLowerCase(), |
| 33 | + parent_id: span.parent_id.toString(16).toLowerCase(), |
| 34 | + name: span.name, |
| 35 | + resource: span.resource, |
| 36 | + service: span.service, |
| 37 | + error: span.error, |
| 38 | + start: Math.floor(span.start / 1e9), |
| 39 | + duration: span.duration, |
| 40 | + meta: span.meta, |
| 41 | + metrics: span.metrics, |
| 42 | + } |
| 43 | + |
| 44 | + if (span.type) { |
| 45 | + result.type = span.type |
| 46 | + } |
| 47 | + |
| 48 | + if (span.meta_struct) { |
| 49 | + result.meta_struct = span.meta_struct |
| 50 | + } |
| 51 | + |
| 52 | + if (span.links && span.links.length > 0) { |
| 53 | + result.links = span.links |
| 54 | + } |
| 55 | + |
| 56 | + return result |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * JSON encoder for agentless span intake. |
| 61 | + * Encodes a single trace as JSON with the payload format: {"spans": [...]} |
| 62 | + * |
| 63 | + * This encoder handles one trace at a time since each trace must be sent as a |
| 64 | + * separate request to the intake. -- bengl |
| 65 | + */ |
| 66 | +class AgentlessJSONEncoder { |
| 67 | + constructor () { |
| 68 | + this._reset() |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the number of spans encoded. |
| 73 | + * @returns {number} |
| 74 | + */ |
| 75 | + count () { |
| 76 | + return this._spanCount |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Encodes a trace (array of spans) into the buffer. |
| 81 | + * @param {object[]} trace - Array of spans to encode |
| 82 | + */ |
| 83 | + encode (trace) { |
| 84 | + for (const span of trace) { |
| 85 | + try { |
| 86 | + const formattedSpan = formatSpan(span) |
| 87 | + const jsonSpan = spanToJSON(formattedSpan) |
| 88 | + |
| 89 | + this._spans.push(jsonSpan) |
| 90 | + this._spanCount++ |
| 91 | + } catch (err) { |
| 92 | + log.error( |
| 93 | + 'Failed to encode span (name: %s, service: %s). Span will be dropped. Error: %s\n%s', |
| 94 | + span?.name || 'unknown', |
| 95 | + span?.service || 'unknown', |
| 96 | + err.message, |
| 97 | + err.stack |
| 98 | + ) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Creates the JSON payload for the encoded trace. |
| 105 | + * @returns {Buffer} JSON payload as a buffer, or empty buffer if no spans |
| 106 | + */ |
| 107 | + makePayload () { |
| 108 | + if (this._spans.length === 0) { |
| 109 | + this._reset() |
| 110 | + return Buffer.alloc(0) |
| 111 | + } |
| 112 | + |
| 113 | + try { |
| 114 | + const payload = JSON.stringify({ spans: this._spans }) |
| 115 | + this._reset() |
| 116 | + return Buffer.from(payload, 'utf8') |
| 117 | + } catch (err) { |
| 118 | + log.error( |
| 119 | + 'Failed to encode trace as JSON (%d spans). Trace will be dropped. Error: %s', |
| 120 | + this._spans.length, |
| 121 | + err.message |
| 122 | + ) |
| 123 | + this._reset() |
| 124 | + return Buffer.alloc(0) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Resets the encoder state. |
| 130 | + */ |
| 131 | + reset () { |
| 132 | + this._reset() |
| 133 | + } |
| 134 | + |
| 135 | + _reset () { |
| 136 | + this._spans = [] |
| 137 | + this._spanCount = 0 |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +module.exports = { AgentlessJSONEncoder } |
0 commit comments