Skip to content

Commit

Permalink
Add truncated flag for tail worker events
Browse files Browse the repository at this point in the history
When set, indicates that the tail worker event was truncated due
to the event buffer being full.
  • Loading branch information
jasnell committed Jul 16, 2024
1 parent b94bad0 commit 2501c3e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/workerd/api/trace.c++
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ TraceItem::TraceItem(jsg::Lock& js, const Trace& trace)
scriptTags(getTraceScriptTags(trace)),
outcome(getTraceOutcome(trace)),
cpuTime(trace.cpuTime / kj::MILLISECONDS),
wallTime(trace.wallTime / kj::MILLISECONDS) {}
wallTime(trace.wallTime / kj::MILLISECONDS),
truncated(trace.truncated) {}

kj::Maybe<TraceItem::EventInfo> TraceItem::getEvent(jsg::Lock& js) {
return eventInfo.map([](auto& info) -> TraceItem::EventInfo {
Expand Down Expand Up @@ -260,6 +261,8 @@ jsg::Optional<kj::Array<kj::StringPtr>> TraceItem::getScriptTags() {

kj::StringPtr TraceItem::getOutcome() { return outcome; }

bool TraceItem::getTruncated() { return truncated; }

uint TraceItem::getCpuTime() { return cpuTime; }

uint TraceItem::getWallTime() { return wallTime; }
Expand Down
3 changes: 3 additions & 0 deletions src/workerd/api/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class TraceItem final: public jsg::Object {

uint getCpuTime();
uint getWallTime();
bool getTruncated();

JSG_RESOURCE_TYPE(TraceItem) {
JSG_LAZY_READONLY_INSTANCE_PROPERTY(event, getEvent);
Expand All @@ -115,6 +116,7 @@ class TraceItem final: public jsg::Object {
JSG_LAZY_READONLY_INSTANCE_PROPERTY(dispatchNamespace, getDispatchNamespace);
JSG_LAZY_READONLY_INSTANCE_PROPERTY(scriptTags, getScriptTags);
JSG_LAZY_READONLY_INSTANCE_PROPERTY(outcome, getOutcome);
JSG_LAZY_READONLY_INSTANCE_PROPERTY(truncated, getTruncated);
}

void visitForMemoryInfo(jsg::MemoryTracker& tracker) const;
Expand All @@ -133,6 +135,7 @@ class TraceItem final: public jsg::Object {
kj::String outcome;
uint cpuTime;
uint wallTime;
bool truncated;
};

// When adding a new TraceItem eventInfo type, it is important not to
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/io/trace.c++
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ void Trace::copyTo(rpc::Trace::Builder builder) {
}
}

builder.setTruncated(truncated);
builder.setOutcome(outcome);
builder.setCpuTime(cpuTime / kj::MILLISECONDS);
builder.setWallTime(wallTime / kj::MILLISECONDS);
Expand Down Expand Up @@ -571,6 +572,7 @@ void WorkerTracer::log(kj::Date timestamp, LogLevel logLevel, kj::String message
size_t newSize = trace->bytesUsed + sizeof(Trace::Log) + message.size();
if (newSize > MAX_TRACE_BYTES) {
trace->exceededLogLimit = true;
trace->truncated = true;
// We use a JSON encoded array/string to match other console.log() recordings:
trace->logs.add(
timestamp, LogLevel::WARN,
Expand Down Expand Up @@ -598,6 +600,7 @@ void WorkerTracer::addException(kj::Date timestamp, kj::String name, kj::String
}
if (newSize > MAX_TRACE_BYTES) {
trace->exceededExceptionLimit = true;
trace->truncated = true;
trace->exceptions.add(
timestamp, kj::str("Error"),
kj::str("Trace resource limit exceeded; subsequent exceptions not recorded."),
Expand All @@ -621,6 +624,7 @@ void WorkerTracer::addDiagnosticChannelEvent(kj::Date timestamp,
message.size();
if (newSize > MAX_TRACE_BYTES) {
trace->exceededDiagnosticChannelEventLimit = true;
trace->truncated = true;
trace->diagnosticChannelEvents.add(timestamp, kj::str("workerd.LimitExceeded"),
kj::Array<kj::byte>());
return;
Expand Down
1 change: 1 addition & 0 deletions src/workerd/io/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ class Trace final : public kj::Refcounted {
kj::Duration cpuTime;
kj::Duration wallTime;

bool truncated = false;
bool exceededLogLimit = false;
bool exceededExceptionLimit = false;
bool exceededDiagnosticChannelEventLimit = false;
Expand Down
3 changes: 3 additions & 0 deletions src/workerd/io/worker-interface.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ struct Trace @0x8e8d911203762d34 {
channel @1 :Text;
message @2 :Data;
}

truncated @24 :Bool;
# Indicates that the trace was truncated due to reaching the maximum size limit.
}

struct ScheduledRun @0xd98fc1ae5c8095d0 {
Expand Down

0 comments on commit 2501c3e

Please sign in to comment.