Skip to content

Commit cc1d9c7

Browse files
committed
[o11y][nfc] Refactor tests, deduplicate tailStream() implementations, update comments
1 parent 04fb23c commit cc1d9c7

File tree

7 files changed

+14
-67
lines changed

7 files changed

+14
-67
lines changed

src/cloudflare/internal/test/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ exports_files(
44
"//src/cloudflare/internal/test/d1:__pkg__",
55
"//src/cloudflare/internal/test/images:__pkg__",
66
"//src/cloudflare/internal/test/tracing:__pkg__",
7-
"//src/workerd/api:__pkg__",
87
],
98
)

src/cloudflare/internal/test/d1/d1-api-instrumentation-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2017-2023 Cloudflare, Inc.
22
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
33
// https://opensource.org/licenses/Apache-2.0
4-
import * as assert from 'node:assert';
4+
55
import {
66
createInstrumentationState,
77
createTailStreamHandler,

src/cloudflare/internal/test/instrumentation-test-helper.js

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -138,60 +138,20 @@ export async function runInstrumentationTest(
138138
* @returns {Object} An object with methods to handle spans
139139
*/
140140
export function createTailStreamCollector() {
141-
const invocationPromises = [];
142-
const spans = new Map();
141+
let state = createInstrumentationState();
143142

144-
const tailStream = (event, env, ctx) => {
145-
// For each "onset" event, store a promise which we will resolve when
146-
// we receive the equivalent "outcome" event
147-
let resolveFn;
148-
invocationPromises.push(
149-
new Promise((resolve, reject) => {
150-
resolveFn = resolve;
151-
})
152-
);
153-
154-
// Accumulate the span info for easier testing
155-
return (event) => {
156-
let spanKey = `${event.invocationId}#${event.event.spanId || event.spanContext.spanId}`;
157-
switch (event.event.type) {
158-
case 'spanOpen':
159-
// The span ids will change between tests, but Map preserves insertion order
160-
spans.set(spanKey, { name: event.event.name });
161-
break;
162-
case 'attributes': {
163-
let span = spans.get(spanKey);
164-
for (let { name, value } of event.event.info) {
165-
span[name] = value;
166-
}
167-
spans.set(spanKey, span);
168-
break;
169-
}
170-
case 'spanClose': {
171-
let span = spans.get(spanKey);
172-
span['closed'] = true;
173-
spans.set(spanKey, span);
174-
break;
175-
}
176-
case 'outcome':
177-
resolveFn();
178-
break;
179-
}
180-
};
181-
};
182-
183-
const waitForCompletion = async () => {
184-
await Promise.allSettled(invocationPromises);
185-
};
143+
const tailStream = createTailStreamHandler(state);
186144

187-
const getSpans = () => {
188-
return Array.from(spans.values());
145+
let spans = state.spans;
146+
let invocationPromises = state.invocationPromises;
147+
const waitForCompletion = () => {
148+
return Promise.allSettled(invocationPromises);
189149
};
190150

191151
return {
192152
tailStream,
193153
waitForCompletion,
194-
getSpans,
154+
spans,
195155
};
196156
}
197157

src/cloudflare/internal/test/tracing/tracing-helpers-instrumentation-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const validateSpans = {
1919
await collector.waitForCompletion();
2020

2121
// Get all spans and prepare for validation
22-
const allSpans = collector.getSpans();
22+
const allSpans = collector.spans.values();
2323
const spansByTest = groupSpansBy(allSpans, 'test');
2424

2525
// Define the core tests that validate withSpan behavior

src/workerd/io/trace.c++

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,12 +1484,6 @@ CompleteSpan CompleteSpan::clone() const {
14841484
}
14851485
return copy;
14861486
}
1487-
1488-
kj::String CompleteSpan::toString() const {
1489-
return kj::str("CompleteSpan: ", operationName,
1490-
kj::strArray(
1491-
KJ_MAP(tag, tags) { return kj::str("(", tag.key, ", ", tag.value, ")"); }, ", "));
1492-
}
14931487
} // namespace tracing
14941488

14951489
// ======================================================================================

src/workerd/io/trace.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -899,14 +899,9 @@ inline kj::String truncateScriptId(kj::StringPtr id) {
899899
// =======================================================================================
900900
// Span tracing
901901
//
902-
// TODO(cleanup): As of now, this aspect of tracing is actually not related to the rest of this
903-
// file. Most of this file defines the interface to feed Trace Workers. Span tracing, however,
904-
// is currently designed to feed tracing of the Workers Runtime itself for the benefit of the
905-
// developers of the runtime.
906-
//
907-
// We might potentially want to give trace workers some access to span tracing as well, but with
908-
// that the trace worker and span interfaces should still be largely independent of each other;
909-
// separate span tracing into a separate header.
902+
// TODO(cleanup): (Streaming) tail workers now have access to span tracing as well, but with that
903+
// the trace worker and span interfaces are still mostly independent of each other; separate span
904+
// tracing into a separate header.
910905

911906
class SpanBuilder;
912907
class SpanObserver;

src/workerd/io/worker-entrypoint.c++

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,8 @@ kj::Promise<void> WorkerEntrypoint::prewarm(kj::StringPtr url) {
560560
kj::mv(KJ_REQUIRE_NONNULL(this->incomingRequest, "prewarm() can only be called once"));
561561
incomingRequest->getMetrics().setIsPrewarm();
562562

563-
// TODO(streaming-tail): Should prewarm be reflected in the tail stream somehow?
564-
565-
// Intentionally don't call incomingRequest->delivered() for prewarm requests.
563+
// Intentionally don't call incomingRequest->delivered() for prewarm requests and do not create
564+
// an Onset event, prewarm is not being traced.
566565

567566
// TODO(someday): Ideally, middleware workers would forward prewarm() to the next stage. At
568567
// present we don't have a good way to decide what stage that is, especially given that we'll

0 commit comments

Comments
 (0)