Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/workerd/api/tests/tail-worker-test-receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const test = {
// Number of traces based on how often main tail worker is invoked from previous tests
let numTraces = 32;
let basicTrace =
'{"type":"onset","executionModel":"stateless","spanId":"0000000000000000","scriptTags":[],"info":{"type":"trace","traces":[]}}{"type":"return"}{"type":"outcome","outcome":"ok","cpuTime":0,"wallTime":0}';
'{"type":"onset","executionModel":"stateless","spanId":"0000000000000000","scriptTags":[],"info":{"type":"trace","traces":[]},"spanKind":"server"}{"type":"return"}{"type":"outcome","outcome":"ok","cpuTime":0,"wallTime":0}';
assert.deepStrictEqual(
Array.from(resposeMap.values()),
Array.from({ length: numTraces }, () => basicTrace)
Expand Down
64 changes: 32 additions & 32 deletions src/workerd/api/tests/tail-worker-test.js

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions src/workerd/io/trace-stream.c++
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ namespace {
V(SPANCLOSE, "spanClose") \
V(SPANCONTEXT, "spanContext") \
V(SPANID, "spanId") \
V(SPANKIND, "spanKind") \
V(SPANKIND_CLIENT, "client") \
V(SPANKIND_SERVER, "server") \
V(SPANKIND_PRODUCER, "producer") \
V(SPANKIND_CONSUMER, "consumer") \
V(SPANKIND_INTERNAL, "internal") \
V(SPANOPEN, "spanOpen") \
V(STACK, "stack") \
V(STATUSCODE, "statusCode") \
Expand Down Expand Up @@ -136,6 +142,22 @@ class StringCache final {
// these structs to be bidirectional. So, instead, let's just do the simple easy thing
// and define a set of serializers to these types.

kj::LiteralStringConst spanKindToString(SpanKind kind) {
switch (kind) {
case SpanKind::CLIENT:
return SPANKIND_CLIENT_STR;
case SpanKind::SERVER:
return SPANKIND_SERVER_STR;
case SpanKind::PRODUCER:
return SPANKIND_PRODUCER_STR;
case SpanKind::CONSUMER:
return SPANKIND_CONSUMER_STR;
case SpanKind::INTERNAL:
return SPANKIND_INTERNAL_STR;
}
KJ_UNREACHABLE;
}

// Serialize attribute value
jsg::JsValue ToJs(jsg::Lock& js, const Attribute::Value& value) {
KJ_SWITCH_ONEOF(value) {
Expand Down Expand Up @@ -405,6 +427,7 @@ jsg::JsValue ToJs(jsg::Lock& js, const Onset& onset, StringCache& cache) {
js.arr(onset.attributes.asPtr(),
[&cache](jsg::Lock& js, const auto& attr) { return ToJs(js, attr, cache); }));
}
obj.set(js, SPANKIND_STR, cache.get(js, spanKindToString(onset.spanKind)));

return obj;
}
Expand Down
20 changes: 19 additions & 1 deletion src/workerd/io/tracer.c++
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,28 @@ void WorkerTracer::setEventInfoInternal(
auto onsetContext = tracing::InvocationSpanContext(
context.getTraceId(), context.getInvocationId(), tracing::SpanId::nullId);

// Derive SpanKind for the root span from the handler type.
SpanKind onsetSpanKind = SpanKind::SERVER;
KJ_SWITCH_ONEOF(info) {
KJ_CASE_ONEOF(_, tracing::ScheduledEventInfo) {
onsetSpanKind = SpanKind::CONSUMER;
}
KJ_CASE_ONEOF(_, tracing::AlarmEventInfo) {
onsetSpanKind = SpanKind::CONSUMER;
}
KJ_CASE_ONEOF(_, tracing::QueueEventInfo) {
onsetSpanKind = SpanKind::CONSUMER;
}
KJ_CASE_ONEOF(_, tracing::EmailEventInfo) {
onsetSpanKind = SpanKind::CONSUMER;
}
KJ_CASE_ONEOF_DEFAULT { /* SERVER for fetch, jsrpc, trace, hibernatableWebSocket, etc. */ }
}

// Not applying size accounting for Onset since it is sent separately
writer->report(onsetContext,
tracing::Onset(context.getSpanId(), cloneEventInfo(info), kj::mv(workerInfo),
attributes.releaseAsArray()),
attributes.releaseAsArray(), onsetSpanKind),
timestamp, 0);
}

Expand Down
3 changes: 3 additions & 0 deletions types/defines/trace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ type EventOutcome = "ok" | "canceled" | "exception" | "unknown" | "killSwitch" |
"daemonDown" | "exceededCpu" | "exceededMemory" | "loadShed" |
"responseStreamDisconnected" | "scriptNotFound";

type SpanKind = "client" | "server" | "producer" | "consumer" | "internal";

interface ScriptVersion {
readonly id: string;
readonly tag?: string;
Expand All @@ -103,6 +105,7 @@ interface Onset {
ScheduledEventInfo | AlarmEventInfo | QueueEventInfo |
EmailEventInfo | TraceEventInfo |
HibernatableWebSocketEventInfo | CustomEventInfo;
readonly spanKind: SpanKind;
}

interface Outcome {
Expand Down
2 changes: 2 additions & 0 deletions types/generated-snapshot/experimental/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14562,6 +14562,7 @@ declare namespace TailStream {
| "loadShed"
| "responseStreamDisconnected"
| "scriptNotFound";
type SpanKind = "client" | "server" | "producer" | "consumer" | "internal";
interface ScriptVersion {
readonly id: string;
readonly tag?: string;
Expand Down Expand Up @@ -14589,6 +14590,7 @@ declare namespace TailStream {
| TraceEventInfo
| HibernatableWebSocketEventInfo
| CustomEventInfo;
readonly spanKind: SpanKind;
}
interface Outcome {
readonly type: "outcome";
Expand Down
2 changes: 2 additions & 0 deletions types/generated-snapshot/experimental/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14519,6 +14519,7 @@ export declare namespace TailStream {
| "loadShed"
| "responseStreamDisconnected"
| "scriptNotFound";
type SpanKind = "client" | "server" | "producer" | "consumer" | "internal";
interface ScriptVersion {
readonly id: string;
readonly tag?: string;
Expand Down Expand Up @@ -14546,6 +14547,7 @@ export declare namespace TailStream {
| TraceEventInfo
| HibernatableWebSocketEventInfo
| CustomEventInfo;
readonly spanKind: SpanKind;
}
interface Outcome {
readonly type: "outcome";
Expand Down
2 changes: 2 additions & 0 deletions types/generated-snapshot/latest/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13868,6 +13868,7 @@ declare namespace TailStream {
| "loadShed"
| "responseStreamDisconnected"
| "scriptNotFound";
type SpanKind = "client" | "server" | "producer" | "consumer" | "internal";
interface ScriptVersion {
readonly id: string;
readonly tag?: string;
Expand Down Expand Up @@ -13895,6 +13896,7 @@ declare namespace TailStream {
| TraceEventInfo
| HibernatableWebSocketEventInfo
| CustomEventInfo;
readonly spanKind: SpanKind;
}
interface Outcome {
readonly type: "outcome";
Expand Down
2 changes: 2 additions & 0 deletions types/generated-snapshot/latest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13825,6 +13825,7 @@ export declare namespace TailStream {
| "loadShed"
| "responseStreamDisconnected"
| "scriptNotFound";
type SpanKind = "client" | "server" | "producer" | "consumer" | "internal";
interface ScriptVersion {
readonly id: string;
readonly tag?: string;
Expand Down Expand Up @@ -13852,6 +13853,7 @@ export declare namespace TailStream {
| TraceEventInfo
| HibernatableWebSocketEventInfo
| CustomEventInfo;
readonly spanKind: SpanKind;
}
interface Outcome {
readonly type: "outcome";
Expand Down
Loading