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

Commit fff4624

Browse files
committed
feat(metrics): Added additional user info on statsd messages
1 parent c31fc97 commit fff4624

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

lib/metrics/statsd.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,37 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
var StatsD = require('node-statsd')
6+
var uaParser = require('ua-parser')
67

78
var STATSD_PREFIX = 'fxa.auth.'
89
var TIMING_SUFFIX = '.time'
910

11+
function getGenericTags(info) {
12+
var tags = []
13+
if (info.userAgent) {
14+
var agent = uaParser.parse(info.userAgent)
15+
if (agent) {
16+
if (agent.ua) {
17+
tags = tags.concat([
18+
'agent_ua_family:' + agent.ua.family, // -> "Safari"
19+
'agent_ua_version:' + agent.ua.toVersionString(), // -> "5.0.1"
20+
'agent_ua_version_major:' + agent.ua.major // -> "5"
21+
])
22+
}
23+
24+
if (agent.os) {
25+
tags = tags.concat([
26+
'agent_os_version:' + agent.os.toVersionString(), // -> "5.1"
27+
'agent_os_family:' + agent.os.family, // -> "iOS"
28+
'agent_os_major:' + agent.os.major // -> "5"
29+
])
30+
}
31+
}
32+
}
33+
34+
return tags
35+
}
36+
1037
function StatsDCollector(log) {
1138
if (! log) {
1239
throw new Error('Log is required')
@@ -53,7 +80,8 @@ StatsDCollector.prototype = {
5380
* @param {Object} info
5481
*/
5582
write: function (info) {
56-
this.increment(info.event)
83+
var tags = getGenericTags(info)
84+
this.increment(info.event, tags)
5785
},
5886

5987
increment: function (name, tags) {

test/local/statsd_tests.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ test(
5151
t.equal(name, 'fxa.auth.some-event')
5252
t.equal(value, 1)
5353
t.ok(sampleRate)
54-
t.notOk(tags)
54+
t.equal(Array.isArray(tags), true)
55+
t.equal(tags.length, 0)
5556
t.end()
5657
}
5758
}
@@ -67,6 +68,40 @@ test(
6768
}
6869
)
6970

71+
test(
72+
'statsd write with tags',
73+
function (t) {
74+
function StatsDMock() {
75+
return {
76+
socket: {},
77+
increment: function (name, value, sampleRate, tags) {
78+
t.equal(name, 'fxa.auth.some-event')
79+
t.equal(value, 1)
80+
t.ok(sampleRate)
81+
t.deepEquals(tags, [
82+
'agent_ua_family:Firefox',
83+
'agent_ua_version:43.0',
84+
'agent_ua_version_major:43',
85+
'agent_os_version:10.11',
86+
'agent_os_family:Mac OS X',
87+
'agent_os_major:10'
88+
])
89+
t.end()
90+
}
91+
}
92+
}
93+
94+
var statsd = new StatsDCollector(mockLog)
95+
statsd.init()
96+
statsd.client = new StatsDMock()
97+
statsd.write({
98+
event: 'some-event',
99+
uid: 'id',
100+
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0'
101+
})
102+
}
103+
)
104+
70105
test(
71106
'statsd write via log.increment',
72107
function (t) {
@@ -77,7 +112,8 @@ test(
77112
t.equal(name, 'fxa.auth.some-event')
78113
t.equal(value, 1)
79114
t.ok(sampleRate)
80-
t.notOk(tags)
115+
t.equal(Array.isArray(tags), true)
116+
t.equal(tags.length, 0)
81117
t.end()
82118
}
83119
}
@@ -191,5 +227,3 @@ test(
191227
t.end()
192228
}
193229
)
194-
195-

0 commit comments

Comments
 (0)