Netlify's build plugins allow us to to perform a variety of custom build/deploy related tasks. One such task might be to log deployment status in Datadog for analysis. Thankfully this is easy using agent-less logging.
Here's what a simple log plugin might look like.
const createLogger = require('./logger');
module.exports = {
onSuccess: async ({ inputs: { service } }) => {
const logger = createLogger({ service });
logger.info('Deploy successful');
await logger.send();
},
onError: async ({ inputs: { service } }) => {
let logger = createLogger({ service });
logger.info('Deploy failed');
await logger.send();
},
};
And below, a logger implementation using Winston.
const {
env: { DATADOG_API_KEY, VERSON, ENVIRONMENT },
} = require('process');
function createLogger({ service }) {
const { createLogger, format, transports } = require('winston');
const queryString = require('query-string');
const { combine, json, timestamp } = format;
const query = queryString.stringify(
{
ddsource: `nodejs`,
service,
ddtags: [`env:${ENVIRONMENT)}`, `releaseVersion:${VERSON)}`],
},
{ arrayFormat: 'comma' },
);
const httpTransportOptions = {
host: 'http-intake.logs.datadoghq.com',
path: `/v1/input/${DATADOG_API_KEY}?${query}`,
format: combine(timestamp(), json()),
ssl: true,
};
const logger = createLogger({
level: 'info',
exitOnError: false,
transports: [new transports.Http(httpTransportOptions)],
});
return {
info: (...args) => logger.info(...args),
send: () => {
const completePromise = new Promise((resolve) => {
logger.on('finish', () => {
clearTimeout(timeout);
console.log(`Successfully sent logs to Datadog`);
resolve(true);
});
});
logger.end();
return completePromise;
},
};
}
Top comments (0)