forked from nuclon/Statsd-PostgreSQL-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticsearch.js
72 lines (59 loc) · 2.24 KB
/
elasticsearch.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
/*
THIS IS TEMPORARY UNTIL WE UPDATE OUR WORKER'S EXPORTER
*/
module.exports = (function () {
let fetch = null
const env = require("dotenv").config({ path: process.env.CONFIG_PATH || "../statsd-postgres-backend/.env" }).parsed
const sendBulkMetrics = async function (body) {
if (!fetch) {
fetch = await import("node-fetch").then(fetchMethod => fetchMethod.default);
}
const response = await fetch(
`${env.ELASTICSEARCH_ENDPOINT}/${env.ELASTICSEARCH_DATASTREAM_NAME}/_bulk`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Basic ${Buffer.from(`${env.ELASTICSEARCH_USER}:${env.ELASTICSEARCH_PASS}`).toString("base64")}`
},
body,
}
)
if (!response.ok) {
console.error("Error sending metrics to Elasticsearch: ", await response.json())
}
}
const compileMetrics = function (metrics) {
const metrics_copy = (metrics || []).slice(0);
// const _index = indexName();
let insertBuffer = []
if (metrics_copy.length === 0) {
return;
}
for (const index in metrics_copy) {
const data = {
"@timestamp": metrics_copy[index].collected,
"topic": metrics_copy[index].topic,
"category": metrics_copy[index].category,
"subcategory": metrics_copy[index].subcategory,
"identity": metrics_copy[index].identity,
"metric": metrics_copy[index].metric,
"type": metrics_copy[index].type,
"value": metrics_copy[index].value,
"tags": metrics_copy[index].tags,
}
insertBuffer.push(JSON.stringify({ create: {} }))
insertBuffer.push(JSON.stringify(data))
}
insertBuffer = insertBuffer.join("\n") + "\n"
return insertBuffer
}
const sendMetricsToElasticSearch = async function (metrics) {
const body = compileMetrics(metrics)
if (!body) return
await sendBulkMetrics(body)
}
return {
sendMetricsToElasticSearch,
}
}())