forked from IBM/logmet-client-njs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogmetConsumer.js
95 lines (79 loc) · 2.94 KB
/
LogmetConsumer.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
/**
* This class encapsulates the Logmet client code, used to query data from Logmet.
*/
var https = require('https');
var logger = require('./logger');
/*
* @constructor
* @this {LogmetConsumer}
*
* @param {string} logmetQueryEndpoint The hostname or IP address of the Logmet server used for querying
*
*/
function LogmetConsumer(logmetQueryEndpoint) {
this.endpoint = logmetQueryEndpoint;
}
//Export the constructor
module.exports = LogmetConsumer;
/*******************************************
* Public Interface of LogmetClient
*******************************************/
/*
* Queries Logmet for data.
*
* @param {string} tenantId The id of the tenant who owns the data
* @param {string} bearerToken The bearer token of the tenant
* @param {string} type The type associated with the Elasticsearch documents to be searched for
* @param {object} queryDSLBody An object representing a query expressed in terms of the Elasticsearch query DSL
* @param {function(error, data)} callback A callback function used to process errors or the data returned by the query
*
* @return An array of objects, where each object corresponds to an Elasticsearch document
*/
LogmetConsumer.prototype.query = function(tenantId, bearerToken, type, queryDSLBody, callback) {
var validatedBearerToken = validateToken(bearerToken);
var path = '/elasticsearch/logstash-' + tenantId + '-*/' + type + '/_search';
var queryOptions = {
hostname: this.endpoint,
path: path,
headers: {
'Accept': 'application/json',
'X-Auth-Token': validatedBearerToken,
'X-Auth-Project-Id': tenantId
},
method: 'POST'
};
var queryBodyJSONString = JSON.stringify(queryDSLBody);
logger.info('Performing Logmet query', {tenant_id: tenantId, doc_type: type, query_body: queryBodyJSONString});
var retrievedString = '';
var retrievedObject;
var request = https.request(queryOptions, function(result) {
result.on('data', function(chunk) {
logger.debug('Received a chunk of Logmet data: ' + chunk);
retrievedString += chunk;
});
result.on('end', function() {
if (retrievedString) {
retrievedObject = JSON.parse(retrievedString);
callback('', retrievedObject.hits.hits);
} else {
callback('', []);
}
});
});
request.write(queryBodyJSONString);
request.end();
request.on('error', function (error) {
logger.warn('ERROR returned by Logmet query: ' + error);
callback(error);
});
};
/*******************************************
* Module's private functions
*******************************************/
function validateToken(bearerToken) {
var validatedToken = bearerToken;
if (bearerToken.startsWith('bearer ')) {
validatedToken = bearerToken.replace('bearer ', '');
}
return validatedToken;
}