@@ -8,48 +8,86 @@ let Statsum = require('statsum');
8
8
let MockMonitor = require ( './mockmonitor' ) ;
9
9
let Monitor = require ( './monitor' ) ;
10
10
11
+ /**
12
+ * Create a new monitor, given options:
13
+ * {
14
+ * project: '...',
15
+ * patchGlobal: true,
16
+ * reportStatsumErrors: true,
17
+ * resourceInterval: 10 * 1000,
18
+ * crashTimeout: 5 * 1000,
19
+ * mock: false,
20
+ * credentials: {
21
+ * clientId: '...',
22
+ * accessToken: '...',
23
+ * },
24
+ * // If credentials aren't given, you must supply:
25
+ * statsumToken: async (project) => {token, expires, baseUrl}
26
+ * sentryDNS: async (project) => {dsn: {secret: '...'}, expires}
27
+ * }
28
+ */
11
29
async function monitor ( options ) {
12
- assert ( options . credentials , 'Must provide taskcluster credentials!' ) ;
13
- assert ( options . project , 'Must provide a project name!' ) ;
14
- let opts = _ . defaults ( options , {
30
+ options = _ . defaults ( { } , options , {
15
31
patchGlobal : true ,
16
32
reportStatsumErrors : true ,
17
33
resourceInterval : 10 * 1000 ,
18
34
crashTimeout : 5 * 1000 ,
35
+ mock : false ,
19
36
} ) ;
37
+ assert ( options . credentials || options . statsumToken && options . sentryDSN ||
38
+ options . mock ,
39
+ 'Must provide taskcluster credentials or sentryDSN and statsumToken' ) ;
40
+ assert ( options . project , 'Must provide a project name!' ) ;
20
41
21
- if ( opts . mock ) {
22
- return new MockMonitor ( opts ) ;
42
+ // Return mock monitor, if mocking
43
+ if ( options . mock ) {
44
+ return new MockMonitor ( options ) ;
23
45
}
24
46
25
- let authClient = new taskcluster . Auth ( {
26
- credentials : opts . credentials ,
27
- } ) ;
28
-
29
- let statsumClient = new Statsum (
30
- project => authClient . statsumToken ( project ) ,
31
- {
32
- project : opts . project ,
33
- emitErrors : opts . reportStatsumErrors ,
47
+ // Find functions for statsum and sentry
48
+ let statsumToken = options . statsumToken ;
49
+ let sentryDSN = options . sentryDSN ;
50
+ // Wrap statsumToken in function if it's not a function
51
+ if ( statsumToken && ! ( statsumToken instanceof Function ) ) {
52
+ statsumToken = ( ) => options . statsumToken ;
53
+ }
54
+ // Wrap sentryDSN in function if it's not a function
55
+ if ( sentryDSN && ! ( sentryDSN instanceof Function ) ) {
56
+ sentryDSN = ( ) => options . sentryDSN ;
57
+ }
58
+ // Use taskcluster credentials for statsumToken and sentryDSN, if given
59
+ if ( options . credentials ) {
60
+ let auth = new taskcluster . Auth ( {
61
+ credentials : options . credentials ,
62
+ } ) ;
63
+ if ( ! statsumToken ) {
64
+ statsumToken = project => auth . statsumToken ( project ) ;
65
+ }
66
+ if ( ! sentryDSN ) {
67
+ sentryDSN = project => auth . sentryDSN ( project ) ;
34
68
}
35
- ) ;
69
+ }
36
70
37
- let sentry = Promise . resolve ( { client : null , expires : new Date ( 0 ) } ) ;
71
+ // Create statsum client
72
+ let statsum = new Statsum ( statsumToken , {
73
+ project : options . project ,
74
+ emitErrors : options . reportStatsumErrors ,
75
+ } ) ;
38
76
39
- let m = new Monitor ( authClient , sentry , statsumClient , opts ) ;
77
+ let m = new Monitor ( sentryDSN , null , statsum , options ) ;
40
78
41
- if ( opts . reportStatsumErrors ) {
42
- statsumClient . on ( 'error' , err => m . reportError ( err , 'warning' ) ) ;
79
+ if ( options . reportStatsumErrors ) {
80
+ statsum . on ( 'error' , err => m . reportError ( err , 'warning' ) ) ;
43
81
}
44
82
45
- if ( opts . patchGlobal ) {
83
+ if ( options . patchGlobal ) {
46
84
process . on ( 'uncaughtException' , async ( err ) => {
47
85
console . log ( 'Uncaught Exception! Attempting to report to Sentry and crash.' ) ;
48
86
console . log ( err . stack ) ;
49
87
setTimeout ( ( ) => {
50
88
console . log ( 'Failed to report error to Sentry after timeout!' ) ;
51
89
process . exit ( 1 ) ;
52
- } , opts . crashTimeout ) ;
90
+ } , options . crashTimeout ) ;
53
91
try {
54
92
await m . reportError ( err , 'fatal' , { } ) ;
55
93
console . log ( 'Succesfully reported error to Sentry.' ) ;
@@ -67,8 +105,8 @@ async function monitor(options) {
67
105
} ) ;
68
106
}
69
107
70
- if ( opts . process ) {
71
- utils . resources ( m , opts . process , opts . resourceInterval ) ;
108
+ if ( options . process ) {
109
+ utils . resources ( m , options . process , options . resourceInterval ) ;
72
110
}
73
111
74
112
return m ;
0 commit comments