-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (44 loc) · 1.68 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
"use strict";
const { JsonConverter, ConverterFactory } = require("kafka-connect");
const PrometheusSinkConfig = require("./lib/PrometheusSinkConfig.js");
const PrometheusSinkConnector = require("./lib/sink/PrometheusSinkConnector.js");
const PrometheusSinkTask = require("./lib/sink/PrometheusSinkTask.js");
const express = require("express");
const { Registry } = require("prom-client");
const debug = require("debug")("nkc:prom:config");
const runSinkConnector = (properties, converters = [], onError = null) => {
if(!properties || !properties.connector || !properties.connector.options){
return Promise.reject(new Error("Connector configuration is missing, connector.options should be an object."));
}
if(!properties.connector.options.job){
return Promise.reject(new Error("Connector configuration is missing, connector.options.job should be set."));
}
if(!properties.http){
properties.http = {};
}
if(!Array.isArray(properties.http.middlewares)){
properties.http.middlewares = [];
}
debug(properties);
const router = express.Router();
const register = new Registry();
router.use(properties.connector.scrapeEndpoint || "/metrics", (req, res) => {
res.set("Content-Type", register.contentType);
res.end(register.metrics());
});
properties.connector.options.register = register;
properties.http.middlewares.push(router);
const config = new PrometheusSinkConfig(properties,
PrometheusSinkConnector,
PrometheusSinkTask, [JsonConverter].concat(converters));
if (onError) {
config.on("error", onError);
}
return config.run().then(() => {
return config;
});
};
module.exports = {
runSinkConnector,
ConverterFactory
};