GitHubivarvong
ply
main
1// EndedSpan and Tracer's onEnd (issue #210): a span as it ended, as plain values, with no OTLP built unless asked.
2// The contract: every field of the EndedSpan equals what flattenLine reads back from the OTLP line end() exports for the
3// same span (judged here over random attribute records of every value kind, scrubbed and not, with events, links and
4// error status), otlp() is that very span, and a tracer builds only the forms someone consumes.
5import { test } from 'vitest';
6import assert from 'node:assert/strict';
7import { Tracer, arrayExporter, flattenLine, KIND } from '../src/otel.js';
9const scrub = s => s.replaceAll('SECRET', '[redacted]');
10const plain = ended => { const flat = { ...ended }; delete flat.startTimeUnixNano; delete flat.endTimeUnixNano; delete flat.otlp; return flat; };
12// A deterministic generator of attribute values: every branch of anyValue, the edges of each (-0, NaN, a bigint past
13// 2^53, an empty and an over-4096-byte Uint8Array, nested arrays holding null and the missing value, objects).
14function rng(seed) { let x = seed >>> 0; return () => { x = (Math.imul(x, 1664525) + 1013904223) >>> 0; return x / 2 ** 32; }; }
15function value(r, depth = 0) {
16 const pick = Math.floor(r() * 17);
17 switch (pick) {
18 case 0: return null;
19 case 1: return undefined;
20 case 2: return '';
21 case 3: return `v${Math.floor(r() * 100)} SECRET tail`;
22 case 4: return r() < 0.5;
23 case 5: return Math.floor(r() * 2 ** 40) - 2 ** 39;
24 case 6: return -0;
25 case 7: return r() * 1e6 - 5e5;
26 case 8: return [Number.NaN, Infinity, -Infinity, 1e21, 2 ** 53 + 2][Math.floor(r() * 5)];
27 case 9: return BigInt(Math.floor(r() * 2 ** 50)) * 123_456_789n - 99n;
28 case 10: return Uint8Array.from({ length: Math.floor(r() * 8) }, () => Math.floor(r() * 256));
29 case 11: return new Uint8Array(4100).fill(7);
30 case 12: return depth > 2 ? 'leaf' : Array.from({ length: Math.floor(r() * 4) }, () => value(r, depth + 1));
31 case 13: return { toString: () => 'object SECRET' };
32 case 14: return new Date(0);
33 case 15: return 0;
34 default: return `plain-${pick}`;
35 }
36}
37function attrs(r) {
38 const out = {};
39 const n = Math.floor(r() * 12);
40 for (let i = 0; i < n; i++) out[`k.${i}.${Math.floor(r() * 3)}`] = value(r);
41 if (r() < 0.1) Object.defineProperty(out, '__proto__', { value: value(r), enumerable: true, writable: true, configurable: true });
42 return out;
43}
45test('an EndedSpan is exactly what flattenLine reads back from the exported OTLP line, and otlp() is that span', () => {
46 const r = rng(20261106);
47 let compared = 0, scrubbed = 0, errors = 0, withEvents = 0, followers = 0;
48 for (let round = 0; round < 400; round++) {
49 const ended = [];
50 const exporter = arrayExporter();
51 const t = new Tracer({ exporter, onEnd: s => ended.push(s), maxEvents: 3 });
52 const useScrub = r() < 0.5;
53 const root = t.start('git.op', { attrs: attrs(r), ...(useScrub ? { scrub } : {}) });
54 const child = t.start('git.op.child', { parent: root, kind: KIND.CLIENT, attrs: attrs(r) });
55 child.set(attrs(r));
56 for (let e = Math.floor(r() * 5); e > 0; e--) child.event(`ev${e}`, attrs(r));
57 if (r() < 0.3) { child.error(Object.assign(new Error('boom SECRET'), { code: 'http' })); errors++; }
58 child.end();
59 const follower = t.start('git.op.stream', { follows: root, attrs: attrs(r) });
60 root.end();
61 follower.end();
62 followers++;
63 assert.equal(ended.length, 3);
64 assert.equal(exporter.lines.length, 3);
65 for (let i = 0; i < 3; i++) {
66 const line = exporter.lines[i];
67 const [flat] = flattenLine(line);
68 assert.deepStrictEqual(plain(ended[i]), flat, `round ${round} span ${i}`);
69 const otlp = line.resourceSpans[0].scopeSpans[0].spans[0];
70 assert.equal(ended[i].otlp(), otlp, 'otlp() is the span the exporter got');
71 assert.equal(ended[i].startTimeUnixNano, otlp.startTimeUnixNano);
72 assert.equal(ended[i].endTimeUnixNano, otlp.endTimeUnixNano);
73 if (useScrub) scrubbed++;
74 if (flat.events.length) withEvents++;
75 compared++;
76 }
77 if (useScrub) for (const s of ended) assert.ok(!JSON.stringify(plain(s)).includes('SECRET'), 'no credential in a scrubbed EndedSpan');
78 }
79 assert.ok(compared === 1200 && scrubbed > 300 && errors > 50 && withEvents > 100 && followers === 400, `vacuous: ${compared} ${scrubbed} ${errors} ${withEvents}`);
80});
82test('onEnd alone: no exporter call and no OTLP until otlp() is asked; then built once, as an exporter would have got it', () => {
83 const ended = [];
84 const t = new Tracer({ onEnd: s => ended.push(s) });
85 const s = t.start('git.x', { attrs: { 'git.x.n': 3n, 'git.x.s': 'SECRET' }, scrub });
86 s.event('e', { a: 1 });
87 s.end();
88 assert.equal(t.count, 1, 'every ended span is counted');
89 assert.equal(ended.length, 1);
90 const [e] = ended;
91 assert.deepStrictEqual(e.attrs, { 'git.x.n': 3, 'git.x.s': '[redacted]', 'ply.isolate.invocation': e.attrs['ply.isolate.invocation'], 'ply.isolate.cold': e.attrs['ply.isolate.cold'] });
92 // late writes after end change the live span, not what ended
93 s.set({ 'git.x.late': 1 });
94 s.event('late');
95 s.error(new Error('late'));
96 assert.equal(s.attrs['git.x.late'], 1, 'a late set still lands on the span');
97 const otlp = e.otlp();
98 assert.equal(e.otlp(), otlp, 'built once');
99 const [flat] = flattenLine({ resourceSpans: [{ resource: t.resource, scopeSpans: [{ scope: t.scope, spans: [otlp] }] }] });
100 assert.deepStrictEqual(plain(e), flat);
101 assert.ok(!otlp.attributes.some(kv => kv.key === 'git.x.late'));
102 assert.equal(otlp.events.length, 1);
103 assert.equal(otlp.status.code, 1);
104 assert.deepStrictEqual(otlp.attributes.find(kv => kv.key === 'git.x.n').value, { intValue: '3' });
105});
107test('a tracer with neither onEnd nor an exporter builds nothing and still counts; the default exporter is unchanged', () => {
108 const t = new Tracer();
109 assert.equal(t.onEnd, null);
110 t.start('a').end();
111 t.start('b').end();
112 assert.equal(t.count, 2);
113 t.exporter({ resourceSpans: [] });
114});
116test('onEnd runs before the exporter, and a subclass that overrides export gets every span (old consumers)', () => {
117 const order = [];
118 const t = new Tracer({ exporter: () => order.push('export'), onEnd: () => order.push('onEnd') });
119 t.start('a').end();
120 assert.deepStrictEqual(order, ['onEnd', 'export']);
121 const seen = [];
122 class Mirror extends Tracer { export(span) { seen.push(span.name); super.export(span); } }
123 new Mirror().start('m1').end();
124 const viaSink = [];
125 new Mirror({ onEnd: s => viaSink.push(s.name) }).start('m2').end();
126 assert.deepStrictEqual(seen, ['m1', 'm2']);
127 assert.deepStrictEqual(viaSink, ['m2']);
128 const exported = [];
129 const late = new Tracer();
130 late.exporter = line => exported.push(line);
131 late.start('assigned').end();
132 assert.equal(exported.length, 1, 'an exporter assigned after construction is used');
133});
135test('OTLP is built only for a consumer of it: the scrubber sees each string once per form built', () => {
136 const run = options => {
137 let calls = 0;
138 const t = new Tracer(options);
139 const s = t.start('git.x', { attrs: { a: 'one', b: 'two', n: 3 }, scrub: v => { calls++; return v; } });
140 s.end();
141 return { calls, t };
142 };
143 const ended = [];
144 assert.equal(run({}).calls, 0, 'neither: nothing built');
145 assert.equal(run({ onEnd: s => ended.push(s) }).calls, 2, 'onEnd alone: the plain values only');
146 assert.equal(run({ exporter: () => {} }).calls, 2, 'exporter alone: the OTLP form only');
147 assert.equal(run({ exporter: () => {}, onEnd: () => {} }).calls, 4, 'both: each form once');
148 let lazy = 0;
149 const t = new Tracer({ onEnd: s => ended.push(s) });
150 t.start('git.y', { attrs: { a: 'one' }, scrub: v => { lazy++; return v; } }).end();
151 assert.equal(lazy, 1);
152 ended.at(-1).otlp();
153 ended.at(-1).otlp();
154 assert.equal(lazy, 2, 'otlp() builds once, on demand');
155});
157test('the OTLP form and times: a root has no parentSpanId key, times are the wall clock in ns, the defaults are ply\'s', () => {
158 const ended = [];
159 const t = new Tracer({ onEnd: s => ended.push(s) });
160 assert.deepStrictEqual(t.scope, { name: 'ply', version: '0.2.0' });
161 assert.deepStrictEqual(t.resource.attributes.map(kv => kv.key), ['service.name', 'telemetry.sdk.name', 'telemetry.sdk.language']);
162 assert.deepStrictEqual(t.resource.attributes.map(kv => kv.value.stringValue), ['ply', 'ply-otel', 'javascript']);
163 const before = BigInt(Date.now()) * 1_000_000n;
164 const root = t.start('r');
165 const child = t.start('c', { parent: root });
166 child.end(); root.end();
167 const after = BigInt(Date.now() + 1) * 1_000_000n;
168 const [c, r] = ended;
169 assert.equal('parentSpanId' in r.otlp(), false);
170 assert.equal(c.otlp().parentSpanId, r.spanId);
171 for (const e of ended) {
172 const start = BigInt(e.startTimeUnixNano), end = BigInt(e.endTimeUnixNano);
173 assert.ok(start >= before - 5_000_000n && end <= after + 5_000_000n && start <= end, `${start} ${end} within ${before}..${after}`);
174 assert.equal(e.durationMs, Number(end - start) / 1e6);
175 }
176});
178test('set() before end writes the span\'s own record; an own __proto__ key is a plain writable property', () => {
179 const ended = [];
180 const t = new Tracer({ onEnd: s => ended.push(s) });
181 // start() spreads its attrs (an own __proto__ key stays a property there; set()'s Object.assign would not keep it)
182 const s = t.start('x', { attrs: Object.defineProperty({}, '__proto__', { value: 'p', enumerable: true, writable: true, configurable: true }) });
183 const record = s.attrs;
184 s.set({ a: 1 });
185 assert.equal(s.attrs, record, 'no copy while the span is open');
186 s.end();
187 assert.notEqual(s.set({ b: 2 }).attrs, record, 'a late set writes a copy');
188 assert.deepStrictEqual(Object.getOwnPropertyDescriptor(ended[0].attrs, '__proto__'), { value: 'p', enumerable: true, writable: true, configurable: true });
189 assert.equal(Object.getPrototypeOf(ended[0].attrs), Object.prototype);
190});
192test('ids: 32 and 16 lowercase hex digits, distinct across pool refills', () => {
193 const t = new Tracer();
194 const ids = new Set();
195 for (let i = 0; i < 2000; i++) {
196 const s = t.start('r');
197 assert.match(s.traceId, /^[0-9a-f]{32}$/);
198 assert.match(s.spanId, /^[0-9a-f]{16}$/);
199 const c = t.start('c', { parent: s });
200 assert.equal(c.traceId, s.traceId);
201 ids.add(s.traceId); ids.add(s.spanId); ids.add(c.spanId);
202 }
203 assert.equal(ids.size, 6000);
204});
Diagnostics6 git calls · 27 ms
CallStatusSizeTime027 ms
discover200191 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=191 git.server.agent=git/github-9922ec520325-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=191 git.pkt.payload.max=36
ls-refs20012 KiB · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=11990 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=152 git.pkt.data=151 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=11990 git.pkt.payload.max=122 git.ls_refs.count=151 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=4347 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4347 git.pkt.payload.max=4247 git.sideband.band1.bytes=4247 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=4247 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400 git.pack.full=10Open pack map
fetchtree:0200919 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=919 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=919 git.pkt.payload.max=892 git.sideband.band1.bytes=892 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=892 git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02005.6 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=5724 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=5724 git.pkt.payload.max=5697 git.sideband.band1.bytes=5697 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=5697 git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch2003.6 KiB · hit18 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=18 git.response.bytes=3724 git.response.cache=HIT git.response.duration_ms=18 git.fetch.wants=9447626767f8b663f5f9b31d4a7d9c25123b2164 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3724 git.pkt.payload.max=3697 git.sideband.band1.bytes=3697 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://github.com/ivarvong/ply
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e2ef763bc3fda9
- Request
- a27aec79…
- Protocol
- v2 · sha1
- Agent
- git/github-9922ec520325-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format