@amplitude/ampli
Version:
Amplitude CLI
91 lines (90 loc) • 3.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Sentry = require("@sentry/node");
const ansiRegex = require("ansi-regex");
const lodash_1 = require("lodash");
const errors_1 = require("../errors");
const constants_1 = require("../constants");
const OCLIFF_ERROR_NAMES = {
UnexpectedArgsError: 'UnexpectedArgsError',
};
class AmpliSentry {
constructor() {
this.captureEnvironment = (config) => Sentry.configureScope(scope => {
scope.setTag('ampli', config.version);
scope.setTag('node', process.version);
scope.setTag('platform', config.platform);
scope.setTag('arch', config.arch);
scope.setTag('windows', config.windows ? 'yes' : 'no');
});
this.captureCommand = (id) => Sentry.configureScope(scope => {
scope.setTag('command', id || 'unknown');
});
this.captureUser = (user) => Sentry.configureScope(scope => {
scope.setUser({ id: user.id });
scope.setTag('orgs', user.orgs.map(org => org.id).join(','));
scope.setTag('zone', user.zone);
});
this.captureCommandFlags = (flags) => {
if (!lodash_1.isEmpty(flags)) {
Sentry.setContext('flags', flags);
}
};
this.captureSource = (source) => {
const { platformId, languageId } = source.runtime;
Sentry.configureScope(scope => {
scope.setTag('source.platform', platformId);
scope.setTag('source.language', languageId);
});
Sentry.setContext('source', {
destinations: source.destinations.map(d => d.serviceId),
language: languageId,
platform: platformId,
});
};
this.captureExec = (command, options) => Sentry.setContext('exec', {
command,
options,
});
this.captureException = (exception, captureContext) => Sentry.captureException(exception, captureContext);
this.getCurrentHub = () => Sentry.getCurrentHub();
this.addBreadcrumb = (breadcrumb) => Sentry.addBreadcrumb(breadcrumb);
this.updateContext = (name, additionalProps) => {
const context = Object.assign(Object.assign({}, this.contextMap[name]), additionalProps);
this.contextMap[name] = context;
Sentry.setContext(name, context);
};
this.contextMap = {};
Sentry.init(Object.assign(Object.assign({}, constants_1.APP_SETTINGS.sentry), { attachStacktrace: true }));
Sentry.configureScope(scope => {
scope.addEventProcessor((event, hint) => {
const ex = hint === null || hint === void 0 ? void 0 : hint.originalException;
const errorName = ex ? ex.name : 'unknown-error';
const commandError = ex;
if (commandError) {
try {
const message = (commandError.message)
? commandError.message
: commandError.toString();
event.message = message.replace(ansiRegex(), '');
}
catch (err) {
event.message = 'Unknown error. Unable to process error message.';
}
}
switch (errorName) {
case OCLIFF_ERROR_NAMES.UnexpectedArgsError:
case errors_1.UserFixableError.name:
event.level = Sentry.Severity.Info;
break;
default:
}
return event;
});
});
}
captureCommandContext(id, context) {
this.updateContext(id || 'unknown', context);
}
}
exports.default = AmpliSentry;