Skip to content

Commit aa74399

Browse files
committed
[o11y] Refactor sql-test-tail.js
This test was poorly designed and failed to await promises for the test output properly, doing so means we receive one additional span by the time we check the test output. The test now follows the regular test STW more closely and is much easier to understand.
1 parent cde1840 commit aa74399

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/workerd/api/sql-test-tail.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ let invocationPromises = [];
44
let spans = new Map();
55

66
export default {
7-
async fetch(ctrl, env, ctx) {
8-
return new Response('');
9-
},
107
async test(ctrl, env, ctx) {
8+
await Promise.allSettled(invocationPromises);
119
let received = Array.from(spans.values()).filter(
1210
(span) => span.name !== 'jsRpcSession'
1311
);
@@ -17,15 +15,14 @@ export default {
1715
acc[curr.name]++;
1816
return acc;
1917
}, {});
20-
await Promise.allSettled(invocationPromises);
2118
assert.deepStrictEqual(reduced, {
2219
durable_object_storage_exec: 268,
2320
durable_object_storage_ingest: 1031,
2421
durable_object_storage_getDatabaseSize: 3,
2522
durable_object_storage_put: 18,
2623
durable_object_storage_get: 18,
2724
durable_object_storage_transaction: 8,
28-
durable_object_subrequest: 46,
25+
durable_object_subrequest: 47,
2926
durable_object_storage_deleteAll: 1,
3027
createStringTable: 4,
3128
runActorFunc: 4,
@@ -48,34 +45,37 @@ export default {
4845
resolveFn = resolve;
4946
})
5047
);
48+
const topLevelSpanId = event.event.spanId;
5149

5250
return (event) => {
5351
try {
54-
const rpcMethodName = Array.isArray(event.event.info)
55-
? (event.event.info || []).find(
56-
(item) => item['name'] === 'jsrpc.method'
57-
)?.value
58-
: undefined;
59-
const spanUniqueId = `${event.spanContext.traceId}#${event.event.spanId || event.spanContext.spanId}`;
52+
let spanKey =
53+
event.invocationId + (event.event.spanId || event.spanContext.spanId);
6054
switch (event.event.type) {
6155
case 'spanOpen':
6256
// The span ids will change between tests, but Map preserves insertion order
63-
spans.set(spanUniqueId, {
64-
name: event.event.name || rpcMethodName,
57+
spans.set(spanKey, {
58+
name: event.event.name,
6559
});
6660
break;
6761
case 'attributes': {
68-
let span = spans.get(spanUniqueId) || { name: rpcMethodName };
62+
let span = spans.get(spanKey);
63+
if (event.spanContext.spanId == topLevelSpanId) {
64+
// top-level JsRpc method name attribute – transform into a span with the given name
65+
const rpcMethodName = event.event.info.find(
66+
(item) => item['name'] === 'jsrpc.method'
67+
).value;
68+
span = { name: rpcMethodName };
69+
}
6970
for (let { name, value } of event.event.info) {
7071
span[name] = value;
7172
}
72-
spans.set(spanUniqueId, span);
73+
spans.set(spanKey, span);
7374
break;
7475
}
7576
case 'spanClose': {
76-
let span = spans.get(spanUniqueId);
77+
let span = spans.get(spanKey);
7778
span['closed'] = true;
78-
spans.set(spanUniqueId, span);
7979
break;
8080
}
8181
case 'outcome':

0 commit comments

Comments
 (0)