Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new event info type email #249

Merged
merged 1 commit into from
Jan 6, 2023
Merged
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
18 changes: 18 additions & 0 deletions src/workerd/api/trace.c++
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ kj::Maybe<TraceItem::EventInfo> TraceItem::getEvent() {
KJ_CASE_ONEOF(queue, Trace::QueueEventInfo) {
return kj::Maybe(jsg::alloc<QueueEventInfo>(kj::addRef(*trace), queue));
}
KJ_CASE_ONEOF(email, Trace::EmailEventInfo) {
return kj::Maybe(jsg::alloc<EmailEventInfo>(kj::addRef(*trace), email));
}
KJ_CASE_ONEOF(custom, Trace::CustomEventInfo) {
return kj::Maybe(jsg::alloc<CustomEventInfo>(kj::addRef(*trace), custom));
}
Expand Down Expand Up @@ -188,6 +191,21 @@ uint32_t TraceItem::QueueEventInfo::getBatchSize() {
return eventInfo.batchSize;
}

TraceItem::EmailEventInfo::EmailEventInfo(kj::Own<Trace> trace,
const Trace::EmailEventInfo& eventInfo) : trace(kj::mv(trace)), eventInfo(eventInfo) {}

kj::StringPtr TraceItem::EmailEventInfo::getMailFrom() {
return eventInfo.mailFrom;
}

kj::StringPtr TraceItem::EmailEventInfo::getRcptTo() {
return eventInfo.rcptTo;
}

uint32_t TraceItem::EmailEventInfo::getRawSize() {
return eventInfo.rawSize;
}

TraceItem::CustomEventInfo::CustomEventInfo(kj::Own<Trace> trace,
const Trace::CustomEventInfo& eventInfo) : trace(kj::mv(trace)), eventInfo(eventInfo) {}

Expand Down
24 changes: 23 additions & 1 deletion src/workerd/api/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ class TraceItem final: public jsg::Object {
class ScheduledEventInfo;
class AlarmEventInfo;
class QueueEventInfo;
class EmailEventInfo;
class CustomEventInfo;

explicit TraceItem(kj::Own<Trace> trace);

typedef kj::OneOf<jsg::Ref<FetchEventInfo>, jsg::Ref<ScheduledEventInfo>,
jsg::Ref<AlarmEventInfo>, jsg::Ref<QueueEventInfo>, jsg::Ref<CustomEventInfo>> EventInfo;
jsg::Ref<AlarmEventInfo>, jsg::Ref<QueueEventInfo>,
jsg::Ref<EmailEventInfo>, jsg::Ref<CustomEventInfo>> EventInfo;
kj::Maybe<EventInfo> getEvent();
// TODO(someday): support more event types (trace, email) via kj::OneOf.
kj::Maybe<double> getEventTimestamp();
Expand Down Expand Up @@ -209,6 +211,25 @@ class TraceItem::QueueEventInfo final: public jsg::Object {
const Trace::QueueEventInfo& eventInfo;
};

class TraceItem::EmailEventInfo final: public jsg::Object {
public:
explicit EmailEventInfo(kj::Own<Trace> trace, const Trace::EmailEventInfo& eventInfo);

kj::StringPtr getMailFrom();
kj::StringPtr getRcptTo();
uint32_t getRawSize();

JSG_RESOURCE_TYPE(EmailEventInfo) {
JSG_READONLY_PROTOTYPE_PROPERTY(mailFrom, getMailFrom);
JSG_READONLY_PROTOTYPE_PROPERTY(rcptTo, getRcptTo);
JSG_READONLY_PROTOTYPE_PROPERTY(rawSize, getRawSize);
}

private:
kj::Own<Trace> trace;
const Trace::EmailEventInfo& eventInfo;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this is a new problem at all but I do generally have a concern about using const refs in jsg::Objects like this. What guarantee do we have that the ref is going to stay alive for as long as the JS wrapper? For instance, if someone happens to store the info object at global scope, for instance, what happens when the properties are accessed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, we should look at this.

};

class TraceItem::CustomEventInfo final: public jsg::Object {
public:
explicit CustomEventInfo(kj::Own<Trace> trace, const Trace::CustomEventInfo& eventInfo);
Expand Down Expand Up @@ -320,6 +341,7 @@ class TraceCustomEventImpl final: public WorkerInterface::CustomEvent {
api::TraceItem::CustomEventInfo, \
api::TraceItem::ScheduledEventInfo, \
api::TraceItem::QueueEventInfo, \
api::TraceItem::EmailEventInfo, \
api::TraceItem::FetchEventInfo, \
api::TraceItem::FetchEventInfo::Request, \
api::TraceItem::FetchEventInfo::Response, \
Expand Down
22 changes: 22 additions & 0 deletions src/workerd/io/trace.c++
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ void Trace::QueueEventInfo::copyTo(rpc::Trace::QueueEventInfo::Builder builder)
builder.setBatchSize(batchSize);
}

Trace::EmailEventInfo::EmailEventInfo(kj::String mailFrom, kj::String rcptTo, uint32_t rawSize)
: mailFrom(kj::mv(mailFrom)), rcptTo(kj::mv(rcptTo)), rawSize(rawSize) {}

Trace::EmailEventInfo::EmailEventInfo(rpc::Trace::EmailEventInfo::Reader reader)
: mailFrom(kj::heapString(reader.getMailFrom())),
rcptTo(kj::heapString(reader.getRcptTo())),
rawSize(reader.getRawSize()) {}

void Trace::EmailEventInfo::copyTo(rpc::Trace::EmailEventInfo::Builder builder) {
builder.setMailFrom(mailFrom);
builder.setRcptTo(rcptTo);
builder.setRawSize(rawSize);
}

Trace::FetchResponseInfo::FetchResponseInfo(uint16_t statusCode)
: statusCode(statusCode) {}

Expand Down Expand Up @@ -175,6 +189,10 @@ void Trace::copyTo(rpc::Trace::Builder builder) {
auto queueBuilder = eventInfoBuilder.initQueue();
queue.copyTo(queueBuilder);
}
KJ_CASE_ONEOF(email, EmailEventInfo) {
auto emailBuilder = eventInfoBuilder.initEmail();
email.copyTo(emailBuilder);
}
KJ_CASE_ONEOF(custom, CustomEventInfo) {
eventInfoBuilder.initCustom();
}
Expand Down Expand Up @@ -248,6 +266,9 @@ void Trace::mergeFrom(rpc::Trace::Reader reader, PipelineLogLevel pipelineLogLev
case rpc::Trace::EventInfo::Which::QUEUE:
eventInfo = QueueEventInfo(e.getQueue());
break;
case rpc::Trace::EventInfo::Which::EMAIL:
eventInfo = EmailEventInfo(e.getEmail());
break;
case rpc::Trace::EventInfo::Which::CUSTOM:
eventInfo = CustomEventInfo(e.getCustom());
break;
Expand Down Expand Up @@ -435,6 +456,7 @@ void WorkerTracer::setEventInfo(kj::Date timestamp, Trace::EventInfo&& info) {
KJ_CASE_ONEOF(_, Trace::ScheduledEventInfo) {}
KJ_CASE_ONEOF(_, Trace::AlarmEventInfo) {}
KJ_CASE_ONEOF(_, Trace::QueueEventInfo) {}
KJ_CASE_ONEOF(_, Trace::EmailEventInfo) {}
KJ_CASE_ONEOF(_, Trace::CustomEventInfo) {}
}
trace->bytesUsed = newSize;
Expand Down
14 changes: 13 additions & 1 deletion src/workerd/io/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ class Trace final : public kj::Refcounted {
void copyTo(rpc::Trace::QueueEventInfo::Builder builder);
};

class EmailEventInfo {
public:
explicit EmailEventInfo(kj::String mailFrom, kj::String rcptTo, uint32_t rawSize);
EmailEventInfo(rpc::Trace::EmailEventInfo::Reader reader);

kj::String mailFrom;
kj::String rcptTo;
uint32_t rawSize;

void copyTo(rpc::Trace::EmailEventInfo::Builder builder);
};

class CustomEventInfo {
public:
explicit CustomEventInfo() {};
Expand Down Expand Up @@ -172,7 +184,7 @@ class Trace final : public kj::Refcounted {
// We treat the origin value as "unset".

typedef kj::OneOf<FetchEventInfo, ScheduledEventInfo, AlarmEventInfo, QueueEventInfo,
CustomEventInfo> EventInfo;
EmailEventInfo, CustomEventInfo> EventInfo;
kj::Maybe<EventInfo> eventInfo;
// TODO(someday): Support more event types.
// TODO(someday): Work out what sort of information we may want to convey about the parent
Expand Down
7 changes: 7 additions & 0 deletions src/workerd/io/worker-interface.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct Trace @0x8e8d911203762d34 {
alarm @9 :AlarmEventInfo;
queue @15 :QueueEventInfo;
custom @13 :CustomEventInfo;
email @16 :EmailEventInfo;
}
struct FetchEventInfo {
method @0 :HttpMethod;
Expand Down Expand Up @@ -76,6 +77,12 @@ struct Trace @0x8e8d911203762d34 {
batchSize @1 :UInt32;
}

struct EmailEventInfo {
mailFrom @0 :Text;
rcptTo @1 :Text;
rawSize @2 :UInt32;
}

struct CustomEventInfo {}

response @8 :FetchResponseInfo;
Expand Down