@atomist/sample-sdm
Version:
Sample Atomist automation for software delivery
198 lines • 8.21 kB
JavaScript
;
/*
* Copyright © 2018 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const nsp = require("@atomist/automation-client/internal/util/cls");
const AutomationEventListener_1 = require("@atomist/automation-client/server/AutomationEventListener");
const _ = require("lodash");
const logzio_nodejs_1 = require("logzio-nodejs");
const os = require("os");
const serializeError = require("serialize-error");
const logzioWinstonTransport = require("winston-logzio");
class LogzioAutomationEventListener extends AutomationEventListener_1.AutomationEventListenerSupport {
constructor(options) {
super();
this.initLogzioLogging(options);
}
commandIncoming(payload) {
this.sendEvent("Incoming command", "request", payload);
}
commandStarting(payload, ctx) {
this.sendOperation("CommandHandler", "operation", "command-handler", payload.name, "starting");
}
commandSuccessful(payload, ctx, result) {
this.sendOperation("CommandHandler", "operation", "command-handler", payload.name, "successful", result);
return Promise.resolve();
}
commandFailed(payload, ctx, err) {
this.sendOperation("CommandHandler", "operation", "command-handler", payload.name, "failed", err);
return Promise.resolve();
}
eventIncoming(payload) {
this.sendEvent("Incoming event", "event", payload);
}
eventStarting(payload, ctx) {
this.sendOperation("EventHandler", "operation", "event-handler", payload.extensions.operationName, "starting");
}
eventSuccessful(payload, ctx, result) {
this.sendOperation("EventHandler", "operation", "event-handler", payload.extensions.operationName, "successful", result);
return Promise.resolve();
}
eventFailed(payload, ctx, err) {
this.sendOperation("EventHandler", "operation", "event-handler", payload.extensions.operationName, "failed", err);
return Promise.resolve();
}
messageSent(message, destinations, options, ctx) {
return __awaiter(this, void 0, void 0, function* () {
this.sendEvent("Outgoing message", "message", {
message,
destinations,
options,
});
});
}
sendOperation(identifier, eventType, type, name, status, err) {
const start = nsp.get().ts;
const data = {
"operation-type": type,
"operation-name": name,
"artifact": nsp.get().name,
"version": nsp.get().version,
"team-id": nsp.get().teamId,
"team-name": nsp.get().teamName,
"event-type": eventType,
"level": status === "failed" ? "error" : "info",
status,
"execution-time": Date.now() - start,
"correlation-id": nsp.get().correlationId,
"invocation-id": nsp.get().invocationId,
"message": `${identifier} ${name} invocation ${status} for ${nsp.get().teamName} '${nsp.get().teamId}'`,
};
if (err) {
if (status === "failed") {
data.stacktrace = serializeError(err);
}
else if (status === "successful") {
data.result = serializeError(err);
}
}
if (this.logzio) {
this.logzio.log(data);
}
}
sendEvent(identifier, type, payload) {
const data = {
"operation-name": nsp.get().operation,
"artifact": nsp.get().name,
"version": nsp.get().version,
"team-id": nsp.get().teamId,
"team-name": nsp.get().teamName,
"event-type": type,
"level": "info",
"correlation-id": nsp.get().correlationId,
"invocation-id": nsp.get().invocationId,
"message": `${identifier} of ${nsp.get().operation} for ${nsp.get().teamName} '${nsp.get().teamId}'`,
"payload": JSON.stringify(payload),
};
if (this.logzio) {
this.logzio.log(data);
}
}
initLogzioLogging(options) {
const logzioOptions = {
token: options.token,
level: "debug",
type: "automation-client",
protocol: "https",
bufferSize: 10,
extraFields: {
"service": options.name,
"artifact": options.name,
"version": options.version,
"environment": options.environment,
"application-id": options.application,
"process-id": process.pid,
"host": os.hostname(),
},
};
// create the logzio event logger
this.logzio = logzio_nodejs_1.createLogger(logzioOptions);
// tslint:disable:no-parameter-reassignment
logzioWinstonTransport.prototype.log = function (level, msg, meta, callback) {
if (typeof msg !== "string" && typeof msg !== "object") {
msg = { message: this.safeToString(msg) };
}
else if (typeof msg === "string") {
msg = { message: msg };
}
if (meta instanceof Error) {
meta = { error: meta.stack || meta.toString() };
}
if (nsp && nsp.get()) {
_.assign(msg, {
level,
"meta": meta,
"operation-name": nsp.get().operation,
"artifact": nsp.get().name,
"version": nsp.get().version,
"team-id": nsp.get().teamId,
"team-name": nsp.get().teamName,
"correlation-id": nsp.get().correlationId,
"invocation-id": nsp.get().invocationId,
});
}
else {
_.assign(msg, {
level,
meta,
});
}
this.logzioLogger.log(msg);
callback(null, true);
};
// create the winston logging adapter
automation_client_1.logger.add(logzioWinstonTransport, logzioOptions);
}
}
exports.LogzioAutomationEventListener = LogzioAutomationEventListener;
/**
* Configure logzio logging if token exists in configuration.
*/
function configureLogzio(configuration) {
if (_.get(configuration, "logzio.token")) {
automation_client_1.logger.debug(`adding logzio listener`);
const options = {
token: configuration.logzio.token,
name: configuration.name,
version: configuration.version,
environment: configuration.environment,
application: configuration.application,
};
configuration.listeners.push(new LogzioAutomationEventListener(options));
}
return Promise.resolve(configuration);
}
exports.configureLogzio = configureLogzio;
//# sourceMappingURL=logzio.js.map