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

Add more Jaeger spans to Worker constructor #114

Merged
merged 1 commit into from
Oct 17, 2022
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
1 change: 1 addition & 0 deletions src/workerd/io/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ class MaybeSpan {
};

bool operator==(std::nullptr_t) { return span == nullptr; }
explicit operator bool() { return span != nullptr; }

kj::Maybe<Jaeger::SpanContext> getSpanContext() {
KJ_IF_MAYBE(s, span) {
Expand Down
28 changes: 18 additions & 10 deletions src/workerd/io/worker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1253,8 +1253,21 @@ Worker::Worker(kj::Own<const Script> scriptParam,
}
});

auto maybeMakeSpan = [&](auto operationName) -> MaybeSpan {
if (auto span = systemTracer.makeSpan(operationName)) {
span.setTag("truncated_script_id"_kj, truncateScriptId(script->getId()));
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess all the spans created this way would have a common truncated_script_id tag... not sure if there is a point at which it becomes more efficient to just have it present once in a parent span. But I suppose there is also a convenience factor in being able to search for spans by the truncated_string_id tag, too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is intentional, precisely for the convenience you mention. In theory, it should be perfectly possible to perform some sort of graph query that expresses, "find me all sub-spans of a span with a particular value for truncated_script_id", or "find me the truncated_script_id value of the ancestor span of a particular span that I have identified", but in practice this is difficult to express, and slow to execute, with the tools we have.

return kj::mv(span);
} else {
return MaybeSpan();
}
};

MaybeSpan currentSpan = maybeMakeSpan("lw:new_startup_metrics"_kj);

auto startupMetrics = metrics->startup(startType);

currentSpan = maybeMakeSpan("lw:new_context");

// Create a stack-allocated handle scope.
v8::HandleScope handleScope(lock.v8Isolate);

Expand All @@ -1264,6 +1277,7 @@ Worker::Worker(kj::Own<const Script> scriptParam,
// const_cast OK because guarded by `lock`.
context = const_cast<jsg::JsContext<api::ServiceWorkerGlobalScope>*>(c)
->getHandle(lock.v8Isolate);
currentSpan.setTag("module_context", true);
} else {
// Create a new context.
context = this->impl->context.emplace(script->isolate->apiIsolate->newContext(lock))
Expand All @@ -1286,11 +1300,8 @@ Worker::Worker(kj::Own<const Script> scriptParam,

try {
try {
MaybeSpan instantiationSpan = systemTracer.makeSpan(
"lw:globals_instantiation"_kj, systemTracer.getSpanContext());
if (instantiationSpan != nullptr) {
instantiationSpan.setTag("truncated_script_id"_kj, truncateScriptId(script->getId()));
}
currentSpan = maybeMakeSpan("lw:globals_instantiation"_kj);

v8::Local<v8::Object> bindingsScope;
if (script->isModular()) {
// Use `env` variable.
Expand All @@ -1317,11 +1328,8 @@ Worker::Worker(kj::Own<const Script> scriptParam,
compileBindings(lock, *script->isolate->apiIsolate, bindingsScope);

// Execute script.
MaybeSpan executionSpan = systemTracer.makeSpan(
"lw:top_level_execution"_kj, systemTracer.getSpanContext());
if (executionSpan != nullptr) {
executionSpan.setTag("truncated_script_id"_kj, truncateScriptId(script->getId()));
}
currentSpan = maybeMakeSpan("lw:top_level_execution"_kj);

KJ_SWITCH_ONEOF(script->impl->unboundScriptOrMainModule) {
KJ_CASE_ONEOF(unboundScript, jsg::NonModuleScript) {
auto limitScope = script->isolate->getLimitEnforcer().enterStartupJs(lock, maybeLimitError);
Expand Down