|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const crypto = require('crypto') |
| 4 | +const test = require('tape') |
| 5 | + |
| 6 | +const TraceContext = require('../../lib/instrumentation/context') |
| 7 | + |
| 8 | +const version = Buffer.alloc(1) |
| 9 | +const traceId = crypto.randomBytes(16) |
| 10 | +const id = crypto.randomBytes(8) |
| 11 | +const flags = Buffer.alloc(1) |
| 12 | + |
| 13 | +const header = `00-${traceId.toString('hex')}-${id.toString('hex')}-00` |
| 14 | + |
| 15 | +test('constructor', t => { |
| 16 | + const context = new TraceContext({ |
| 17 | + version, |
| 18 | + traceId, |
| 19 | + id, |
| 20 | + flags |
| 21 | + }) |
| 22 | + |
| 23 | + t.ok(context instanceof TraceContext, 'has a trace context object') |
| 24 | + t.ok(version.equals(context.version), 'version matches') |
| 25 | + t.ok(traceId.equals(context.traceId), 'traceId matches') |
| 26 | + t.ok(id.equals(context.id), 'id matches') |
| 27 | + t.ok(flags.equals(context.flags), 'flags matches') |
| 28 | + |
| 29 | + t.end() |
| 30 | +}) |
| 31 | + |
| 32 | +test('fromString', t => { |
| 33 | + const context = TraceContext.fromString(header) |
| 34 | + |
| 35 | + t.ok(context instanceof TraceContext, 'has a trace context object') |
| 36 | + t.ok(version.equals(context.version), 'version matches') |
| 37 | + t.ok(traceId.equals(context.traceId), 'traceId matches') |
| 38 | + t.ok(id.equals(context.id), 'id matches') |
| 39 | + t.ok(flags.equals(context.flags), 'flags matches') |
| 40 | + |
| 41 | + t.end() |
| 42 | +}) |
| 43 | + |
| 44 | +test('toString', t => { |
| 45 | + const result = TraceContext.fromString(header).toString() |
| 46 | + t.equal(result, header, 'trace context stringifies to valid header') |
| 47 | + t.end() |
| 48 | +}) |
| 49 | + |
| 50 | +test('toJSON', t => { |
| 51 | + const context = TraceContext.fromString(header) |
| 52 | + |
| 53 | + t.deepEqual(context.toJSON(), { |
| 54 | + version: version.toString('hex'), |
| 55 | + traceId: traceId.toString('hex'), |
| 56 | + id: id.toString('hex'), |
| 57 | + flags: flags.toString('hex'), |
| 58 | + parentId: undefined |
| 59 | + }, 'trace context serializes fields to hex strings, in JSON form') |
| 60 | + |
| 61 | + t.end() |
| 62 | +}) |
| 63 | + |
| 64 | +test('create', t => { |
| 65 | + const context = TraceContext.create() |
| 66 | + |
| 67 | + t.ok(context instanceof TraceContext, 'has a trace context object') |
| 68 | + t.ok(version.equals(context.version), 'version matches') |
| 69 | + t.notOk(traceId.equals(context.traceId), 'has new traceId') |
| 70 | + t.notOk(id.equals(context.id), 'has new spanId') |
| 71 | + t.ok(flags.equals(context.flags), 'flags matches') |
| 72 | + |
| 73 | + t.end() |
| 74 | +}) |
| 75 | + |
| 76 | +test('child', t => { |
| 77 | + const parent = TraceContext.fromString(header) |
| 78 | + const context = parent.child() |
| 79 | + |
| 80 | + t.ok(context instanceof TraceContext, 'has a trace context object') |
| 81 | + t.ok(version.equals(context.version), 'version matches') |
| 82 | + t.ok(traceId.equals(context.traceId), 'traceId matches') |
| 83 | + t.notOk(id.equals(context.id), 'has new id') |
| 84 | + t.ok(flags.equals(context.flags), 'flags matches') |
| 85 | + t.ok(parent.id.equals(context.parentId), 'parentId matches parent.id') |
| 86 | + |
| 87 | + t.end() |
| 88 | +}) |
0 commit comments