Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit c1ac185

Browse files
authored
Merge pull request #43 from taskcluster/config-without-auth
Configuration without using tc credentials, so we can bootstrap auth
2 parents e17a682 + 03d71a6 commit c1ac185

File tree

2 files changed

+67
-28
lines changed

2 files changed

+67
-28
lines changed

src/index.js

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,86 @@ let Statsum = require('statsum');
88
let MockMonitor = require('./mockmonitor');
99
let Monitor = require('./monitor');
1010

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+
*/
1129
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, {
1531
patchGlobal: true,
1632
reportStatsumErrors: true,
1733
resourceInterval: 10 * 1000,
1834
crashTimeout: 5 * 1000,
35+
mock: false,
1936
});
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!');
2041

21-
if (opts.mock) {
22-
return new MockMonitor(opts);
42+
// Return mock monitor, if mocking
43+
if (options.mock) {
44+
return new MockMonitor(options);
2345
}
2446

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);
3468
}
35-
);
69+
}
3670

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+
});
3876

39-
let m = new Monitor(authClient, sentry, statsumClient, opts);
77+
let m = new Monitor(sentryDSN, null, statsum, options);
4078

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'));
4381
}
4482

45-
if (opts.patchGlobal) {
83+
if (options.patchGlobal) {
4684
process.on('uncaughtException', async (err) => {
4785
console.log('Uncaught Exception! Attempting to report to Sentry and crash.');
4886
console.log(err.stack);
4987
setTimeout(() => {
5088
console.log('Failed to report error to Sentry after timeout!');
5189
process.exit(1);
52-
}, opts.crashTimeout);
90+
}, options.crashTimeout);
5391
try {
5492
await m.reportError(err, 'fatal', {});
5593
console.log('Succesfully reported error to Sentry.');
@@ -67,8 +105,8 @@ async function monitor(options) {
67105
});
68106
}
69107

70-
if (opts.process) {
71-
utils.resources(m, opts.process, opts.resourceInterval);
108+
if (options.process) {
109+
utils.resources(m, options.process, options.resourceInterval);
72110
}
73111

74112
return m;

src/monitor.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ let utils = require('./utils');
66

77
class Monitor {
88

9-
constructor(authClient, sentry, statsumClient, opts) {
9+
constructor(sentryDSN, sentry, statsumClient, opts) {
1010
this._opts = opts;
11-
this._auth = authClient;
12-
this._sentry = sentry; // This must be a Promise that resolves to {client, expires}
11+
this._sentryDSN = sentryDSN;
12+
// This must be a Promise that resolves to {client, expires}
13+
this._sentry = sentry || Promise.resolve({client: null, expires: new Date(0)});
1314
this._statsum = statsumClient;
1415
this._resourceInterval = null;
1516
}
@@ -21,7 +22,7 @@ class Monitor {
2122
}
2223
this._sentry = this._sentry.then(async (sentry) => {
2324
if (!sentry.expires || Date.parse(sentry.expires) <= Date.now()) {
24-
let sentryInfo = await this._auth.sentryDSN(this._opts.project);
25+
let sentryInfo = await this._sentryDSN(this._opts.project);
2526
return {
2627
client: new raven.Client(sentryInfo.dsn.secret),
2728
expires: sentryInfo.expires,
@@ -86,7 +87,7 @@ class Monitor {
8687
let newopts = _.cloneDeep(this._opts);
8788
newopts.prefix = (this._opts.prefix || '') + '.' + prefix;
8889
return new Monitor(
89-
this._auth,
90+
this._sentryDSN,
9091
this._sentry,
9192
this._statsum.prefix(prefix),
9293
newopts

0 commit comments

Comments
 (0)