This repository was archived by the owner on Jan 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
131 lines (121 loc) · 4.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const Job = require('./lib/job');
const stream = require('./lib/stream');
/**
* Create a readable stream of messages or records that result from a Sumo Logic
* search. **Messages** are the raw log messages, **records** are the result of
* a search with some form of aggregation (e.g. `count by _sourceCategory`).
* Credentials can be provided explicitly, or read from environment variables:
* `SUMO_LOGIC_ACCESS_ID` and `SUMO_LOGIC_ACCESS_KEY`.
*
* @param {string} type - one of `messages` or `records`
* @param {object} search - Sumo Logic search parameters
* @param {string} search.query - the query string
* @param {number} search.from - the starting timestamp in ms
* @param {number} search.to - the ending timestamp in ms
* @param {object} [search.auth] - Sumo Logic credentials
* @param {string} [search.auth.accessId] - Sumo Logic access ID
* @param {string} [search.auth.accessKey] - Sumo Logic access key
* @param {object} options - readable stream options
* @returns {object} a readable stream of messages or records
*
* @example
* const sumo = require('@mapbox/sumo');
* const search = {
* query: '"error" | count by _sourceCategory',
* from: 1487733054071,
* to: 1487733356114,
* auth: {
* accessId: xxx,
* accessKey: xxxx
* }
* };
* const messages = sumo.createReadStream('messages', search);
* messages.on('data', (msg) => console.log(msg));
*/
module.exports.createReadStream = (type, search, options) => {
if (!/^messages|records$/.test(type))
throw new Error('type must be either messages or records');
if (!search.auth) search.auth = {
accessId: process.env.SUMO_LOGIC_ACCESS_ID,
accessKey: process.env.SUMO_LOGIC_ACCESS_KEY
};
if (type === 'messages') return stream.Messages.create(search, options);
if (type === 'records') return stream.Records.create(search, options);
};
/**
* Perform a search limited to up to 10,000 results. This will return both
* raw messages and aggregate records where applicable. Credentials can be
* provided explicitly, or read from environment variables:
* `SUMO_LOGIC_ACCESS_ID` and `SUMO_LOGIC_ACCESS_KEY`.
*
* @param {object} search - Sumo Logic search parameters
* @param {string} search.query - the query string
* @param {number} search.from - the starting timestamp in ms
* @param {number} search.to - the ending timestamp in ms
* @param {number} [search.limit=10000] - the maximum number of messages/records
* @param {object} [search.auth] - Sumo Logic credentials
* @param {string} [search.auth.accessId] - Sumo Logic access ID
* @param {string} [search.auth.accessKey] - Sumo Logic access key
* @param {function} [callback] - a function to call with the results
* @returns {promise} resolves with the results, an object with two properties,
* each of which are an array: `.messages` and `.records`
*
* @example
* const sumo = require('@mapbox/sumo');
* const search = {
* query: '"error" | count by _sourceCategory',
* from: 1487733054071,
* to: 1487733356114,
* auth: {
* accessId: xxx,
* accessKey: xxxx
* }
* };
* sumo.search(search, (err, data) => {
* if (err) throw err;
* data.messages.forEach((msg) => console.log(msg));
* data.records.forEach((rec) => console.log(rec));
* });
*/
module.exports.search = (search, callback) => {
callback = callback || function() {};
search.limit = search.limit || 10000;
const limit = Math.min(10000, search.limit);
if (!search.auth) search.auth = {
accessId: process.env.SUMO_LOGIC_ACCESS_ID,
accessKey: process.env.SUMO_LOGIC_ACCESS_KEY
};
return Job.create(
search.auth,
search.query,
search.from,
search.to
).then((job) => {
let hasRecords = false;
const getRecords = new Promise((resolve) => {
job.once('records', () => {
hasRecords = true;
return job.fetchRecords(limit)
.then((records) => resolve(records));
});
job.once('completed', () => {
if (!hasRecords) resolve([]);
});
});
return Promise.all([
job.fetchMessages(limit),
getRecords
]);
}).then((results) => {
const data = {
messages: results[0],
records: results[1]
};
callback(null, data);
return Promise.resolve(data);
}).catch((err) => {
callback(err);
return Promise.reject(err);
});
};