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

Commit 9ba6af9

Browse files
committed
Merge pull request #1 from taskcluster/nicer-interface
Make a nicer interface for token loading
2 parents a05e98e + 635edeb commit 9ba6af9

File tree

5 files changed

+46
-57
lines changed

5 files changed

+46
-57
lines changed

README.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ A Javascript client library for [statsum](https://github.com/jonasfj/statsum).
88

99
let Statsum = require('statsum');
1010

11-
// Create a project token
12-
let token = Statsum.createToken('my-project', 'SECRET', '24h');
13-
1411
// Create a client object
15-
let statsum = new Statsum({
16-
project: 'my-project',
17-
token: token,
18-
baseUrl: 'https://statsum.example.com',
19-
});
12+
let configurer = async (project) => { return {
13+
project: 'example-project',
14+
baseUrl: 'https://example.com',
15+
token: 'KEY',
16+
expires: new Date().toJSON()
17+
}};
18+
let statsum = new Statsum(configurer, {project: 'test'});
2019

2120
// Send metrics
2221
statsum.count('my-counter', 1);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "statsum",
3-
"version": "0.3.2",
3+
"version": "0.4.0",
44
"author": "Jonas Finnemann Jensen <[email protected]>",
55
"description": "Client library for statsum",
66
"license": "MPL-2.0",

src/client.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,28 @@ class StatsumClient extends events.EventEmitter {
1111
/**
1212
* Create a statsum client.
1313
*
14-
* Óptions:
14+
* Configurer: A function that must return an object with these specific keys
1515
* ```js
1616
* {
1717
* project: '...', // Project to submit for
18-
* token: '...', // JWT token (if not getToken)
19-
* getToken: () => {token, expires}, // Async function for token
18+
* token: '...', // JWT token
2019
* baseUrl: 'https://example.com/', // baseUrl for the server
20+
* expires: 'date-time', // Time at which the token expires
21+
* }
22+
* ```
23+
*
24+
* Options:
25+
* ```js
26+
* {
27+
* project: '...', // Project to submit for
2128
* maxDataPoints: 10000, // Max data-points before flushing
2229
* maxDelay: 90, // Max delay before flush (s)
2330
* minDelay: 30, // Min delay before flush (s)
2431
* emitErrors: false, // Emit 'error' events on errors
2532
* }
2633
* ```
2734
*/
28-
constructor(options) {
35+
constructor(configurer, options) {
2936
super();
3037
this._options = options = _.defaults({}, options || {}, {
3138
maxDataPoints: 10000,
@@ -34,9 +41,6 @@ class StatsumClient extends events.EventEmitter {
3441
emitErrors: false,
3542
});
3643
assert(options.project, 'project is required');
37-
assert(options.token || options.getToken instanceof Function,
38-
'token or getToken is required');
39-
assert(options.baseUrl, 'baseUrl is required');
4044
assert(typeof(options.minDelay) === 'number', 'minDelay must be a number');
4145
assert(typeof(options.maxDelay) === 'number', 'maxDelay must be a number');
4246
assert(options.maxDelay >= 30, 'maxDelay must be > 30 seconds');
@@ -47,6 +51,7 @@ class StatsumClient extends events.EventEmitter {
4751
this._measures = {};
4852
this._flushTimer = null;
4953
this._dataPoints = 0;
54+
this._configurer = configurer;
5055
}
5156

5257
/** Increment counter for `key` with `count` */
@@ -110,7 +115,7 @@ class StatsumClient extends events.EventEmitter {
110115
dataPoints, payload.counters.length + payload.measures.length,
111116
this._options.project,
112117
);
113-
await sendDataPoints(this._options, payload);
118+
await sendDataPoints(this._configurer, this._options, payload);
114119
} catch (err) {
115120
debug('Failed to send data-points with error: %s, stack: %j', err, err);
116121
if (this._options.emitErrors) {

src/send.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
let assert = require('assert');
12
let urljoin = require('url-join');
23
let got = require('got');
34
let debug = require('debug')('statsum');
@@ -23,19 +24,16 @@ if (msgpack) {
2324
}
2425

2526
/** Send data-point to statsum */
26-
let sendDataPoints = async (options, payload) => {
27-
// Get authentication token
28-
let token = options.token;
29-
if (!token || options.tokenExpires < new Date()) {
30-
// If we have one, or it's expired, we get a new one
31-
let result = await options.getToken();
32-
if (!result.token || !result.expires) {
33-
let err = new Error('getToken() must return {token, expires}');
34-
err.result = result;
35-
throw err;
36-
}
27+
let sendDataPoints = async (configurer, options, payload) => {
28+
29+
if (!options.token || options.tokenExpires < new Date()) {
30+
let result = await configurer(options.project);
31+
assert(result.token, 'token is required from the configurer');
32+
assert(result.baseUrl, 'baseUrl is required from the configurer');
33+
assert(result.expires, 'expires is required from the configurer');
3734
options.token = result.token;
3835
options.tokenExpires = new Date(result.expires);
36+
options.baseUrl = result.baseUrl;
3937
}
4038

4139
let url = urljoin(options.baseUrl, 'v1/project', options.project);

test/statsum_test.js

+16-29
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ suite('statsum', () => {
1717
deserialize = (data) => msgpack.unpack(data);
1818
}
1919

20+
2021
let server = null;
2122
let baseUrl = null;
2223
let payload = null;
24+
let configurer = null;
2325
before(async () => {
2426
server = http.createServer();
2527
server.on('request', async (req, res) => {
@@ -37,6 +39,13 @@ suite('statsum', () => {
3739
server.listen();
3840
});
3941
baseUrl = 'http://localhost:' + server.address().port;
42+
43+
configurer = async (project) => { return {
44+
project,
45+
baseUrl,
46+
token: 'KEY',
47+
expires: new Date().toJSON()
48+
}};
4049
});
4150
after(async () => {
4251
server.close();
@@ -50,30 +59,8 @@ suite('statsum', () => {
5059
assert(result.exp > Date.now() / 1000 + 60);
5160
});
5261

53-
test('getToken', async () => {
54-
let getTokenCalls = 0;
55-
let delay = 0;
56-
let statsum = new Statsum({project: 'test', getToken() {
57-
getTokenCalls +=1;
58-
return new Promise({token: 'KEY', expires: new Date(Date.now() + delay)});
59-
}, baseUrl});
60-
61-
statsum.count('my-counter', 10);
62-
assert(getTokenCalls === 0);
63-
await statsum.flush();
64-
await new Promise(accept => setTimeout(accept, 100));
65-
assert(getTokenCalls === 1);
66-
67-
delay = 500;
68-
statsum.count('my-counter', 10);
69-
await statsum.flush();
70-
assert(getTokenCalls === 2);
71-
await new Promise(accept => setTimeout(accept, 100));
72-
assert(getTokenCalls === 2);
73-
});
74-
7562
test('count()', async () => {
76-
let statsum = new Statsum({project: 'test', token: 'KEY', baseUrl});
63+
let statsum = new Statsum(configurer, {project: 'test'});
7764

7865
statsum.count('my-counter', 10);
7966
await statsum.flush();
@@ -94,7 +81,7 @@ suite('statsum', () => {
9481
});
9582

9683
test('value()', async () => {
97-
let statsum = new Statsum({project: 'test', token: 'KEY', baseUrl});
84+
let statsum = new Statsum(configurer, {project: 'test'});
9885

9986
statsum.measure('my-timer', 10);
10087
await statsum.flush();
@@ -118,7 +105,7 @@ suite('statsum', () => {
118105
});
119106

120107
test('value() w. tags', async () => {
121-
let statsum = new Statsum({project: 'test', token: 'KEY', baseUrl});
108+
let statsum = new Statsum(configurer, {project: 'test'});
122109

123110
statsum.measure({tag1: 'v1', tag2: 'v2'}, 5);
124111
statsum.measure({tag1: 'v1', tag2: 'v1'}, 5);
@@ -142,7 +129,7 @@ suite('statsum', () => {
142129
});
143130

144131
test('prefix().count()', async () => {
145-
let statsum = new Statsum({project: 'test', token: 'KEY', baseUrl});
132+
let statsum = new Statsum(configurer, {project: 'test'});
146133
statsum = statsum.prefix('my')
147134

148135
statsum.count('counter', 10);
@@ -164,7 +151,7 @@ suite('statsum', () => {
164151
});
165152

166153
test('prefix().measure()', async () => {
167-
let statsum = new Statsum({project: 'test', token: 'KEY', baseUrl});
154+
let statsum = new Statsum(configurer, {project: 'test'});
168155
statsum = statsum.prefix('my')
169156

170157
statsum.measure('timer', 10);
@@ -189,7 +176,7 @@ suite('statsum', () => {
189176
});
190177

191178
test('prefix().prefix()', async () => {
192-
let statsum = new Statsum({project: 'test', token: 'KEY', baseUrl});
179+
let statsum = new Statsum(configurer, {project: 'test'});
193180
statsum = statsum.prefix('my').prefix('count');
194181

195182
statsum.count('2', 10);
@@ -201,4 +188,4 @@ suite('statsum', () => {
201188
assert(payload.counters[0].k === 'my.count.2', 'missing my.count.2');
202189
assert(payload.counters[0].v === 12, 'wrong count');
203190
});
204-
});
191+
});

0 commit comments

Comments
 (0)