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

Commit 65870d3

Browse files
committed
feat(metrics): send account verification time to statsd
1 parent 8bcc064 commit 65870d3

File tree

4 files changed

+95
-9
lines changed

4 files changed

+95
-9
lines changed

lib/log.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ Lug.prototype.summary = function (request, response) {
135135
}
136136
}
137137

138+
Lug.prototype.timing = function(name, timing, tags) {
139+
this.statsd.timing(name, timing, tags)
140+
}
141+
138142
module.exports = function (level, name) {
139143
var log = new Lug(
140144
{

lib/metrics/statsd.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
var StatsD = require('node-statsd')
66

77
var STATSD_PREFIX = 'fxa.auth.'
8+
var TIMING_SUFFIX = '.time'
89

910
function StatsDCollector(log) {
1011
if (! log) {
@@ -56,15 +57,26 @@ StatsDCollector.prototype = {
5657
},
5758

5859
increment: function (name, tags) {
59-
var self = this
60+
if (this.client) {
61+
this.client.increment(
62+
STATSD_PREFIX + name,
63+
1,
64+
this.sampleRate,
65+
tags,
66+
handleErrors(this, 'increment')
67+
)
68+
}
69+
},
6070

61-
if (self.client) {
62-
self.client.increment(STATSD_PREFIX + name, 1, self.sampleRate, tags, function messageSentCallback(err) {
63-
// this only gets called once after all messages have been sent
64-
if (err) {
65-
self.log.error({op: 'statsd', err: err})
66-
}
67-
})
71+
timing: function (name, timing, tags) {
72+
if (this.client) {
73+
this.client.timing(
74+
STATSD_PREFIX + name + TIMING_SUFFIX,
75+
timing,
76+
this.sampleRate,
77+
tags,
78+
handleErrors(this, 'timing')
79+
)
6880
}
6981
},
7082

@@ -79,4 +91,15 @@ StatsDCollector.prototype = {
7991
}
8092
}
8193

94+
function handleErrors (self, method) {
95+
return function (error) {
96+
if (error) {
97+
self.log.error({
98+
op: 'statsd.' + method,
99+
err: error
100+
})
101+
}
102+
}
103+
}
104+
82105
module.exports = StatsDCollector

lib/routes/account.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ module.exports = function (
678678
if (!butil.buffersAreEqual(code, account.emailCode)) {
679679
throw error.invalidVerificationCode()
680680
}
681+
log.timing('account.verified', Date.now() - account.createdAt)
681682
log.event('verified', { email: account.email, uid: account.uid, locale: account.locale })
682683
log.increment('account.verified')
683684
return db.verifyEmail(account)

test/local/statsd_tests.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ test(
9797
function (t) {
9898
var mockLog = {
9999
error: function (log) {
100-
t.equal(log.op, 'statsd')
100+
t.equal(log.op, 'statsd.increment')
101101
t.equal(log.err.message, 'Failed to send message')
102102
t.end()
103103
}
@@ -122,6 +122,64 @@ test(
122122
}
123123
)
124124

125+
test(
126+
'statsd.timing',
127+
function (t) {
128+
function StatsDMock() {
129+
return {
130+
socket: {},
131+
increment: function () {
132+
t.fail('statsd.increment should not be called')
133+
t.end()
134+
},
135+
timing: function () {
136+
t.equal(arguments.length, 5, 'statsd.timing received the correct number arguments')
137+
t.equal(arguments[0], 'fxa.auth.foo.time', 'statsd.timing received the correct name argument')
138+
t.equal(arguments[1], 1, 'statsd.timing received the correct timing argument')
139+
t.equal(typeof arguments[2], 'number', 'statsd.timing received the correct timing argument')
140+
t.equal(arguments[3], undefined, 'statsd.timing received the correct tags argument')
141+
t.equal(typeof arguments[4], 'function', 'statsd.timing received the correct callback argument')
142+
t.end()
143+
}
144+
}
145+
}
146+
147+
var statsd = new StatsDCollector(mockLog)
148+
statsd.init()
149+
statsd.client = new StatsDMock()
150+
var log = require('../../lib/log')('info')
151+
log.statsd = statsd
152+
log.timing('foo', 1)
153+
}
154+
)
155+
156+
test(
157+
'statsd.timing error',
158+
function (t) {
159+
var mockLog = {
160+
error: function (log) {
161+
t.equal(log.op, 'statsd.timing')
162+
t.equal(log.err, 'foo')
163+
t.end()
164+
}
165+
}
166+
167+
function StatsDMock() {
168+
return {
169+
socket: {},
170+
timing: function () {
171+
arguments[4]('foo')
172+
}
173+
}
174+
}
175+
176+
var statsd = new StatsDCollector(mockLog)
177+
statsd.init()
178+
statsd.client = new StatsDMock()
179+
statsd.timing('wibble', 42)
180+
}
181+
)
182+
125183
test(
126184
'statsd close',
127185
function (t) {

0 commit comments

Comments
 (0)