Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ const copied = Profile.decode(profile.encode())
console.log(copied)
```

The pprof decoder returns negative `int64` values as negative `bigint` values.
It does not change the representation of unsigned fields or positive values.

## OpenTelemetry profiles

The `pprof-format/otel` entry point encodes and decodes the OpenTelemetry
Expand Down
37 changes: 37 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,43 @@ test('Protobufjs compat', async (t) => {
})
})

test('decodes signed pprof integers without changing unsigned fields', () => {
const negativeLimit = -(2n ** 63n)
const positiveLimit = 2n ** 63n - 1n
const unsignedLimit = 2n ** 64n - 1n
const timeNanos = 1_700_000_000_000_000_001n
const stringTable = new StringTable()
const dropFrames = stringTable.dedup('internal')
const keepFrames = stringTable.dedup('application')
const value = [negativeLimit, -1n, 0, 2 ** 28 - 1, 2n ** 28n, positiveLimit]
const profile = new Profile({
sample: [{ value, locationId: [unsignedLimit], label: [{ num: -1 }] }],
location: [{ id: unsignedLimit, address: unsignedLimit, line: [{ line: -2 }] }],
timeNanos,
stringTable,
dropFrames,
keepFrames,
})
const decoded = Profile.decode(profile.encode())
assert.deepStrictEqual(decoded.sample[0].value, value)
assert.deepStrictEqual(decoded.sample[0].locationId, [unsignedLimit])
assert.strictEqual(decoded.sample[0].label[0].num, -1n)
assert.strictEqual(decoded.location[0].address, unsignedLimit)
assert.strictEqual(decoded.location[0].line[0].line, -2n)
assert.strictEqual(decoded.timeNanos, timeNanos)
assert.strictEqual(decoded.dropFrames, dropFrames)
assert.strictEqual(decoded.keepFrames, keepFrames)
assert.strictEqual(Profile.decode(new Profile({ timeNanos: negativeLimit }).encode()).timeNanos, negativeLimit)
assert.deepStrictEqual(decoded.encode(), profile.encode())
})

test('decodes signed values in both packed and unpacked sample fields', () => {
const packed = new Sample({ value: [-1] }).encode()
const unpacked = hexToBuf('10ffffffffffffffffff01')
const mixed = Uint8Array.from([0x10, 1, ...packed, ...unpacked, 0x10, 2])
assert.deepStrictEqual(Sample.decode(mixed).value, [1, -1n, -1n, 2])
})

test('non-packed repeated fields', async (t: TestContext) => {
// Go's runtime/pprof encodes Sample.value as non-packed repeated int64 (one
// field occurrence per value). The decoder must append rather than overwrite
Expand Down
48 changes: 25 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
Message,
Numeric,
decodeFields,
decodeInt64,
decodeInt64s,
decodeNumber,
decodeNumbers,
encodeBoolField,
Expand Down Expand Up @@ -145,10 +147,10 @@ export class ValueType extends Message {
static decodeValue(data: ValueTypeInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) {
switch (field) {
case ValueTypeField.type:
if (isVarInt(wireType)) data.type = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.type = decodeInt64(buffer, start, end)
break
case ValueTypeField.unit:
if (isVarInt(wireType)) data.unit = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.unit = decodeInt64(buffer, start, end)
break
}
}
Expand Down Expand Up @@ -203,16 +205,16 @@ export class Label extends Message {
static decodeValue(data: LabelInput, field: number, buffer: Uint8Array, start = 0, end = buffer.length, wireType?: number) {
switch (field) {
case LabelField.key:
if (isVarInt(wireType)) data.key = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.key = decodeInt64(buffer, start, end)
break
case LabelField.str:
if (isVarInt(wireType)) data.str = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.str = decodeInt64(buffer, start, end)
break
case LabelField.num:
if (isVarInt(wireType)) data.num = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.num = decodeInt64(buffer, start, end)
break
case LabelField.numUnit:
if (isVarInt(wireType)) data.numUnit = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.numUnit = decodeInt64(buffer, start, end)
break
}
}
Expand Down Expand Up @@ -265,7 +267,7 @@ export class Sample extends Message {
if (isPackedOrVarInt(wireType)) data.locationId = pushAll(decodeNumbers(buffer, start, end), data.locationId)
break
case SampleField.value:
if (isPackedOrVarInt(wireType)) data.value = pushAll(decodeNumbers(buffer, start, end), data.value)
if (isPackedOrVarInt(wireType)) data.value = pushAll(decodeInt64s(buffer, start, end), data.value)
break
case SampleField.label:
if (isLengthDelim(wireType)) data.label = push(Label.decode(buffer, start, end), data.label)
Expand Down Expand Up @@ -365,10 +367,10 @@ export class Mapping extends Message {
if (isVarInt(wireType)) data.fileOffset = decodeNumber(buffer, start, end)
break
case MappingField.filename:
if (isVarInt(wireType)) data.filename = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.filename = decodeInt64(buffer, start, end)
break
case MappingField.buildId:
if (isVarInt(wireType)) data.buildId = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.buildId = decodeInt64(buffer, start, end)
break
case MappingField.hasFunctions:
if (isVarInt(wireType)) data.hasFunctions = !!decodeNumber(buffer, start, end)
Expand Down Expand Up @@ -433,10 +435,10 @@ export class Line extends Message {
if (isVarInt(wireType)) data.functionId = decodeNumber(buffer, start, end)
break
case LineField.line:
if (isVarInt(wireType)) data.line = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.line = decodeInt64(buffer, start, end)
break
case LineField.column:
if (isVarInt(wireType)) data.column = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.column = decodeInt64(buffer, start, end)
break
}
}
Expand Down Expand Up @@ -571,16 +573,16 @@ export class Function extends Message {
if (isVarInt(wireType)) data.id = decodeNumber(buffer, start, end)
break
case FunctionField.name:
if (isVarInt(wireType)) data.name = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.name = decodeInt64(buffer, start, end)
break
case FunctionField.systemName:
if (isVarInt(wireType)) data.systemName = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.systemName = decodeInt64(buffer, start, end)
break
case FunctionField.filename:
if (isVarInt(wireType)) data.filename = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.filename = decodeInt64(buffer, start, end)
break
case FunctionField.startLine:
if (isVarInt(wireType)) data.startLine = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.startLine = decodeInt64(buffer, start, end)
break
}
}
Expand Down Expand Up @@ -767,16 +769,16 @@ export class Profile extends Message {
break
}
case ProfileField.dropFrames:
if (isVarInt(wireType)) data.dropFrames = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.dropFrames = decodeInt64(buffer, start, end)
break
case ProfileField.keepFrames:
if (isVarInt(wireType)) data.keepFrames = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.keepFrames = decodeInt64(buffer, start, end)
break
case ProfileField.timeNanos:
if (isVarInt(wireType)) data.timeNanos = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.timeNanos = decodeInt64(buffer, start, end)
break
case ProfileField.durationNanos:
if (isVarInt(wireType)) data.durationNanos = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.durationNanos = decodeInt64(buffer, start, end)
break
case ProfileField.periodType:
// Occurrences of a singular message field are merged:
Expand All @@ -786,16 +788,16 @@ export class Profile extends Message {
}
break
case ProfileField.period:
if (isVarInt(wireType)) data.period = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.period = decodeInt64(buffer, start, end)
break
case ProfileField.comment:
if (isPackedOrVarInt(wireType)) data.comment = pushAll(decodeNumbers(buffer, start, end), data.comment)
if (isPackedOrVarInt(wireType)) data.comment = pushAll(decodeInt64s(buffer, start, end), data.comment)
break
case ProfileField.defaultSampleType:
if (isVarInt(wireType)) data.defaultSampleType = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.defaultSampleType = decodeInt64(buffer, start, end)
break
case ProfileField.docUrl:
if (isVarInt(wireType)) data.docUrl = decodeNumber(buffer, start, end)
if (isVarInt(wireType)) data.docUrl = decodeInt64(buffer, start, end)
break
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/otel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
decodeFields,
decodeFixed64,
decodeFixed64s,
decodeInt64,
decodeInt64s,
decodeNumber,
decodeNumbers,
decodeString,
Expand Down Expand Up @@ -56,7 +58,6 @@ import {
prepareBuffer,
push,
pushAll,
toSigned,
verifyEncodedLength,
yieldToEventLoop,
} from './protobuf.js'
Expand Down Expand Up @@ -98,14 +99,6 @@ function decodeUint32(buffer: Uint8Array, start: number, end: number): number {
return toUint32(decodeNumber(buffer, start, end))
}

function decodeInt64(buffer: Uint8Array, start: number, end: number): Numeric {
return toSigned(decodeNumber(buffer, start, end))
}

function decodeInt64s(buffer: Uint8Array, start: number, end: number): Numeric[] {
return decodeNumbers(buffer, start, end).map(toSigned)
}

// Copy into a plain Uint8Array so decoded messages don't retain the input.
function decodeBytes(buffer: Uint8Array, start: number, end: number): Uint8Array {
return new Uint8Array(buffer.subarray(start, end))
Expand Down
22 changes: 18 additions & 4 deletions src/protobuf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,27 @@ export function decodeNumber(buffer: Uint8Array, start = 0, end = buffer.length)
}

export function decodeNumbers(buffer: Uint8Array, from = 0, to = buffer.length): Array<Numeric> {
return decodeVarints(buffer, from, to, false)
}

export function decodeInt64(buffer: Uint8Array, start: number, end: number): Numeric {
if (end - start > 4) return toSigned(decodeBigNumber(buffer, start))
return decodeSmallNumber(buffer, start, end)
}

export function decodeInt64s(buffer: Uint8Array, from: number, to: number): Numeric[] {
return decodeVarints(buffer, from, to, true)
}

function decodeVarints(buffer: Uint8Array, from: number, to: number, signed: boolean): Numeric[] {
const values = []
let start = from

for (let i = from; i < to; i++) {
if ((buffer[i] & 0b10000000) === 0) {
if (i - start >= 4) {
values.push(decodeBigNumber(buffer, start))
const value = decodeBigNumber(buffer, start)
values.push(signed ? toSigned(value) : value)
} else {
values.push(decodeSmallNumber(buffer, start, i + 1))
}
Expand All @@ -132,11 +146,11 @@ const twoTo63 = 2n ** 63n
const twoTo64 = 2n ** 64n

/**
* Interprets an unsigned varint as int64 or int32. Negative values use 10 bytes
* Interprets an unsigned varint as int64. Negative values use 10 bytes
* and decode as bigints >= 2^63.
*/
export function toSigned(number: Numeric): Numeric {
return typeof number === 'bigint' && number >= twoTo63 ? number - twoTo64 : number
function toSigned(number: bigint): bigint {
return number >= twoTo63 ? number - twoTo64 : number
}

/** Decodes a little-endian uint64 as a number if safe, or a bigint otherwise. */
Expand Down
Loading