Skip to content

Commit 88f9c5c

Browse files
committed
Remove non specified fields to reduce noise
We were getting too many random or obscure headers and unexpected params which was causing our elasticsearch indexes to run out of fields.
1 parent 9475a1c commit 88f9c5c

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

lib/utils.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,53 @@ const removeTokensFromUrl = function(url) {
3636
};
3737
module.exports.removeTokensFromUrl = removeTokensFromUrl;
3838

39+
/**
40+
* pick
41+
* Given an object and array of keys, return an object with only the specified fields
42+
*
43+
* @param {Object} obj
44+
* @param {Array} keys
45+
* @return {Object}
46+
*/
47+
const pick = function(obj, keys = []) {
48+
return keys.reduce((picked, key) => {
49+
if (key in obj) {
50+
picked[key] = obj[key];
51+
}
52+
return picked;
53+
}, {});
54+
};
55+
module.exports.pick = pick;
56+
3957

58+
const headersToLog = [
59+
'user-agent',
60+
'referer',
61+
'cache-control',
62+
];
4063
/**
4164
* getRequestDataToLog
4265
* Given a http request object, return the proper structure to log
4366
*
4467
* @param {http.ClientRequest} req
4568
* @return {Object}
4669
*/
47-
const getRequestDataToLog = function(req) {
70+
const getRequestDataToLog = function(req, params = []) {
4871
const cleanUrl = removeTokensFromUrl(req.url);
4972
const query = req.query || parse(req.url, true).query;
5073

5174
const request = {
5275
url: cleanUrl,
5376
method: req.method,
54-
params: removeTokensFromQuery(query),
77+
params: removeTokensFromQuery(pick(query, params)),
5578
connection: {
5679
remoteAddress: req.connection.remoteAddress,
5780
remotePort: req.connection.remotePort,
5881
},
5982
};
6083

61-
// Copy headers, removing cookies from the noise
62-
request.headers = Object.assign({}, req.headers);
63-
delete request.headers.cookie;
84+
// Copy only headers we wish to log
85+
request.headers = pick(Object.assign({}, req.headers), headersToLog);
6486

6587
return request;
6688
};

test/testUtils.js

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { assert } = require('chai');
44
const {
55
removeTokensFromQuery,
66
removeTokensFromUrl,
7+
pick,
78
getRequestDataToLog,
89
} = require('../lib/utils');
910

@@ -52,8 +53,44 @@ describe('utils', () => {
5253

5354
});
5455

56+
describe('pick', () => {
57+
58+
it('should only return object with specified keys', () => {
59+
const obj = {
60+
good: 'should remain',
61+
bad: 'should be gone',
62+
};
63+
const picked = pick(obj, ['good']);
64+
assert.deepEqual(picked, { good: 'should remain' });
65+
});
66+
67+
});
68+
5569
describe('getRequestDataToLog', () => {
5670

71+
it('should filter out headers', () => {
72+
const req = {
73+
url: '/path?key=val',
74+
method: 'GET',
75+
connection: {
76+
remoteAddress: '::ffff:172.18.0.12',
77+
remotePort: 40234,
78+
},
79+
headers: {
80+
'user-agent': 'Mozilla/5.0',
81+
referer: 'https://twitter.com',
82+
'cache-control': 'no-cache',
83+
'x-something-bogus': false,
84+
}
85+
};
86+
const data = getRequestDataToLog(req);
87+
assert.deepEqual(data.headers, {
88+
'user-agent': 'Mozilla/5.0',
89+
referer: 'https://twitter.com',
90+
'cache-control': 'no-cache',
91+
});
92+
});
93+
5794
it('should grab the data we want to track', () => {
5895
const req = {
5996
url: '/path?key=val',
@@ -75,19 +112,35 @@ describe('utils', () => {
75112
assert.deepEqual(data, {
76113
url: '/path?key=val',
77114
method: 'GET',
78-
params: {
79-
key: 'val',
80-
},
81115
connection: {
82116
remoteAddress: '::ffff:172.18.0.12',
83117
remotePort: 40234,
84118
},
85119
headers: {
86120
'user-agent': 'Mozilla/5.0',
87121
},
122+
params: {},
88123
});
89124
});
90125

126+
it('should filter out params that are not specified', () => {
127+
const req = {
128+
url: '/path?keep=1&remove=0',
129+
method: 'GET',
130+
query: {
131+
keep: 1,
132+
remove: 0,
133+
},
134+
connection: {
135+
remoteAddress: '::ffff:172.18.0.12',
136+
remotePort: 40234,
137+
},
138+
headers: {},
139+
};
140+
const data = getRequestDataToLog(req, ['keep']);
141+
assert.deepEqual(data.params, { keep: 1 });
142+
});
143+
91144
it('should handle requests with unparsed query objects', () => {
92145
const req = {
93146
url: '/path?key=val',
@@ -100,7 +153,7 @@ describe('utils', () => {
100153
cookie: 'ok',
101154
},
102155
};
103-
const data = getRequestDataToLog(req);
156+
const data = getRequestDataToLog(req, ['key']);
104157
assert.equal(data.params.key, 'val');
105158
});
106159

0 commit comments

Comments
 (0)