|
| 1 | +import express from 'express' |
| 2 | +import zlib from 'zlib' |
| 3 | + |
| 4 | +import { |
| 5 | + IEvent, |
| 6 | + IExportTraceServiceRequest, |
| 7 | + IResourceSpans, |
| 8 | + ISpan, |
| 9 | +} from '@opentelemetry/otlp-transformer/build/esm/trace/internal-types' |
| 10 | + |
| 11 | +const DEFAULT_PORT = 3101 |
| 12 | +const RESOURCE_SPANS_BY_PORT = new Map<number, IResourceSpans[]>() |
| 13 | +const shouldStart = !!process.argv.find((arg) => arg.includes('--start')) |
| 14 | + |
| 15 | +if (shouldStart) { |
| 16 | + const portFlag = |
| 17 | + process.argv.find((arg) => arg.includes('--port')) || |
| 18 | + `--port=${DEFAULT_PORT}` |
| 19 | + const port = +portFlag.split('=')[1] |
| 20 | + |
| 21 | + startMockOtelServer({ port }) |
| 22 | +} |
| 23 | + |
| 24 | +export function startMockOtelServer({ |
| 25 | + port = DEFAULT_PORT, |
| 26 | +}: { |
| 27 | + port?: number |
| 28 | +}) { |
| 29 | + const app = express() |
| 30 | + app.use(unzipBody) |
| 31 | + app.use((req, res, next) => { |
| 32 | + try { |
| 33 | + const trace = JSON.parse( |
| 34 | + req.body.toString(), |
| 35 | + ) as IExportTraceServiceRequest |
| 36 | + |
| 37 | + if (trace.resourceSpans) { |
| 38 | + const resourceSpans = getResourceSpansByPort(port) |
| 39 | + const spanNames = trace.resourceSpans.flatMap( |
| 40 | + (resourceSpan) => |
| 41 | + resourceSpan.scopeSpans.flatMap((scopeSpan) => |
| 42 | + scopeSpan.spans?.flatMap((span) => span.name), |
| 43 | + ) ?? [], |
| 44 | + ) |
| 45 | + const spanAttributes = [ |
| 46 | + ...trace.resourceSpans.flatMap( |
| 47 | + (resourceSpan) => |
| 48 | + resourceSpan.resource?.attributes.flatMap( |
| 49 | + (attr) => attr.key, |
| 50 | + ) ?? [], |
| 51 | + ), |
| 52 | + ...trace.resourceSpans.flatMap( |
| 53 | + (resourceSpan) => |
| 54 | + resourceSpan.scopeSpans.flatMap( |
| 55 | + (span) => span.scope?.attributes, |
| 56 | + ) ?? [], |
| 57 | + ), |
| 58 | + ...trace.resourceSpans.flatMap( |
| 59 | + (resourceSpan) => |
| 60 | + resourceSpan.scopeSpans.flatMap((span) => |
| 61 | + span.spans?.flatMap((s) => s.attributes), |
| 62 | + ) ?? [], |
| 63 | + ), |
| 64 | + ] |
| 65 | + console.log({ trace, spanNames, spanAttributes }) |
| 66 | + |
| 67 | + resourceSpans.push(...trace.resourceSpans) |
| 68 | + |
| 69 | + setResourceSpansByPort(port, resourceSpans) |
| 70 | + } |
| 71 | + } catch (error) { |
| 72 | + console.error(error) |
| 73 | + } |
| 74 | + next() |
| 75 | + }) |
| 76 | + |
| 77 | + app.post('/v1/traces', (req, res) => { |
| 78 | + res.status(200).send('OK') |
| 79 | + }) |
| 80 | + |
| 81 | + const server = app.listen(port, () => { |
| 82 | + console.info(`Server is running on ${port}`) |
| 83 | + }) |
| 84 | + |
| 85 | + return () => server.close() |
| 86 | +} |
| 87 | + |
| 88 | +export function getResourceSpans(port = DEFAULT_PORT, names: string[] = []) { |
| 89 | + return new Promise<{ |
| 90 | + details: ReturnType<typeof aggregateAttributes> |
| 91 | + resourceSpans: IResourceSpans[] |
| 92 | + }>((resolve, reject) => { |
| 93 | + const interval = setInterval(() => { |
| 94 | + const resourceSpans = getResourceSpansByPort(port) |
| 95 | + const details = aggregateAttributes(resourceSpans) |
| 96 | + const hasSessionId = details.some( |
| 97 | + (detail) => detail.attributes['highlight.session_id'], |
| 98 | + ) |
| 99 | + const hasName = |
| 100 | + !names.length || |
| 101 | + details |
| 102 | + .flatMap((detail) => detail.events) |
| 103 | + .some((event) => names.includes(event.name)) |
| 104 | + |
| 105 | + if (hasSessionId && hasName) { |
| 106 | + clearInterval(interval) |
| 107 | + |
| 108 | + resolve({ details, resourceSpans }) |
| 109 | + } |
| 110 | + }, 250) |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +export function clearResourceSpans(port = DEFAULT_PORT) { |
| 115 | + setResourceSpansByPort(port, []) |
| 116 | +} |
| 117 | + |
| 118 | +function unzipBody( |
| 119 | + req: express.Request, |
| 120 | + res: express.Response, |
| 121 | + next: express.NextFunction, |
| 122 | +) { |
| 123 | + if (req.headers['content-encoding'] === 'gzip') { |
| 124 | + const unzip = zlib.createGunzip() |
| 125 | + const stream = req.pipe(unzip) |
| 126 | + const buffers: Buffer[] = [] |
| 127 | + |
| 128 | + stream.on('data', (chunk) => { |
| 129 | + buffers.push(chunk) |
| 130 | + }) |
| 131 | + |
| 132 | + stream.on('end', () => { |
| 133 | + req.body = Buffer.concat(buffers) |
| 134 | + next() |
| 135 | + }) |
| 136 | + } else { |
| 137 | + next() |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +function aggregateAttributes(resourceSpans: IResourceSpans[]) { |
| 142 | + const aggregatedAttributes: { |
| 143 | + spanNames: string[] |
| 144 | + events: IEvent[] |
| 145 | + attributes: Record<string, string> |
| 146 | + }[] = [] |
| 147 | + |
| 148 | + resourceSpans?.forEach((resourceSpan) => { |
| 149 | + const filteredSpans = resourceSpan.scopeSpans.flatMap( |
| 150 | + (scopeSpan) => scopeSpan.spans, |
| 151 | + ) as ISpan[] |
| 152 | + const spanNames = filteredSpans.map((span) => span.name) |
| 153 | + |
| 154 | + const resourceAttributes = |
| 155 | + resourceSpan.resource?.attributes.reduce<Record<string, string>>( |
| 156 | + (acc, attribute) => { |
| 157 | + acc[attribute.key] = |
| 158 | + attribute.value.stringValue || |
| 159 | + attribute.value.boolValue?.toString() || |
| 160 | + attribute.value.intValue?.toString() || |
| 161 | + attribute.value.arrayValue?.toString() || |
| 162 | + '' |
| 163 | + |
| 164 | + return acc |
| 165 | + }, |
| 166 | + {}, |
| 167 | + ) || {} |
| 168 | + const spanAttributes = filteredSpans |
| 169 | + .flatMap((span) => span.attributes) |
| 170 | + .reduce<Record<string, string>>((acc, attribute) => { |
| 171 | + acc[attribute.key] = |
| 172 | + attribute.value.stringValue || |
| 173 | + attribute.value.boolValue?.toString() || |
| 174 | + attribute.value.intValue?.toString() || |
| 175 | + attribute.value.arrayValue?.toString() || |
| 176 | + '' |
| 177 | + |
| 178 | + return acc |
| 179 | + }, resourceAttributes) |
| 180 | + const events = filteredSpans.flatMap((span) => span.events) as IEvent[] |
| 181 | + const attributes = events.reduce<Record<string, string>>( |
| 182 | + (acc, event) => { |
| 183 | + event.attributes.forEach((attribute) => { |
| 184 | + acc[attribute.key] = |
| 185 | + attribute.value.stringValue || |
| 186 | + attribute.value.boolValue?.toString() || |
| 187 | + attribute.value.intValue?.toString() || |
| 188 | + attribute.value.arrayValue?.toString() || |
| 189 | + '' |
| 190 | + }) |
| 191 | + |
| 192 | + return acc |
| 193 | + }, |
| 194 | + spanAttributes, |
| 195 | + ) |
| 196 | + |
| 197 | + aggregatedAttributes.push({ |
| 198 | + spanNames, |
| 199 | + attributes, |
| 200 | + events, |
| 201 | + }) |
| 202 | + }) |
| 203 | + |
| 204 | + return aggregatedAttributes |
| 205 | +} |
| 206 | + |
| 207 | +function getResourceSpansByPort(port = DEFAULT_PORT) { |
| 208 | + return RESOURCE_SPANS_BY_PORT.get(port) || [] |
| 209 | +} |
| 210 | + |
| 211 | +function setResourceSpansByPort(port = DEFAULT_PORT, resourceSpans: any[]) { |
| 212 | + RESOURCE_SPANS_BY_PORT.set(port, resourceSpans) |
| 213 | +} |
| 214 | + |
| 215 | +export function getOtlpEndpoint(port = DEFAULT_PORT) { |
| 216 | + return `http://127.0.0.1:${port}` |
| 217 | +} |
| 218 | + |
| 219 | +export function filterEventsByName( |
| 220 | + details: ReturnType<typeof aggregateAttributes>, |
| 221 | + name: string, |
| 222 | +) { |
| 223 | + const events = details.flatMap((detail) => detail.events) |
| 224 | + |
| 225 | + return events.filter((event) => event.name === name) |
| 226 | +} |
| 227 | + |
| 228 | +export function filterDetailsBySessionId( |
| 229 | + details: ReturnType<typeof aggregateAttributes>, |
| 230 | + sessionId: string, |
| 231 | +) { |
| 232 | + return details.filter( |
| 233 | + (detail) => detail.attributes['highlight.session_id'] === sessionId, |
| 234 | + ) |
| 235 | +} |
| 236 | + |
| 237 | +export function logDetails( |
| 238 | + details: Awaited<ReturnType<typeof getResourceSpans>>['details'], |
| 239 | +) { |
| 240 | + console.info(details.flatMap((d) => d.spanNames)) |
| 241 | + console.info(details.flatMap((d) => d.events.map((e) => e.name))) |
| 242 | +} |
0 commit comments