Skip to content

Commit

Permalink
Merge pull request #3146 from cloudflare/jsnell/move-tracing-info-typ…
Browse files Browse the repository at this point in the history
…es-out-of-Trace
  • Loading branch information
jasnell authored Nov 21, 2024
2 parents 579762d + 1d293cc commit 66ec290
Show file tree
Hide file tree
Showing 9 changed files with 384 additions and 364 deletions.
14 changes: 7 additions & 7 deletions src/workerd/api/hibernatable-web-socket.c++
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,27 @@ kj::Promise<WorkerInterface::CustomEvent::Result> HibernatableWebSocketCustomEve
auto eventParameters = consumeParams();

KJ_IF_SOME(t, incomingRequest->getWorkerTracer()) {
Trace::HibernatableWebSocketEventInfo::Type type =
[&]() -> Trace::HibernatableWebSocketEventInfo::Type {
tracing::HibernatableWebSocketEventInfo::Type type =
[&]() -> tracing::HibernatableWebSocketEventInfo::Type {
KJ_SWITCH_ONEOF(eventParameters.eventType) {
KJ_CASE_ONEOF(_, HibernatableSocketParams::Text) {
return Trace::HibernatableWebSocketEventInfo::Message{};
return tracing::HibernatableWebSocketEventInfo::Message{};
}
KJ_CASE_ONEOF(data, HibernatableSocketParams::Data) {
return Trace::HibernatableWebSocketEventInfo::Message{};
return tracing::HibernatableWebSocketEventInfo::Message{};
}
KJ_CASE_ONEOF(close, HibernatableSocketParams::Close) {
return Trace::HibernatableWebSocketEventInfo::Close{
return tracing::HibernatableWebSocketEventInfo::Close{
.code = close.code, .wasClean = close.wasClean};
}
KJ_CASE_ONEOF(_, HibernatableSocketParams::Error) {
return Trace::HibernatableWebSocketEventInfo::Error{};
return tracing::HibernatableWebSocketEventInfo::Error{};
}
}
KJ_UNREACHABLE;
}();

t.setEventInfo(context.now(), Trace::HibernatableWebSocketEventInfo(kj::mv(type)));
t.setEventInfo(context.now(), tracing::HibernatableWebSocketEventInfo(kj::mv(type)));
}

try {
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/queue.c++
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ kj::Promise<WorkerInterface::CustomEvent::Result> QueueCustomEventImpl::run(
}

KJ_IF_SOME(t, incomingRequest->getWorkerTracer()) {
t.setEventInfo(context.now(), Trace::QueueEventInfo(kj::mv(queueName), batchSize));
t.setEventInfo(context.now(), tracing::QueueEventInfo(kj::mv(queueName), batchSize));
}

// Create a custom refcounted type for holding the queueEvent so that we can pass it to the
Expand Down
83 changes: 42 additions & 41 deletions src/workerd/api/trace.c++
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ kj::Maybe<double> getTraceTimestamp(const Trace& trace) {
return (trace.eventTimestamp - kj::UNIX_EPOCH) / kj::MILLISECONDS;
}

double getTraceLogTimestamp(const Trace::Log& log) {
double getTraceLogTimestamp(const tracing::Log& log) {
if (isPredictableModeForTest()) {
return 0;
} else {
return (log.timestamp - kj::UNIX_EPOCH) / kj::MILLISECONDS;
}
}

double getTraceDiagnosticChannelEventTimestamp(const Trace::DiagnosticChannelEvent& event) {
double getTraceDiagnosticChannelEventTimestamp(const tracing::DiagnosticChannelEvent& event) {
if (isPredictableModeForTest()) {
return 0;
} else {
return (event.timestamp - kj::UNIX_EPOCH) / kj::MILLISECONDS;
}
}

kj::String getTraceLogLevel(const Trace::Log& log) {
kj::String getTraceLogLevel(const tracing::Log& log) {
switch (log.logLevel) {
case LogLevel::DEBUG_:
return kj::str("debug");
Expand All @@ -69,7 +69,7 @@ kj::String getTraceLogLevel(const Trace::Log& log) {
KJ_UNREACHABLE;
}

jsg::V8Ref<v8::Object> getTraceLogMessage(jsg::Lock& js, const Trace::Log& log) {
jsg::V8Ref<v8::Object> getTraceLogMessage(jsg::Lock& js, const tracing::Log& log) {
return js.parseJson(log.message).cast<v8::Object>(js);
}

Expand All @@ -96,7 +96,7 @@ kj::Maybe<ScriptVersion> getTraceScriptVersion(const Trace& trace) {
return trace.scriptVersion.map([](const auto& version) { return ScriptVersion(*version); });
}

double getTraceExceptionTimestamp(const Trace::Exception& ex) {
double getTraceExceptionTimestamp(const tracing::Exception& ex) {
if (isPredictableModeForTest()) {
return 0;
} else {
Expand Down Expand Up @@ -126,7 +126,7 @@ kj::String enumToStr(const Enum& var) {
}

kj::Own<TraceItem::FetchEventInfo::Request::Detail> getFetchRequestDetail(
jsg::Lock& js, const Trace& trace, const Trace::FetchEventInfo& eventInfo) {
jsg::Lock& js, const Trace& trace, const tracing::FetchEventInfo& eventInfo) {
const auto getCf = [&]() -> jsg::Optional<jsg::V8Ref<v8::Object>> {
const auto& cfJson = eventInfo.cfJson;
if (cfJson.size() > 0) {
Expand All @@ -135,9 +135,9 @@ kj::Own<TraceItem::FetchEventInfo::Request::Detail> getFetchRequestDetail(
return kj::none;
};

const auto getHeaders = [&]() -> kj::Array<Trace::FetchEventInfo::Header> {
const auto getHeaders = [&]() -> kj::Array<tracing::FetchEventInfo::Header> {
return KJ_MAP(header, eventInfo.headers) {
return Trace::FetchEventInfo::Header(kj::str(header.name), kj::str(header.value));
return tracing::FetchEventInfo::Header(kj::str(header.name), kj::str(header.value));
};
};

Expand All @@ -148,43 +148,43 @@ kj::Own<TraceItem::FetchEventInfo::Request::Detail> getFetchRequestDetail(
kj::Maybe<TraceItem::EventInfo> getTraceEvent(jsg::Lock& js, const Trace& trace) {
KJ_IF_SOME(e, trace.eventInfo) {
KJ_SWITCH_ONEOF(e) {
KJ_CASE_ONEOF(fetch, Trace::FetchEventInfo) {
KJ_CASE_ONEOF(fetch, tracing::FetchEventInfo) {
return kj::Maybe(
jsg::alloc<TraceItem::FetchEventInfo>(js, trace, fetch, trace.fetchResponseInfo));
}
KJ_CASE_ONEOF(jsRpc, Trace::JsRpcEventInfo) {
KJ_CASE_ONEOF(jsRpc, tracing::JsRpcEventInfo) {
return kj::Maybe(jsg::alloc<TraceItem::JsRpcEventInfo>(trace, jsRpc));
}
KJ_CASE_ONEOF(scheduled, Trace::ScheduledEventInfo) {
KJ_CASE_ONEOF(scheduled, tracing::ScheduledEventInfo) {
return kj::Maybe(jsg::alloc<TraceItem::ScheduledEventInfo>(trace, scheduled));
}
KJ_CASE_ONEOF(alarm, Trace::AlarmEventInfo) {
KJ_CASE_ONEOF(alarm, tracing::AlarmEventInfo) {
return kj::Maybe(jsg::alloc<TraceItem::AlarmEventInfo>(trace, alarm));
}
KJ_CASE_ONEOF(queue, Trace::QueueEventInfo) {
KJ_CASE_ONEOF(queue, tracing::QueueEventInfo) {
return kj::Maybe(jsg::alloc<TraceItem::QueueEventInfo>(trace, queue));
}
KJ_CASE_ONEOF(email, Trace::EmailEventInfo) {
KJ_CASE_ONEOF(email, tracing::EmailEventInfo) {
return kj::Maybe(jsg::alloc<TraceItem::EmailEventInfo>(trace, email));
}
KJ_CASE_ONEOF(tracedTrace, Trace::TraceEventInfo) {
KJ_CASE_ONEOF(tracedTrace, tracing::TraceEventInfo) {
return kj::Maybe(jsg::alloc<TraceItem::TailEventInfo>(trace, tracedTrace));
}
KJ_CASE_ONEOF(hibWs, Trace::HibernatableWebSocketEventInfo) {
KJ_CASE_ONEOF(hibWs, tracing::HibernatableWebSocketEventInfo) {
KJ_SWITCH_ONEOF(hibWs.type) {
KJ_CASE_ONEOF(message, Trace::HibernatableWebSocketEventInfo::Message) {
KJ_CASE_ONEOF(message, tracing::HibernatableWebSocketEventInfo::Message) {
return kj::Maybe(jsg::alloc<TraceItem::HibernatableWebSocketEventInfo>(trace, message));
}
KJ_CASE_ONEOF(close, Trace::HibernatableWebSocketEventInfo::Close) {
KJ_CASE_ONEOF(close, tracing::HibernatableWebSocketEventInfo::Close) {
return kj::Maybe(jsg::alloc<TraceItem::HibernatableWebSocketEventInfo>(trace, close));
}
KJ_CASE_ONEOF(error, Trace::HibernatableWebSocketEventInfo::Error) {
KJ_CASE_ONEOF(error, tracing::HibernatableWebSocketEventInfo::Error) {
return kj::Maybe(jsg::alloc<TraceItem::HibernatableWebSocketEventInfo>(trace, error));
}
}
KJ_UNREACHABLE;
}
KJ_CASE_ONEOF(custom, Trace::CustomEventInfo) {
KJ_CASE_ONEOF(custom, tracing::CustomEventInfo) {
return kj::Maybe(jsg::alloc<TraceItem::CustomEventInfo>(trace, custom));
}
}
Expand Down Expand Up @@ -304,13 +304,13 @@ uint TraceItem::getWallTime() {

TraceItem::FetchEventInfo::FetchEventInfo(jsg::Lock& js,
const Trace& trace,
const Trace::FetchEventInfo& eventInfo,
kj::Maybe<const Trace::FetchResponseInfo&> responseInfo)
const tracing::FetchEventInfo& eventInfo,
kj::Maybe<const tracing::FetchResponseInfo&> responseInfo)
: request(jsg::alloc<Request>(js, trace, eventInfo)),
response(responseInfo.map([&](auto& info) { return jsg::alloc<Response>(trace, info); })) {}

TraceItem::FetchEventInfo::Request::Detail::Detail(jsg::Optional<jsg::V8Ref<v8::Object>> cf,
kj::Array<Trace::FetchEventInfo::Header> headers,
kj::Array<tracing::FetchEventInfo::Header> headers,
kj::String method,
kj::String url)
: cf(kj::mv(cf)),
Expand All @@ -330,7 +330,7 @@ jsg::Optional<jsg::Ref<TraceItem::FetchEventInfo::Response>> TraceItem::FetchEve
}

TraceItem::FetchEventInfo::Request::Request(
jsg::Lock& js, const Trace& trace, const Trace::FetchEventInfo& eventInfo)
jsg::Lock& js, const Trace& trace, const tracing::FetchEventInfo& eventInfo)
: detail(getFetchRequestDetail(js, trace, eventInfo)) {}

TraceItem::FetchEventInfo::Request::Request(Detail& detail, bool redacted)
Expand Down Expand Up @@ -375,23 +375,23 @@ jsg::Ref<TraceItem::FetchEventInfo::Request> TraceItem::FetchEventInfo::Request:
}

TraceItem::FetchEventInfo::Response::Response(
const Trace& trace, const Trace::FetchResponseInfo& responseInfo)
const Trace& trace, const tracing::FetchResponseInfo& responseInfo)
: status(responseInfo.statusCode) {}

uint16_t TraceItem::FetchEventInfo::Response::getStatus() {
return status;
}

TraceItem::JsRpcEventInfo::JsRpcEventInfo(
const Trace& trace, const Trace::JsRpcEventInfo& eventInfo)
const Trace& trace, const tracing::JsRpcEventInfo& eventInfo)
: rpcMethod(kj::str(eventInfo.methodName)) {}

kj::StringPtr TraceItem::JsRpcEventInfo::getRpcMethod() {
return rpcMethod;
}

TraceItem::ScheduledEventInfo::ScheduledEventInfo(
const Trace& trace, const Trace::ScheduledEventInfo& eventInfo)
const Trace& trace, const tracing::ScheduledEventInfo& eventInfo)
: scheduledTime(eventInfo.scheduledTime),
cron(kj::str(eventInfo.cron)) {}

Expand All @@ -403,15 +403,15 @@ kj::StringPtr TraceItem::ScheduledEventInfo::getCron() {
}

TraceItem::AlarmEventInfo::AlarmEventInfo(
const Trace& trace, const Trace::AlarmEventInfo& eventInfo)
const Trace& trace, const tracing::AlarmEventInfo& eventInfo)
: scheduledTime(eventInfo.scheduledTime) {}

kj::Date TraceItem::AlarmEventInfo::getScheduledTime() {
return scheduledTime;
}

TraceItem::QueueEventInfo::QueueEventInfo(
const Trace& trace, const Trace::QueueEventInfo& eventInfo)
const Trace& trace, const tracing::QueueEventInfo& eventInfo)
: queueName(kj::str(eventInfo.queueName)),
batchSize(eventInfo.batchSize) {}

Expand All @@ -424,7 +424,7 @@ uint32_t TraceItem::QueueEventInfo::getBatchSize() {
}

TraceItem::EmailEventInfo::EmailEventInfo(
const Trace& trace, const Trace::EmailEventInfo& eventInfo)
const Trace& trace, const tracing::EmailEventInfo& eventInfo)
: mailFrom(kj::str(eventInfo.mailFrom)),
rcptTo(kj::str(eventInfo.rcptTo)),
rawSize(eventInfo.rawSize) {}
Expand All @@ -442,13 +442,14 @@ uint32_t TraceItem::EmailEventInfo::getRawSize() {
}

kj::Array<jsg::Ref<TraceItem::TailEventInfo::TailItem>> getConsumedEventsFromEventInfo(
const Trace::TraceEventInfo& eventInfo) {
const tracing::TraceEventInfo& eventInfo) {
return KJ_MAP(t, eventInfo.traces) -> jsg::Ref<TraceItem::TailEventInfo::TailItem> {
return jsg::alloc<TraceItem::TailEventInfo::TailItem>(t);
};
}

TraceItem::TailEventInfo::TailEventInfo(const Trace& trace, const Trace::TraceEventInfo& eventInfo)
TraceItem::TailEventInfo::TailEventInfo(
const Trace& trace, const tracing::TraceEventInfo& eventInfo)
: consumedEvents(getConsumedEventsFromEventInfo(eventInfo)) {}

kj::Array<jsg::Ref<TraceItem::TailEventInfo::TailItem>> TraceItem::TailEventInfo::
Expand All @@ -458,15 +459,15 @@ kj::Array<jsg::Ref<TraceItem::TailEventInfo::TailItem>> TraceItem::TailEventInfo
};
}

TraceItem::TailEventInfo::TailItem::TailItem(const Trace::TraceEventInfo::TraceItem& traceItem)
TraceItem::TailEventInfo::TailItem::TailItem(const tracing::TraceEventInfo::TraceItem& traceItem)
: scriptName(traceItem.scriptName.map([](auto& s) { return kj::str(s); })) {}

kj::Maybe<kj::StringPtr> TraceItem::TailEventInfo::TailItem::getScriptName() {
return scriptName;
}

TraceDiagnosticChannelEvent::TraceDiagnosticChannelEvent(
const Trace& trace, const Trace::DiagnosticChannelEvent& eventInfo)
const Trace& trace, const tracing::DiagnosticChannelEvent& eventInfo)
: timestamp(getTraceDiagnosticChannelEventTimestamp(eventInfo)),
channel(kj::heapString(eventInfo.channel)),
message(kj::heapArray<kj::byte>(eventInfo.message)) {}
Expand Down Expand Up @@ -509,19 +510,19 @@ ScriptVersion::ScriptVersion(const ScriptVersion& other)
message{other.message.map([](const auto& message) { return kj::str(message); })} {}

TraceItem::CustomEventInfo::CustomEventInfo(
const Trace& trace, const Trace::CustomEventInfo& eventInfo)
const Trace& trace, const tracing::CustomEventInfo& eventInfo)
: eventInfo(eventInfo) {}

TraceItem::HibernatableWebSocketEventInfo::HibernatableWebSocketEventInfo(
const Trace& trace, const Trace::HibernatableWebSocketEventInfo::Message& eventInfo)
const Trace& trace, const tracing::HibernatableWebSocketEventInfo::Message& eventInfo)
: eventType(jsg::alloc<TraceItem::HibernatableWebSocketEventInfo::Message>(trace, eventInfo)) {}

TraceItem::HibernatableWebSocketEventInfo::HibernatableWebSocketEventInfo(
const Trace& trace, const Trace::HibernatableWebSocketEventInfo::Close& eventInfo)
const Trace& trace, const tracing::HibernatableWebSocketEventInfo::Close& eventInfo)
: eventType(jsg::alloc<TraceItem::HibernatableWebSocketEventInfo::Close>(trace, eventInfo)) {}

TraceItem::HibernatableWebSocketEventInfo::HibernatableWebSocketEventInfo(
const Trace& trace, const Trace::HibernatableWebSocketEventInfo::Error& eventInfo)
const Trace& trace, const tracing::HibernatableWebSocketEventInfo::Error& eventInfo)
: eventType(jsg::alloc<TraceItem::HibernatableWebSocketEventInfo::Error>(trace, eventInfo)) {}

TraceItem::HibernatableWebSocketEventInfo::Type TraceItem::HibernatableWebSocketEventInfo::
Expand All @@ -548,7 +549,7 @@ bool TraceItem::HibernatableWebSocketEventInfo::Close::getWasClean() {
return eventInfo.wasClean;
}

TraceLog::TraceLog(jsg::Lock& js, const Trace& trace, const Trace::Log& log)
TraceLog::TraceLog(jsg::Lock& js, const Trace& trace, const tracing::Log& log)
: timestamp(getTraceLogTimestamp(log)),
level(getTraceLogLevel(log)),
message(getTraceLogMessage(js, log)) {}
Expand All @@ -565,7 +566,7 @@ jsg::V8Ref<v8::Object> TraceLog::getMessage(jsg::Lock& js) {
return message.addRef(js);
}

TraceException::TraceException(const Trace& trace, const Trace::Exception& exception)
TraceException::TraceException(const Trace& trace, const tracing::Exception& exception)
: timestamp(getTraceExceptionTimestamp(exception)),
name(kj::str(exception.name)),
message(kj::str(exception.message)),
Expand Down Expand Up @@ -604,7 +605,7 @@ kj::Promise<void> sendTracesToExportedHandler(kj::Own<IoContext::IncomingRequest
auto& metrics = incomingRequest->getMetrics();

KJ_IF_SOME(t, incomingRequest->getWorkerTracer()) {
t.setEventInfo(context.now(), Trace::TraceEventInfo(traces));
t.setEventInfo(context.now(), tracing::TraceEventInfo(traces));
}

auto nonEmptyTraces = kj::Vector<kj::Own<Trace>>(kj::size(traces));
Expand Down
Loading

0 comments on commit 66ec290

Please sign in to comment.