Skip to content

Commit

Permalink
Pass scriptId and sourcemapsAvailable in Trace
Browse files Browse the repository at this point in the history
  • Loading branch information
zebp committed Mar 18, 2024
1 parent 549c9b4 commit cb44afe
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
24 changes: 18 additions & 6 deletions src/workerd/io/trace.c++
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,12 @@ Trace::Exception::Exception(kj::Date timestamp, kj::String name, kj::String mess
: timestamp(timestamp), name(kj::mv(name)), message(kj::mv(message)) {}

Trace::Trace(kj::Maybe<kj::String> stableId, kj::Maybe<kj::String> scriptName,
kj::Maybe<kj::Own<ScriptVersion::Reader>> scriptVersion, kj::Maybe<kj::String> dispatchNamespace,
kj::Array<kj::String> scriptTags)
kj::Maybe<kj::Own<ScriptVersion::Reader>> scriptVersion, kj::Maybe<kj::String> scriptId,
kj::Maybe<kj::String> dispatchNamespace, kj::Array<kj::String> scriptTags)
: stableId(kj::mv(stableId)),
scriptName(kj::mv(scriptName)),
scriptVersion(kj::mv(scriptVersion)),
scriptId(kj::mv(scriptId)),
dispatchNamespace(kj::mv(dispatchNamespace)),
scriptTags(kj::mv(scriptTags)) {}
Trace::Trace(rpc::Trace::Reader reader) {
Expand All @@ -259,6 +260,7 @@ void Trace::copyTo(rpc::Trace::Builder builder) {
}
}

