Skip to content
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
36 changes: 22 additions & 14 deletions src/main/modules/appinsights/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
const appInsights = require('applicationinsights');
import * as appInsights from 'applicationinsights';
import type { TelemetryClient } from 'applicationinsights';

import config from 'config';

declare global {
var __appInsightsEnabled: boolean | undefined;
}

export class AppInsights {

private client: TelemetryClient;

enable(): void {
if (global.__appInsightsEnabled) {
return;
}
global.__appInsightsEnabled = true;

if (config.get('appInsights.connectionString')) {
appInsights.setup(config.get<string>('appInsights.connectionString'))
.setSendLiveMetrics(true)
.setAutoCollectConsole(true, true)
.setAutoCollectExceptions(true)
.setSendLiveMetrics(false)
.setAutoCollectConsole(false)
.setAutoCollectPerformance(false, false)
.setAutoCollectDependencies(false); // Disable dependency tracking

appInsights.defaultClient.config.samplingPercentage = config.get('appInsights.samplingPercentage');
appInsights.defaultClient.trackTrace({message: 'App insights activated'});
this.client = appInsights.defaultClient;
this.client.config.samplingPercentage = config.get('appInsights.samplingPercentage');
this.client.trackTrace({message: 'App insights activated'});
appInsights.start();
}
}

public trackTrace(trace: string | {'message': string}) {
if (!this.client) {
console.warn('trackTrace called before AppInsights client initialised, dropped trace: ', trace);
return;
}
if (typeof trace === 'string') {
this.client.trackTrace({message: trace});
} else {
this.client.trackTrace(trace);
}
}
}
36 changes: 31 additions & 5 deletions src/main/modules/logging/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
const winston = require('winston');

Check warning on line 1 in src/main/modules/logging/index.ts

View workflow job for this annotation

GitHub Actions / build

A `require()` style import is forbidden
import TransportStream from 'winston-transport';
import * as appInsights from 'applicationinsights';

import type { LogEntry } from 'winston';
import type { TransportStreamOptions } from 'winston-transport';

class AppInsightsTransport extends TransportStream {
constructor(opts?: TransportStreamOptions) {
super(opts);
}

log(info: LogEntry, callback: () => void) {
setImmediate(() => this.emit('logged', info));

const client = appInsights.defaultClient;
if (client) {
client.trackTrace({
message: info.message,
properties: {
level: info.level,
...info.meta,
},
});
}

callback();
}
}

const logger = winston.createLogger({
level: 'info',
level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json(),
),
transports: [
new winston.transports.Console({
stderrLevels: ['info', 'warn', 'error', 'critical'],
consoleWarnLevels: ['info', 'warn', 'error', 'critical'],
}),
new winston.transports.Console({level: 'info'}),
new AppInsightsTransport({level: 'info'}),
],
});

Expand Down
Loading