@web3-storage/toucan-js
Version:
Cloudflare Workers client for Sentry (fork)
784 lines (777 loc) • 29.6 kB
JavaScript
'use strict';
var core = require('@sentry/core');
var utils = require('@sentry/utils');
var cookie = require('cookie');
var stacktraceJs = require('stacktrace-js');
var hub = require('@sentry/hub');
/**
* Parts of code taken from: https://github.com/getsentry/sentry-javascript/blob/06d6bd87971b22dcaba99b03e1f885158c7dd66f/packages/hub/src/scope.ts
*/
class SentryScopeAdapter extends hub.Scope {
/**
* Applies the current context to the event.
*
* @param event Event
*/
applyToEventSync(event) {
var _a, _b, _c, _d;
if (this._extra && Object.keys(this._extra).length) {
event.extra = { ...this._extra, ...event.extra };
}
if (this._tags && Object.keys(this._tags).length) {
event.tags = { ...this._tags, ...event.tags };
}
if (this._user && Object.keys(this._user).length) {
event.user = { ...this._user, ...event.user };
}
event.fingerprint = [
...((_a = event.fingerprint) !== null && _a !== void 0 ? _a : []),
...((_b = this._fingerprint) !== null && _b !== void 0 ? _b : []),
];
event.fingerprint =
event.fingerprint.length > 0 ? event.fingerprint : undefined;
event.breadcrumbs = [
...((_c = event.breadcrumbs) !== null && _c !== void 0 ? _c : []),
...((_d = this._breadcrumbs) !== null && _d !== void 0 ? _d : []),
];
event.breadcrumbs =
event.breadcrumbs.length > 0 ? event.breadcrumbs : undefined;
return event;
}
/**
* Inherit values from the parent scope.
* @param scope to clone.
*/
static clone(scope) {
const newScope = new SentryScopeAdapter();
if (scope) {
newScope._breadcrumbs = [...scope._breadcrumbs];
newScope._tags = { ...scope._tags };
newScope._extra = { ...scope._extra };
newScope._contexts = { ...scope._contexts };
newScope._user = scope._user;
newScope._level = scope._level;
newScope._span = scope._span;
newScope._session = scope._session;
newScope._transactionName = scope._transactionName;
newScope._fingerprint = scope._fingerprint;
newScope._eventProcessors = [...scope._eventProcessors];
}
return newScope;
}
}
class Scope {
constructor() {
this.adapter = new SentryScopeAdapter();
}
/**
* Sets the breadcrumbs in the scope
*
* @param breadcrumb
* @param maxBreadcrumbs
*/
addBreadcrumb(breadcrumb, maxBreadcrumbs) {
// Type-casting 'breadcrumb' to any because our level type is a union of literals, as opposed to Level enum.
return this.adapter.addBreadcrumb(breadcrumb, maxBreadcrumbs);
}
/**
* Set key:value that will be sent as tags data with the event.
*
* @param key String key of tag
* @param value Primitive value of tag
*/
setTag(key, value) {
this.adapter.setTag(key, value);
}
/**
* Set an object that will be merged sent as tags data with the event.
*
* @param tags Tags context object to merge into current context.
*/
setTags(tags) {
this.adapter.setTags(tags);
}
/**
* Set key:value that will be sent as extra data with the event.
*
* @param key String key of extra
* @param extra Extra value of extra
*/
setExtra(key, extra) {
this.adapter.setExtra(key, extra);
}
/**
* Set an object that will be merged sent as extra data with the event.
*
* @param extras Extras context object to merge into current context.
*/
setExtras(extras) {
this.adapter.setExtras(extras);
}
/**
* Overrides the Sentry default grouping. See https://docs.sentry.io/data-management/event-grouping/sdk-fingerprinting/
*
* @param fingerprint Array of strings used to override the Sentry default grouping.
*/
setFingerprint(fingerprint) {
this.adapter.setFingerprint(fingerprint);
}
/**
* Updates user context information for future events.
*
* @param user — User context object to be set in the current context. Pass null to unset the user.
*/
setUser(user) {
this.adapter.setUser(user);
}
/**
* Applies the current context to the event.
*
* @param event Event
*/
applyToEvent(event) {
return this.adapter.applyToEventSync(event);
}
/**
* Inherit values from the parent scope.
* @param scope to clone.
*/
static clone(scope) {
const newScope = new Scope();
if (scope) {
newScope.adapter = SentryScopeAdapter.clone(scope.adapter);
}
return newScope;
}
}
/**
* Checks the given sample rate to make sure it is valid type and value (a boolean, or a number between 0 and 1).
*/
function isValidSampleRate(rate) {
// we need to check NaN explicitly because it's of type 'number' and therefore wouldn't get caught by this typecheck
if (!((typeof rate === 'number' && !isNaN(rate)) || typeof rate === 'boolean')) {
return false;
}
// in case sampleRate is a boolean, it will get automatically cast to 1 if it's true and 0 if it's false
if (rate < 0 || rate > 1) {
return false;
}
return true;
}
/**
* Determines if tracing is currently enabled.
*
* Tracing is enabled when at least one of `tracesSampleRate` and `tracesSampler` is defined in the SDK config.
*/
function hasTracingEnabled(options) {
return ('sampleRate' in options ||
'tracesSampleRate' in options ||
'tracesSampler' in options);
}
class Toucan {
constructor(options) {
/**
* Default maximum number of breadcrumbs added to an event. Can be overwritten
* with {@link Options.maxBreadcrumbs}.
*/
this.DEFAULT_BREADCRUMBS = 100;
/**
* Absolute maximum number of breadcrumbs added to an event. The
* `maxBreadcrumbs` option cannot be higher than this value.
*/
this.MAX_BREADCRUMBS = 100;
this.scopes = [new Scope()];
this.options = options;
if (!options.dsn || options.dsn.length === 0) {
// If an empty DSN is passed, we should treat it as valid option which signifies disabling the SDK.
this.url = '';
this.disabled = true;
this.debug(() => this.log('dsn missing, SDK is disabled'));
}
else {
this.url = new core.API(options.dsn).getStoreEndpointWithUrlEncodedAuth();
this.disabled = false;
this.debug(() => this.log(`dsn parsed, full store endpoint: ${this.url}`));
}
// This is to maintain backwards compatibility for 'event' option. When we remove it, all this complex logic can go away.
if ('context' in options && options.context.request) {
this.request = this.toSentryRequest(options.context.request);
}
else if ('request' in options && options.request) {
this.request = this.toSentryRequest(options.request);
}
else if ('event' in options && 'request' in options.event) {
this.request = this.toSentryRequest(options.event.request);
}
this.beforeSend = this.beforeSend.bind(this);
/**
* Wrap all class methods in a proxy that:
* 1. Wraps all code in try/catch to handle internal erros gracefully.
* 2. Prevents execution if disabled = true
*/
return new Proxy(this, {
get: (target, key, receiver) => {
return (...args) => {
if (this.disabled)
return;
try {
return Reflect.get(target, key, receiver).apply(target, args);
}
catch (err) {
this.debug(() => this.error(err));
}
};
},
});
}
/**
* Set key:value that will be sent as extra data with the event.
*
* @param key String key of extra
* @param extra Extra value of extra
*/
setExtra(key, extra) {
this.getScope().setExtra(key, extra);
}
/**
* Set an object that will be merged sent as extra data with the event.
*
* @param extras Extras context object to merge into current context.
*/
setExtras(extras) {
this.getScope().setExtras(extras);
}
/**
* Set key:value that will be sent as tags data with the event.
*
* @param key String key of tag
* @param value Primitive value of tag
*/
setTag(key, value) {
this.getScope().setTag(key, value);
}
/**
* Set an object that will be merged sent as tags data with the event.
*
* @param tags Tags context object to merge into current context.
*/
setTags(tags) {
this.getScope().setTags(tags);
}
/**
* Overrides the Sentry default grouping. See https://docs.sentry.io/data-management/event-grouping/sdk-fingerprinting/
*
* @param fingerprint Array of strings used to override the Sentry default grouping.
*/
setFingerprint(fingerprint) {
this.getScope().setFingerprint(fingerprint);
}
/**
* Records a new breadcrumb which will be attached to future events.
*
* Breadcrumbs will be added to subsequent events to provide more context on user's actions prior to an error or crash.
* @param breadcrumb The breadcrum to record.
*/
addBreadcrumb(breadcrumb) {
var _a;
const maxBreadcrumbs = (_a = this.options.maxBreadcrumbs) !== null && _a !== void 0 ? _a : this.DEFAULT_BREADCRUMBS;
const numberOfBreadcrumbs = Math.min(maxBreadcrumbs, this.MAX_BREADCRUMBS);
if (numberOfBreadcrumbs <= 0)
return;
if (!breadcrumb.timestamp) {
breadcrumb.timestamp = this.timestamp();
}
this.getScope().addBreadcrumb(breadcrumb, numberOfBreadcrumbs);
}
/**
* Captures an exception event and sends it to Sentry.
*
* @param exception An exception-like object.
* @returns The generated eventId, or undefined if event wasn't scheduled.
*/
captureException(exception) {
this.debug(() => this.log(`calling captureException`));
const event = this.buildEvent({});
if (!event)
return;
// This is to maintain backwards compatibility for 'event' option. When we remove it, all this complex logic can go away.
if ('context' in this.options) {
this.options.context.waitUntil(this.reportException(event, exception));
}
else if ('event' in this.options) {
this.options.event.waitUntil(this.reportException(event, exception));
}
else {
// 'waitUntil' not provided -- this is probably called from within a Durable Object
this.reportException(event, exception);
}
return event.event_id;
}
/**
* Captures a message event and sends it to Sentry.
*
* @param message The message to send to Sentry.
* @param level Define the level of the message.
* @returns The generated eventId, or undefined if event wasn't scheduled.
*/
captureMessage(message, level = 'info') {
this.debug(() => this.log(`calling captureMessage`));
const event = this.buildEvent({ level, message });
if (!event)
return;
if ('context' in this.options) {
this.options.context.waitUntil(this.reportMessage(event));
}
else if ('event' in this.options) {
this.options.event.waitUntil(this.reportMessage(event));
}
else {
// 'waitUntil' not provided -- this is probably called from within a Durable Object
this.reportMessage(event);
}
return event.event_id;
}
/**
* Updates user context information for future events.
*
* @param user — User context object to be set in the current context. Pass null to unset the user.
*/
setUser(user) {
this.getScope().setUser(user);
}
/**
* In Cloudflare Workers it’s not possible to read event.request's body after having generated a response (if you attempt to, it throws an exception).
* Chances are that if you are interested in reporting request body to Sentry, you have already read the data (via request.json()/request.text()).
* Use this method to set it in Sentry context.
* @param body
*/
setRequestBody(body) {
if (this.request) {
this.request.data = body;
}
else {
this.request = { data: body };
}
}
/**
* Creates a new scope with and executes the given operation within. The scope is automatically removed once the operation finishes or throws.
* This is essentially a convenience function for:
*
* @example
* pushScope();
* callback();
* popScope();
*/
withScope(callback) {
const scope = this.pushScope();
try {
callback(scope);
}
finally {
this.popScope();
}
}
/**
* Send data to Sentry.
*
* @param data Event data
*/
async postEvent(data) {
var _a, _b;
// We are sending User-Agent for backwards compatibility with older Sentry
let headers = {
'Content-Type': 'application/json',
'User-Agent': '@web3-storage/toucan-js/2.7.1',
};
// Build headers
if ((_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.transportOptions) === null || _b === void 0 ? void 0 : _b.headers) {
headers = {
...headers,
...this.options.transportOptions.headers,
};
}
// Build body string
const body = JSON.stringify(data);
// Log the outgoing request
this.debug(() => {
this.log(`sending request to Sentry with headers: ${JSON.stringify(headers)} and body: ${body}`);
});
// Send to Sentry and wait for Response
const response = await fetch(this.url, {
method: 'POST',
body,
headers,
});
// Log the response
await this.debug(() => {
return this.logResponse(response);
});
// Resolve with response
return response;
}
/**
* Builds event payload. Applies beforeSend.
*
* @param additionalData Additional data added to defaults.
* @returns Event
*/
buildEvent(additionalData) {
var _a;
if (hasTracingEnabled(this.options)) {
// 1.0 === 100% events are sent
// 0.0 === 0% events are sent
const sampleRate = typeof this.options.tracesSampler === 'function'
? this.options.tracesSampler({ request: this.request })
: typeof this.options.tracesSampleRate === 'number'
? this.options.tracesSampleRate
: typeof this.options.sampleRate === 'number'
? this.options.sampleRate
: null;
// Invalid sampling values result in skipped events, for parity with other Sentry SDKs.
// However, don't skip if
if (!isValidSampleRate(sampleRate)) {
this.debug(() => this.log(`skipping this event because of invalid sample rate.`));
return;
}
// Now we roll the dice. Math.random() is inclusive of 0, but not 1, so we need >= here.
// This is because When sampleRate is 0 and Math.random() returns 0, we must skip the event.
// When sampleRate is 1, the event will always be sent, because Math.random() cannot return 1.
if (Math.random() >= Number(sampleRate)) {
this.debug(() => this.log(`skipping this event (sampling rate ${Number(sampleRate)})`));
return;
}
}
const pkg = this.options.pkg;
// 'release' option takes precedence, if not present - try to derive from package.json
const release = this.options.release
? this.options.release
: pkg
? `${pkg.name}-${pkg.version}`
: undefined;
const scope = this.getScope();
// per https://docs.sentry.io/development/sdk-dev/event-payloads/#required-attributes
const payload = {
event_id: crypto.randomUUID().replace(/-/g, ''),
logger: 'EdgeWorker',
platform: 'node',
release,
environment: this.options.environment,
timestamp: this.timestamp(),
level: 'error',
modules: pkg
? {
...pkg.dependencies,
...pkg.devDependencies,
}
: undefined,
...additionalData,
request: this.request,
sdk: {
name: '@web3-storage/toucan-js',
version: '2.7.1',
},
};
// Type-casting 'breadcrumb' to any because our level type is a union of literals, as opposed to Level enum.
scope.applyToEvent(payload);
const beforeSend = (_a = this.options.beforeSend) !== null && _a !== void 0 ? _a : this.beforeSend;
return beforeSend(payload);
}
/**
* Converts data from fetch event's Request to Sentry Request used in Sentry Event
*
* @param request FetchEvent Request
* @returns Sentry Request
*/
toSentryRequest(request) {
// Build cookies
const cookieString = request.headers.get('cookie');
let cookies = undefined;
if (cookieString) {
try {
cookies = cookie.parse(cookieString);
}
catch (e) { }
}
const headers = {};
// Build headers (omit cookie header, because we used it in the previous step)
for (const [k, v] of request.headers.entries()) {
if (k !== 'cookie') {
headers[k] = v;
}
}
const rv = {
method: request.method,
cookies,
headers,
};
try {
const url = new URL(request.url);
rv.url = `${url.protocol}//${url.hostname}${url.pathname}`;
rv.query_string = url.search;
}
catch (e) {
// `new URL` failed, let's try to split URL the primitive way
const qi = request.url.indexOf('?');
if (qi < 0) {
// no query string
rv.url = request.url;
}
else {
rv.url = request.url.substr(0, qi);
rv.query_string = request.url.substr(qi + 1);
}
}
return rv;
}
/**
* This SDK's implementation of beforeSend. If 'beforeSend' is not provided in options, this implementation will be applied.
* This function is applied to all events before sending to Sentry.
*
* By default it:
* 1. Removes all request headers (unless opts.allowedHeaders is provided - in that case the allowlist is applied)
* 2. Removes all request cookies (unless opts.allowedCookies is provided- in that case the allowlist is applied)
* 3. Removes all search params (unless opts.allowedSearchParams is provided- in that case the allowlist is applied)
*
* @param event
* @returns Event
*/
beforeSend(event) {
const request = event.request;
if (request) {
// Let's try to remove sensitive data from incoming Request
const allowedHeaders = this.options.allowedHeaders;
const allowedCookies = this.options.allowedCookies;
const allowedSearchParams = this.options.allowedSearchParams;
if (allowedHeaders) {
request.headers = this.applyAllowlist(request.headers, allowedHeaders);
}
else {
delete request.headers;
}
if (allowedCookies) {
request.cookies = this.applyAllowlist(request.cookies, allowedCookies);
}
else {
delete request.cookies;
}
if (allowedSearchParams) {
const params = Object.fromEntries(new URLSearchParams(request.query_string));
const allowedParams = new URLSearchParams();
Object.keys(this.applyAllowlist(params, allowedSearchParams)).forEach((allowedKey) => {
allowedParams.set(allowedKey, params[allowedKey]);
});
request.query_string = allowedParams.toString();
}
else {
delete request.query_string;
}
}
event.request = request;
return event;
}
/**
* Helper function that applies 'allowlist' on 'obj' keys.
*
* @param obj
* @param allowlist
* @returns New object with allowed keys.
*/
applyAllowlist(obj = {}, allowlist) {
let predicate = (item) => false;
if (allowlist instanceof RegExp) {
predicate = (item) => allowlist.test(item);
}
else if (Array.isArray(allowlist)) {
const allowlistLowercased = allowlist.map((item) => item.toLowerCase());
predicate = (item) => allowlistLowercased.includes(item);
}
else {
this.debug(() => this.warn('allowlist must be an array of strings, or a regular expression.'));
return {};
}
return Object.keys(obj)
.map((key) => key.toLowerCase())
.filter((key) => predicate(key))
.reduce((allowed, key) => {
allowed[key] = obj[key];
return allowed;
}, {});
}
/**
* A number representing the seconds elapsed since the UNIX epoch.
*/
timestamp() {
return Date.now() / 1000;
}
/**
* Builds Message as per https://develop.sentry.dev/sdk/event-payloads/message/, adds it to the event,
* and sends it to Sentry. Inspired by https://github.com/getsentry/sentry-javascript/blob/master/packages/node/src/backend.ts.
*
* @param event
*/
async reportMessage(event) {
return this.postEvent(event);
}
/**
* Builds Exception as per https://docs.sentry.io/development/sdk-dev/event-payloads/exception/, adds it to the event,
* and sends it to Sentry. Inspired by https://github.com/getsentry/sentry-javascript/blob/master/packages/node/src/backend.ts.
*
* @param event
* @param error
*/
async reportException(event, maybeError) {
var _a, _b, _c;
let error;
if (utils.isError(maybeError)) {
error = maybeError;
}
else if (utils.isPlainObject(maybeError)) {
// This will allow us to group events based on top-level keys
// which is much better than creating new group when any key/value change
const message = `Non-Error exception captured with keys: ${utils.extractExceptionKeysForMessage(maybeError)}`;
this.setExtra('__serialized__', utils.normalizeToSize(maybeError));
error = new Error(message);
}
else {
// This handles when someone does: `throw "something awesome";`
// We use synthesized Error here so we can extract a (rough) stack trace.
error = new Error(maybeError);
}
const stacktrace = await this.buildStackTrace(error);
const values = (_b = (_a = event.exception) === null || _a === void 0 ? void 0 : _a.values) !== null && _b !== void 0 ? _b : [];
if (!((_c = event.exception) === null || _c === void 0 ? void 0 : _c.values)) {
event.exception = { values };
}
// push cause on to the top
values.unshift({ type: error.name, value: error.message, stacktrace });
if (error.cause) {
return this.reportException(event, error.cause);
}
else {
return this.postEvent(event);
}
}
/**
* Builds Stacktrace as per https://docs.sentry.io/development/sdk-dev/event-payloads/stacktrace/
*
* @param error Error object.
* @returns Stacktrace
*/
async buildStackTrace(error) {
var _a;
if (this.options.attachStacktrace === false) {
return undefined;
}
try {
const stack = await stacktraceJs.fromError(error);
/**
* sentry-cli and webpack-sentry-plugin upload the source-maps named by their path with a ~/ prefix.
* Lets adhere to this behavior.
*/
const rewriteFrames = (_a = this.options.rewriteFrames) !== null && _a !== void 0 ? _a : {
root: '~/',
iteratee: (frame) => frame,
};
return {
frames: stack
.map((frame) => {
var _a;
const filename = (_a = frame.fileName) !== null && _a !== void 0 ? _a : '';
const stackFrame = {
colno: frame.columnNumber,
lineno: frame.lineNumber,
filename,
function: frame.functionName,
};
if (!!rewriteFrames.root) {
stackFrame.filename = `${rewriteFrames.root}${stackFrame.filename}`;
}
return !!rewriteFrames.iteratee
? rewriteFrames.iteratee(stackFrame)
: stackFrame;
})
.reverse(),
};
}
catch (e) {
return undefined;
}
}
/**
* Runs a callback if debug === true.
* Use this to delay execution of debug logic, to ensure toucan doesn't burn I/O in non-debug mode.
*
* @param callback
*/
debug(callback) {
if (this.options.debug) {
return callback();
}
}
log(message) {
console.log(`toucan-js: ${message}`);
}
warn(message) {
console.warn(`toucan-js: ${message}`);
}
error(message) {
console.error(`toucan-js: ${message}`);
}
/**
* Reads and logs Response object from Sentry. Warning: Reads the Response stream (.text()).
* Do not use without this.debug wrapper.
*
* @param response Response
*/
async logResponse(response) {
var _a;
let responseText = '';
// Read response body, set to empty if fails
try {
responseText = await response.text();
}
catch (e) {
responseText += '';
}
// Parse origin from response.url, but at least give some string if parsing fails.
let origin = 'Sentry';
try {
const originUrl = new URL(response.url);
origin = originUrl.origin;
}
catch (e) {
origin = (_a = response.url) !== null && _a !== void 0 ? _a : 'Sentry';
}
const msg = `${origin} responded with [${response.status} ${response.statusText}]: ${responseText}`;
if (response.ok) {
this.log(msg);
}
else {
this.error(msg);
}
}
/** Returns the scope of the top stack. */
getScope() {
return this.scopes[this.scopes.length - 1];
}
/**
* Create a new scope to store context information.
* The scope will be layered on top of the current one. It is isolated, i.e. all breadcrumbs and context information added to this scope will be removed once the scope ends. * Be sure to always remove this scope with {@link this.popScope} when the operation finishes or throws.
*/
pushScope() {
// We want to clone the content of prev scope
const scope = Scope.clone(this.getScope());
this.scopes.push(scope);
return scope;
}
/**
* Removes a previously pushed scope from the stack.
* This restores the state before the scope was pushed. All breadcrumbs and context information added since the last call to {@link this.pushScope} are discarded.
*/
popScope() {
if (this.scopes.length <= 1)
return false;
return !!this.scopes.pop();
}
}
module.exports = Toucan;