builder.setSourcemapsAvailable(sourcemapsAvailable);
builder.setOutcome(outcome);
builder.setCpuTime(cpuTime / kj::MILLISECONDS);
builder.setWallTime(wallTime / kj::MILLISECONDS);
Expand All @@ -268,6 +270,9 @@ void Trace::copyTo(rpc::Trace::Builder builder) {
KJ_IF_SOME(version, scriptVersion) {
builder.setScriptVersion(*version);
}
KJ_IF_SOME(id, scriptId) {
builder.setScriptId(id);
}
KJ_IF_SOME(ns, dispatchNamespace) {
builder.setDispatchNamespace(ns);
}
Expand Down Expand Up @@ -363,6 +368,10 @@ void Trace::mergeFrom(rpc::Trace::Reader reader, PipelineLogLevel pipelineLogLev
scriptVersion = capnp::clone(reader.getScriptVersion());
}

if (reader.hasScriptId()) {
scriptId = kj::str(reader.getScriptId());
}

if (reader.hasDispatchNamespace()) {
dispatchNamespace = kj::str(reader.getDispatchNamespace());
}
Expand All @@ -373,6 +382,8 @@ void Trace::mergeFrom(rpc::Trace::Reader reader, PipelineLogLevel pipelineLogLev

eventTimestamp = kj::UNIX_EPOCH + reader.getEventTimestampNs() * kj::NANOSECONDS;

sourcemapsAvailable = reader.getSourcemapsAvailable();

if (pipelineLogLevel == PipelineLogLevel::NONE) {
eventInfo = kj::none;
} else {
Expand Down Expand Up @@ -500,11 +511,11 @@ kj::Promise<kj::Array<kj::Own<Trace>>> PipelineTracer::onComplete() {
}

kj::Own<WorkerTracer> PipelineTracer::makeWorkerTracer(
PipelineLogLevel pipelineLogLevel, kj::Maybe<kj::String> stableId,
PipelineLogLevel pipelineLogLevel, kj::Maybe<kj::String> scriptId, kj::Maybe<kj::String> stableId,
kj::Maybe<kj::String> scriptName, kj::Maybe<kj::Own<ScriptVersion::Reader>> scriptVersion,
kj::Maybe<kj::String> dispatchNamespace, kj::Array<kj::String> scriptTags) {
auto trace = kj::refcounted<Trace>(kj::mv(stableId), kj::mv(scriptName), kj::mv(scriptVersion),
kj::mv(dispatchNamespace), kj::mv(scriptTags));
kj::mv(scriptId), kj::mv(dispatchNamespace), kj::mv(scriptTags));
traces.add(kj::addRef(*trace));
return kj::refcounted<WorkerTracer>(kj::addRef(*this), kj::mv(trace), pipelineLogLevel);
}
Expand All @@ -515,7 +526,7 @@ WorkerTracer::WorkerTracer(kj::Own<PipelineTracer> parentPipeline,
parentPipeline(kj::mv(parentPipeline)) {}
WorkerTracer::WorkerTracer(PipelineLogLevel pipelineLogLevel)
: pipelineLogLevel(pipelineLogLevel),
trace(kj::refcounted<Trace>(kj::none, kj::none, kj::none, kj::none, nullptr)) {}
trace(kj::refcounted<Trace>(kj::none, kj::none, kj::none, kj::none, kj::none, nullptr)) {}

void WorkerTracer::log(kj::Date timestamp, LogLevel logLevel, kj::String message) {
if (trace->exceededLogLimit) {
Expand All @@ -537,7 +548,7 @@ void WorkerTracer::log(kj::Date timestamp, LogLevel logLevel, kj::String message
trace->logs.add(timestamp, logLevel, kj::mv(message));
}

void WorkerTracer::addException(kj::Date timestamp, kj::String name, kj::String message) {
void WorkerTracer::addException(kj::Date timestamp, kj::String name, kj::String message, bool sourcemapsAvailable) {
if (trace->exceededExceptionLimit) {
return;
}
Expand All @@ -557,6 +568,7 @@ void WorkerTracer::addException(kj::Date timestamp, kj::String name, kj::String
}
trace->bytesUsed = newSize;
trace->exceptions.add(timestamp, kj::mv(name), kj::mv(message));
trace->sourcemapsAvailable = sourcemapsAvailable;
}

void WorkerTracer::addDiagnosticChannelEvent(kj::Date timestamp,
Expand Down
10 changes: 8 additions & 2 deletions src/workerd/io/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ enum class PipelineLogLevel {
class Trace final : public kj::Refcounted {
public:
explicit Trace(kj::Maybe<kj::String> stableId, kj::Maybe<kj::String> scriptName,
kj::Maybe<kj::Own<ScriptVersion::Reader>> scriptVersion,
kj::Maybe<kj::Own<ScriptVersion::Reader>> scriptVersion, kj::Maybe<kj::String> scriptId,
kj::Maybe<kj::String> dispatchNamespace, kj::Array<kj::String> scriptTags);
Trace(rpc::Trace::Reader reader);
~Trace() noexcept(false);
Expand Down Expand Up @@ -260,6 +260,7 @@ class Trace final : public kj::Refcounted {

kj::Maybe<kj::String> scriptName;
kj::Maybe<kj::Own<ScriptVersion::Reader>> scriptVersion;
kj::Maybe<kj::String> scriptId;
kj::Maybe<kj::String> dispatchNamespace;
kj::Array<kj::String> scriptTags;

Expand All @@ -276,6 +277,9 @@ class Trace final : public kj::Refcounted {
kj::Duration cpuTime;
kj::Duration wallTime;

// If we add an exception we do a check to see if the script has sourcemaps available so
// we can remap the stacktrace.
bool sourcemapsAvailable = false;
bool exceededLogLimit = false;
bool exceededExceptionLimit = false;
bool exceededDiagnosticChannelEventLimit = false;
Expand Down Expand Up @@ -319,6 +323,7 @@ class PipelineTracer final : public kj::Refcounted {
}

kj::Own<WorkerTracer> makeWorkerTracer(PipelineLogLevel pipelineLogLevel,
kj::Maybe<kj::String> scriptId,
kj::Maybe<kj::String> stableId,
kj::Maybe<kj::String> scriptName,
kj::Maybe<kj::Own<ScriptVersion::Reader>> scriptVersion,
Expand Down Expand Up @@ -352,7 +357,8 @@ class WorkerTracer final : public kj::Refcounted {
// TODO(soon): Eventually:
//void setMetrics(...) // Or get from MetricsCollector::Request directly?

void addException(kj::Date timestamp, kj::String name, kj::String message);
void addException(kj::Date timestamp, kj::String name,
kj::String message, bool sourcemapsAvailable);

void addDiagnosticChannelEvent(kj::Date timestamp, kj::String channel,
kj::Array<kj::byte> message);
Expand Down
2 changes: 2 additions & 0 deletions src/workerd/io/worker-interface.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ struct Trace @0x8e8d911203762d34 {
outcome @2 :EventOutcome;
scriptName @4 :Text;
scriptVersion @19 :ScriptVersion;
scriptId @21 :Text;
sourcemapsAvailable @22 :Bool;

eventTimestampNs @5 :Int64;

Expand Down
12 changes: 8 additions & 4 deletions src/workerd/io/worker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ void addExceptionToTrace(jsg::Lock& js,
message = kj::str(m);
}
// TODO(someday): Limit size of exception content?
tracer.addException(timestamp, kj::mv(name), kj::mv(message));
tracer.addException(
timestamp, kj::mv(name), kj::mv(message),
ioContext.getWorker().getScript().hasSourcemapsAvailable()
);
}

void reportStartupError(
Expand Down Expand Up @@ -1075,10 +1078,11 @@ Worker::Isolate::Isolate(kj::Own<Api> apiParam,

Worker::Script::Script(kj::Own<const Isolate> isolateParam, kj::StringPtr id,
Script::Source source, IsolateObserver::StartType startType,
bool logNewScript, kj::Maybe<ValidationErrorReporter&> errorReporter)
bool logNewScript, kj::Maybe<ValidationErrorReporter&> errorReporter, bool sourcemapsAvailable)
: isolate(kj::mv(isolateParam)),
id(kj::str(id)),
modular(source.is<ModulesSource>()),
sourcemapsAvailable(sourcemapsAvailable),
impl(kj::heap<Impl>()) {
this->isPython = false;
auto parseMetrics = isolate->metrics->parse(startType);
Expand Down Expand Up @@ -3434,11 +3438,11 @@ uint Worker::Isolate::getLockSuccessCount() const {

kj::Own<const Worker::Script> Worker::Isolate::newScript(
kj::StringPtr scriptId, Script::Source source,
IsolateObserver::StartType startType, bool logNewScript,
IsolateObserver::StartType startType, bool logNewScript, bool sourcemapAvailable,
kj::Maybe<ValidationErrorReporter&> errorReporter) const {
// Script doesn't already exist, so compile it.
return kj::atomicRefcounted<Script>(kj::atomicAddRef(*this), scriptId, kj::mv(source),
startType, logNewScript, errorReporter);
startType, logNewScript, errorReporter, sourcemapAvailable);
}

void Worker::Isolate::completedRequest() const {
Expand Down
6 changes: 4 additions & 2 deletions src/workerd/io/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class Worker::Script: public kj::AtomicRefcounted {
inline kj::StringPtr getId() const { return id; }
inline const Isolate& getIsolate() const { return *isolate; }
inline bool isModular() const { return modular; }
inline bool hasSourcemapsAvailable() const { return sourcemapsAvailable; }

struct CompiledGlobal {
jsg::V8Ref<v8::String> name;
Expand Down Expand Up @@ -209,6 +210,7 @@ class Worker::Script: public kj::AtomicRefcounted {
kj::Own<const Isolate> isolate;
kj::String id;
bool modular;
bool sourcemapsAvailable;

struct Impl;
kj::Own<Impl> impl;
Expand All @@ -218,7 +220,7 @@ class Worker::Script: public kj::AtomicRefcounted {
public: // pretend this is private (needs to be public because allocated through template)
explicit Script(kj::Own<const Isolate> isolate, kj::StringPtr id, Source source,
IsolateObserver::StartType startType, bool logNewScript,
kj::Maybe<ValidationErrorReporter&> errorReporter);
kj::Maybe<ValidationErrorReporter&> errorReporter, bool sourcemapsAvailable);
};

// Multiple zones may share the same script. We would like to compile each script only once,
Expand Down Expand Up @@ -267,7 +269,7 @@ class Worker::Isolate: public kj::AtomicRefcounted {
// Parses the given code to create a new script object and returns it.
kj::Own<const Worker::Script> newScript(
kj::StringPtr id, Script::Source source,
IsolateObserver::StartType startType, bool logNewScript = false,
IsolateObserver::StartType startType, bool logNewScript = false, bool sourcemapsAvailable = false,
kj::Maybe<ValidationErrorReporter&> errorReporter = kj::none) const;

inline const IsolateLimitEnforcer& getLimitEnforcer() const { return *limitEnforcer; }
Expand Down

0 comments on commit cb44afe

Please sign in to comment.