chadtmiller.com/monolith
An atproto PDS
0
0
// A stub OTLP/HTTP receiver of traces (spec/OPERATIONS.md): it takes
// `ExportTraceServiceRequest`s in protobuf, as the server must send them,
// keeps every span, and can go away, as a receiver in an outage does.
import { createServer } from 'node:http'
import { gunzipSync } from 'node:zlib'
const KINDS = ['UNSPECIFIED', 'INTERNAL', 'SERVER', 'CLIENT', 'PRODUCER', 'CONSUMER']
/// Start a receiver on a port of its own. Answers `{url, spans, exports,
/// refused, mode, hang(), down(), up(), close()}`: `spans` by span id, each
/// `{traceId, spanId, parentSpanId, name, kind, start, end, attributes,
/// status, resource, scope, at}`, times in ms since the epoch; `exports`
/// each export's `{at, headers, type, encoding, spans}`; `refused` the
/// exports it could not take, with why. `hang` keeps taking connections
/// and answers none; `down` stops listening and drops every connection;
/// `up` listens again on the same port.
export async function startReceiver() {
const r = { spans: new Map(), exports: [], refused: [], mode: 'up', held: new Set() }
const sockets = new Set()
const s = createServer((req, res) => {
if (r.mode === 'hang') return r.held.add(res)
const chunks = []
req.on('data', (c) => chunks.push(c))
req.on('end', () => {
const headers = { ...req.headers }
const type = req.headers['content-type'] ?? ''
const encoding = req.headers['content-encoding'] ?? null
const refuse = (why) => {
r.refused.push({ at: Date.now(), method: req.method, path: req.url, type, encoding, why })
res.writeHead(400, { 'content-type': 'text/plain' })
res.end(why)
}
if (req.method !== 'POST' || req.url !== '/v1/traces') return refuse(`${req.method} ${req.url}: not POST /v1/traces`)
if (type.split(';')[0].trim() !== 'application/x-protobuf') return refuse(`Content-Type ${type}, not application/x-protobuf`)
let body = Buffer.concat(chunks)
try {
if (encoding === 'gzip') body = gunzipSync(body)
else if (encoding && encoding !== 'identity') return refuse(`Content-Encoding ${encoding}`)
const spans = decodeExport(body)
const at = Date.now()
for (const sp of spans) r.spans.set(sp.spanId, { ...sp, at })
r.exports.push({ at, headers, type, encoding, spans: spans.length })
} catch (err) {
return refuse(`not an ExportTraceServiceRequest: ${err.message}`)
}
res.writeHead(200, { 'content-type': 'application/x-protobuf' })
res.end()
})
})
s.on('connection', (c) => {
sockets.add(c)
c.on('close', () => sockets.delete(c))
})
await new Promise((ok) => s.listen(0, '127.0.0.1', ok))
const port = s.address().port
r.url = `http://127.0.0.1:${port}`
r.hang = () => (r.mode = 'hang')
r.down = async () => {
r.mode = 'down'
const closed = new Promise((ok) => s.close(ok))
for (const c of sockets) c.destroy()
r.held.clear()
await closed
}
r.up = async () => {
if (r.mode === 'hang') {
for (const c of sockets) c.destroy()
r.held.clear()
}
if (!s.listening) await new Promise((ok) => s.listen(port, '127.0.0.1', ok))
r.mode = 'up'
}
r.close = () => {
for (const c of sockets) c.destroy()
s.close()
}
return r
}
// --------------------------------------------- protobuf, as much as OTLP needs
function varint(buf, i) {
let v = 0n
let shift = 0n
for (;;) {
if (i >= buf.length) throw new Error('truncated varint')
const b = buf[i++]
v |= BigInt(b & 0x7f) << shift
if (!(b & 0x80)) return [v, i]
shift += 7n
}
}
/// The fields of a message: `[number, wire type, value]`, a value being a
/// BigInt for a varint and the bytes otherwise.
function fields(buf) {
const out = []
let i = 0
while (i < buf.length) {
let key
;[key, i] = varint(buf, i)
const f = Number(key >> 3n)
const w = Number(key & 7n)
let v
if (w === 0) [v, i] = varint(buf, i)
else if (w === 1) (v = buf.subarray(i, i + 8)), (i += 8)
else if (w === 5) (v = buf.subarray(i, i + 4)), (i += 4)
else if (w === 2) {
let len
;[len, i] = varint(buf, i)
v = buf.subarray(i, i + Number(len))
i += Number(len)
} else throw new Error(`wire type ${w}`)
if (i > buf.length) throw new Error('truncated field')
out.push([f, w, v])
}
return out
}
const text = (b) => Buffer.from(b).toString('utf8')
const hex = (b) => Buffer.from(b).toString('hex')
/// An AnyValue: a string, boolean, number (an int64 as a BigInt), array,
/// object or bytes.
function anyValue(buf) {
for (const [f, w, v] of fields(buf)) {
if (f === 1) return text(v)
if (f === 2) return v !== 0n
if (f === 3) return BigInt.asIntN(64, v)
if (f === 4 && w === 1) return Buffer.from(v).readDoubleLE(0)
if (f === 5) return fields(v).filter(([g]) => g === 1).map(([, , x]) => anyValue(x))
if (f === 6) return attributes(fields(v).filter(([g]) => g === 1).map(([, , x]) => x))
if (f === 7) return Buffer.from(v)
}
return null
}
/// KeyValues as an object.
function attributes(kvs) {
const out = {}
for (const kv of kvs) {
let key = ''
let value = null
for (const [f, , v] of fields(kv)) {
if (f === 1) key = text(v)
if (f === 2) value = anyValue(v)
}
out[key] = value
}
return out
}
const nanosToMs = (b) => Number(Buffer.from(b).readBigUInt64LE(0) / 1000n) / 1000
function span(buf, resource, scope) {
const sp = { traceId: '', spanId: '', parentSpanId: '', name: '', kind: 'UNSPECIFIED', start: 0, end: 0, attributes: {}, status: { code: 'UNSET', message: '' }, resource, scope }
const attrs = []
for (const [f, , v] of fields(buf)) {
if (f === 1) sp.traceId = hex(v)
else if (f === 2) sp.spanId = hex(v)
else if (f === 4) sp.parentSpanId = hex(v)
else if (f === 5) sp.name = text(v)
else if (f === 6) sp.kind = KINDS[Number(v)] ?? `kind ${v}`
else if (f === 7) sp.start = nanosToMs(v)
else if (f === 8) sp.end = nanosToMs(v)
else if (f === 9) attrs.push(v)
else if (f === 15)
for (const [g, , x] of fields(v)) {
if (g === 2) sp.status.message = text(x)
if (g === 3) sp.status.code = ['UNSET', 'OK', 'ERROR'][Number(x)] ?? `code ${x}`
}
}
sp.attributes = attributes(attrs)
return sp
}
/// The spans of an ExportTraceServiceRequest.
export function decodeExport(buf) {
const spans = []
for (const [f, , rs] of fields(buf)) {
if (f !== 1) continue
let resource = {}
const scopeSpans = []
for (const [g, , v] of fields(rs)) {
if (g === 1) resource = attributes(fields(v).filter(([h]) => h === 1).map(([, , x]) => x))
if (g === 2) scopeSpans.push(v)
}
for (const ss of scopeSpans) {
let scope = ''
const raw = []
for (const [g, , v] of fields(ss)) {
if (g === 1) for (const [h, , x] of fields(v)) if (h === 1) scope = text(x)
if (g === 2) raw.push(v)
}
for (const b of raw) spans.push(span(b, resource, scope))
}
}
return spans
}