Skip to content
Open
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
5 changes: 1 addition & 4 deletions src/workerd/jsg/util.c++
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,8 @@ v8::Local<v8::Value> exceptionToJs(
.internalErrorId = shouldLogWithInternalId ? tunneledException.internalErrorId : kj::none,
});
if (shouldLogWithInternalId) {
// LOG_EXCEPTION("jsgInternalError", ...), but with internal error ID:
auto& e = exception;
constexpr auto sentryErrorContext = "jsgInternalError";
auto& wdErrId = KJ_ASSERT_NONNULL(tunneledException.internalErrorId);
KJ_LOG(ERROR, e, sentryErrorContext, wdErrId);
LOG_EXCEPTION_WITH_ID("jsgInternalError", exception, wdErrId);
} else {
KJ_LOG(INFO, exception); // Run with --verbose to see exception logs.
}
Expand Down
7 changes: 7 additions & 0 deletions src/workerd/util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ kj_test(
],
)

kj_test(
src = "sentry-test.c++",
deps = [
":sentry",
],
)

kj_test(
size = "large",
src = "sqlite-test.c++",
Expand Down
34 changes: 34 additions & 0 deletions src/workerd/util/sentry-test.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2026 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

#include "sentry.h"

#include <kj/test.h>

namespace workerd {
namespace {

void expectSentryDisposition(kj::StringPtr disposition) {
auto exception = KJ_EXCEPTION(FAILED, "test-error");
exception.setDetail(SENTRY_DISPOSITION_DETAIL_ID, kj::heapArray(disposition.asBytes()));

auto loggingException = KJ_ASSERT_NONNULL(_::tagSentryExceptionForLogging(exception));
KJ_EXPECT(loggingException.getDescription() == kj::str(disposition, " test-error"));

KJ_EXPECT_LOG(ERROR, disposition);
LOG_EXCEPTION("sentryDispositionTest", exception);
KJ_EXPECT_LOG(ERROR, disposition);
auto wdErrId = makeInternalErrorId();
LOG_EXCEPTION_WITH_ID("sentryDispositionWithIdTest", exception, wdErrId);
KJ_EXPECT(exception.getDescription() == "test-error", exception);
}

KJ_TEST("Sentry dispositions are applied only when logging") {
expectSentryDisposition("SENTRY_DO"_kj);
expectSentryDisposition("SENTRY_RT"_kj);
expectSentryDisposition("NOSENTRY"_kj);
}

} // namespace
} // namespace workerd
36 changes: 32 additions & 4 deletions src/workerd/util/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@

namespace workerd {

constexpr kj::Exception::DetailTypeId SENTRY_DISPOSITION_DETAIL_ID = 0xd3b1bbd08ecf715ull;

namespace _ { // private

inline kj::Maybe<kj::Exception> tagSentryExceptionForLogging(const kj::Exception& exception) {
KJ_IF_SOME(dispositionBytes, exception.getDetail(SENTRY_DISPOSITION_DETAIL_ID)) {
auto disposition = kj::str(dispositionBytes.asChars());
if (disposition.size() > 0 && !exception.getDescription().contains(disposition)) {
auto result = exception.clone();
result.setDescription(kj::str(disposition, " ", result.getDescription()));
return kj::mv(result);
}
}
return kj::none;
}

} // namespace _

// For internal errors, we generate an ID to include when rendering user-facing "internal error"
// exceptions and writing internal exception logs, to make it easier to search for logs
// corresponding to "internal error" exceptions reported by users.
Expand All @@ -34,16 +52,26 @@ InternalErrorId makeInternalErrorId();
// from the macro so that we do not accidentally make a more granular fingerprint. It also will only
// take a `context` argument that is known at compile time (via constexpr assignment).
#define LOG_EXCEPTION(context, exception) \
[&](const kj::Exception& e) { \
[&](const kj::Exception& untagged) { \
constexpr auto sentryErrorContext = context; \
auto tagged = ::workerd::_::tagSentryExceptionForLogging(untagged); \
auto& e = [&]() -> const kj::Exception& { \
KJ_IF_SOME(e, tagged) return e; \
return untagged; \
}(); \
KJ_LOG(ERROR, e, sentryErrorContext); \
}(exception)

#define LOG_EXCEPTION_WITH_ID(context, exception, id) \
[&](const kj::Exception& e) { \
[&](const kj::Exception& untagged, const ::workerd::InternalErrorId& wdErrId) { \
constexpr auto sentryErrorContext = context; \
KJ_LOG(ERROR, e, sentryErrorContext, id); \
}(exception)
auto tagged = ::workerd::_::tagSentryExceptionForLogging(untagged); \
auto& e = [&]() -> const kj::Exception& { \
KJ_IF_SOME(e, tagged) return e; \
return untagged; \
}(); \
KJ_LOG(ERROR, e, sentryErrorContext, wdErrId); \
}(exception, id)

#define ACTOR_STORAGE_OP_PREFIX "; actorStorageOp = "

Expand Down
Loading