Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
static setup(key = null, echo = false) {
// exit if we we are already setup
if (appInsights.client instanceof MockInsights) {
return;
}
if (!key || key === 'mock') {
appInsights.client = new MockInsights();
} else {
appInsights
.setup(key)
.setAutoCollectPerformance(false)
.setAutoCollectDependencies(false)
.start();
if (echo) {
appInsights.client = new MockInsights(appInsights.client);
}
}
}
const handler = function (error, request, response, next) {
appInsights.client.trackException(error, { name: 'SvcRequestFailure' });
if (response.headersSent) {
return next(error);
}
response.status(error.status || 500);
let propertiesToSerialize = ['success', 'message'];
if (app.get('env') !== 'production') {
propertiesToSerialize.push('stack');
}
// Properties on Error object aren't enumerable so need to explicitly list properties to serialize
response.send(JSON.stringify(error, propertiesToSerialize));
response.end();
};
app.use(handler);
exports.init = function (isEnabled) {
_isEnabled = isEnabled;
_subscription = _getCurrentSubscription();
if (_isEnabled) {
_appInsights.setup(Constants.TELEMETRY_INSTRUMENTATION_KEY)
.setAutoCollectRequests(false)
.setAutoCollectPerformance(false)
.setAutoCollectExceptions(false);
// Overwrite appinsight default values.
var context = _appInsights.client.context;
context.tags[context.keys.userId] = _subscription.user.id;
_appInsights.start();
}
};
static setup(key = null, echo = false) {
// exit if we we are already setup
if (appInsights.client instanceof MockInsights) {
return;
}
if (!key || key === 'mock') {
appInsights.client = new MockInsights();
} else {
appInsights
.setup(key)
.setAutoCollectPerformance(false)
.setAutoCollectDependencies(false)
.start();
if (echo) {
appInsights.client = new MockInsights(appInsights.client);
}
}
}
} else if (options.insights) {
// If insights is set, just use the default client
// We expect it to be already configured and started
this.client = options.insights.client;
} else {
// Setup insights and start it
// If options.key is defined, use it. Else the SDK will expect
// an environment variable to be set.
appInsights
.setup(options.key)
.start();
this.client = appInsights.client;
}
if (!this.client) {
throw new Error('Could not get an Application Insights client instance');
}
this.name = WINSTON_LOGGER_NAME;
this.level = options.level || WINSTON_DEFAULT_LEVEL;
this.silent = options.silent || DEFAULT_IS_SILENT;
this.treatErrorsAsExceptions = !!options.treatErrorsAsExceptions;
// Setup AI here!
};
export function trackEvent(code: string, properties?: { [key: string]: string; }, measurements?: { [key: string]: number; }) {
let payload = {
code: code,
properties: properties,
measurements: measurements
};
console.log(JSON.stringify(payload, null, '\t'));
let configuration = vscode.workspace.getConfiguration('ecdc');
let collectTelemetry = configuration.get('collectTelemetry');
if (collectTelemetry) {
ai.client.trackEvent(code, properties);
}
}
var _flush = function (callback) {
if (_isEnabled) {
_appInsights.client.sendPendingData(callback);
}
};
exports.onError = function (err, callback) {
if (_isEnabled && _event) {
_stop(_event);
_event.isSuccess = false;
_event.stacktrace = _stripUsername(err.stack);
_event.errorCategory = err.statusCode ? 'HTTP_Error_' + err.statusCode : 'CLI_Error';
_appInsights.client.trackEvent('CmdletError', _event);
_flush(callback);
} else {
callback();
}
};
var _trackPageView = function (data) {
var pageView = new _PageViewData();
pageView.name = data.commandName;
if (!isNaN(data.duration)) {
pageView.duration = _msToTimeSpan(data.duration);
}
pageView.properties = data;
var _data = new _Data();
_data.baseType = 'PageViewData';
_data.baseData = pageView;
_appInsights.client.track(_data);
};