Skip to content

Commit a37e67e

Browse files
authored
Merge pull request #4978 from cloudflare/felix/090325-STW-QA
[o11y] 09-03-25 STW cleanup, QA, debug logging
2 parents 2adf723 + 0037462 commit a37e67e

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

src/workerd/api/worker-rpc.c++

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,9 @@ kj::Promise<WorkerInterface::CustomEvent::Result> JsRpcSessionCustomEventImpl::r
19351935
IoContext& ioctx = incomingRequest->getContext();
19361936

19371937
incomingRequest->delivered();
1938+
KJ_IF_SOME(tracer, incomingRequest->getWorkerTracer()) {
1939+
tracer.setIsJsRpc();
1940+
}
19381941

19391942
KJ_DEFER({
19401943
// waitUntil() should allow extending execution on the server side even when the client

src/workerd/io/trace.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,8 +1111,8 @@ inline SpanBuilder SpanBuilder::newChild(kj::ConstString operationName, kj::Date
11111111

11121112
// Interface to track trace context including both Jaeger and User spans.
11131113
// TODO(o11y): Consider fleshing this out to make it a proper class, support adding tags/child spans
1114-
// to both,... We expect that tracking user spans will not needed in all places where we have the
1115-
// existing spans, so synergies will likely be limited.
1114+
// to both,... We expect that tracking user spans will not be needed in all places where we have the
1115+
// existing spans, so synergies will be limited.
11161116
struct TraceContext {
11171117
TraceContext(SpanBuilder span, SpanBuilder userSpan)
11181118
: span(kj::mv(span)),

src/workerd/io/tracer.c++

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// https://opensource.org/licenses/Apache-2.0
44

55
#include <workerd/io/tracer.h>
6+
#include <workerd/util/sentry.h>
67
#include <workerd/util/thread-scopes.h>
78

89
#include <capnp/message.h> // for capnp::clone()
@@ -167,6 +168,10 @@ void WorkerTracer::addLog(const tracing::InvocationSpanContext& context,
167168
// available. If the given worker stage is only tailed by a streaming tail worker, adding the log
168169
// to the legacy trace object is not needed; this will be addressed in a future refactor.
169170
KJ_IF_SOME(writer, maybeTailStreamWriter) {
171+
// TODO(felix): Used for debug logging, remove after a few days.
172+
if (topLevelInvocationSpanContext == kj::none) {
173+
LOG_NOSENTRY(WARNING, "tried to send log before onset event", trace->entrypoint, isJsRpc);
174+
}
170175
// If message is too big on its own, truncate it.
171176
writer->report(context,
172177
{(tracing::Log(timestamp, logLevel,
@@ -220,7 +225,8 @@ void WorkerTracer::addSpan(CompleteSpan&& span) {
220225
// TODO(o11y): Provide correct nested spans
221226
// TODO(o11y): Propagate span context when context entropy is not available for RPC-based worker
222227
// invocations as indicated by isTrigger
223-
auto& topLevelContext = KJ_ASSERT_NONNULL(topLevelInvocationSpanContext, span);
228+
auto& topLevelContext =
229+
KJ_ASSERT_NONNULL(topLevelInvocationSpanContext, span, trace->entrypoint, isJsRpc);
224230
tracing::InvocationSpanContext context = [&]() {
225231
if (topLevelContext.isTrigger()) {
226232
return topLevelContext.clone();
@@ -419,6 +425,12 @@ void WorkerTracer::setOutcome(EventOutcome outcome, kj::Duration cpuTime, kj::Du
419425
// fixed size.
420426
}
421427

428+
void WorkerTracer::recordTimestamp(kj::Date timestamp) {
429+
if (completeTime == kj::UNIX_EPOCH) {
430+
completeTime = timestamp;
431+
}
432+
}
433+
422434
void WorkerTracer::setFetchResponseInfo(tracing::FetchResponseInfo&& info) {
423435
// Match the behavior of setEventInfo(). Any resolution of the TODO comments
424436
// in setEventInfo() that are related to this check while probably also affect
@@ -434,6 +446,7 @@ void WorkerTracer::setFetchResponseInfo(tracing::FetchResponseInfo&& info) {
434446
}
435447

436448
void WorkerTracer::setUserRequestSpan(SpanParent&& span) {
449+
KJ_ASSERT(span.isObserved(), "span argument must be observed");
437450
KJ_ASSERT(!userRequestSpan.isObserved(), "setUserRequestSpan can only be called once");
438451
userRequestSpan = kj::mv(span);
439452
}
@@ -446,4 +459,8 @@ SpanParent WorkerTracer::getUserRequestSpan() {
446459
return userRequestSpan.addRef();
447460
}
448461

462+
void BaseTracer::setIsJsRpc() {
463+
isJsRpc = true;
464+
}
465+
449466
} // namespace workerd

src/workerd/io/tracer.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,15 @@ class BaseTracer: public kj::Refcounted {
128128

129129
// Report time as seen from the incoming Request when the request is complete, since it will not
130130
// be available afterwards.
131-
void recordTimestamp(kj::Date timestamp) {
132-
if (completeTime == kj::UNIX_EPOCH) {
133-
completeTime = timestamp;
134-
}
135-
}
131+
virtual void recordTimestamp(kj::Date timestamp) = 0;
136132

137133
virtual SpanParent getUserRequestSpan() = 0;
138134

139-
// Time to be reported for the outcome event time. This will be set before the outcome is
140-
// dispatched.
141-
kj::Date completeTime = kj::UNIX_EPOCH;
135+
// TODO(felix): Used for debug logging, remove after a few days.
136+
void setIsJsRpc();
137+
138+
// Indicates that we're reporting from a JsRpc customEvent.
139+
bool isJsRpc = false;
142140
};
143141

144142
// Records a worker stage's trace information into a Trace object. When all references to the
@@ -172,6 +170,8 @@ class WorkerTracer: public BaseTracer {
172170
tracing::EventInfo&& info) override;
173171
void setFetchResponseInfo(tracing::FetchResponseInfo&& info) override;
174172
void setOutcome(EventOutcome outcome, kj::Duration cpuTime, kj::Duration wallTime) override;
173+
virtual void recordTimestamp(kj::Date timestamp) override;
174+
175175
SpanParent getUserRequestSpan() override;
176176
// Allow setting the user request span after the tracer has been created so its observer can
177177
// reference the tracer. This can only be set once.
@@ -198,5 +198,9 @@ class WorkerTracer: public BaseTracer {
198198
kj::Maybe<kj::Rc<PipelineTracer>> parentPipeline;
199199

200200
kj::Maybe<kj::Own<tracing::TailStreamWriter>> maybeTailStreamWriter;
201+
202+
// Time to be reported for the outcome event time. This will be set before the outcome is
203+
// dispatched.
204+
kj::Date completeTime = kj::UNIX_EPOCH;
201205
};
202206
} // namespace workerd

src/workerd/io/worker.c++

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ void addExceptionToTrace(jsg::Lock& js,
235235
}
236236
}
237237

238-
// TODO(someday): Limit size of exception content?
239238
tracer.addException(ioContext.getInvocationSpanContext(), timestamp, kj::mv(name),
240239
kj::mv(message), kj::mv(stack));
241240
}

0 commit comments

Comments
 (0)