pino-datadogmatic
Version:
Pino transporter for Datadog and Logmatic
67 lines (60 loc) • 1.97 kB
JavaScript
const fs = require('fs');
const path = require('path');
const tls = require('tls');
const split = require('split2');
const pump = require('pump');
const through = require('through2');
const logmaticCertificate = fs.readFileSync(path.join(__dirname, './logmaticapi.crt'));
const getConfig = (env, argv) => {
if (env.LOGMATIC_API_KEY)
// cf logmatic documentation: https://doc.logmatic.io/reference#input-api-tcp-tcps-udp
return {
apiKey: env.LOGMATIC_API_KEY,
host: 'api.logmatic.io',
port: 10515
};
if (env.DATADOG_API_KEY) {
const isEu = argv.includes('--eu');
return {
apiKey: env.DATADOG_API_KEY,
host: isEu ? 'tcp-intake.logs.datadoghq.eu' : 'intake.logs.datadoghq.com',
port: isEu ? 443 : 10516
};
// cf datadog doc: https://docs.datadoghq.com/api/?lang=python#send-logs-over-http
}
if (env.API_KEY && env.HOST && env.PORT) {
return {
apiKey: env.API_KEY,
host: env.HOST,
port: parseInt(env.PORT, 10)
};
}
throw new Error(
`datadogmatic need to be configured either with 'LOGMATIC_API_KEY' or 'DATADOG_API_KEY' env variable
or a trio of 'API_KEY', 'HOST' and 'PORT'`
);
};
const config = getConfig(process.env, process.argv);
const datadogmaticTransporter = through.obj(function(chunk, enc, cb) {
// §todo: To improve later by merging connection;
try {
const client = tls.connect(
{port: config.port, host: config.host, ca: [logmaticCertificate]},
() => {
client.write(`${config.apiKey} ${chunk}\n`, () => {
this.push(chunk);
this.push('\n');
client.end();
cb(null);
});
}
);
} catch (err) {
process.stderr.write(`pino-datadogmatic connection error: ${err.message}`);
this.push(chunk);
this.push('\n');
return cb(null);
}
});
const main = () => pump(process.stdin, split(), datadogmaticTransporter, process.stdout);
module.exports = main;