Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
2d21b40a76 | |||
2d1a5cdc50 | |||
20a41d3381 | |||
b2019b33f8 | |||
1ab582db51 | |||
9c87f5ee5e | |||
ef9cb193d5 | |||
9f706e0a70 | |||
b2d4b82532 | |||
63713f4cd2 | |||
5d3bc13126 | |||
4b30234cb6 | |||
271971a373 | |||
d636dab664 | |||
5f94db8d5d | |||
f9866076ca | |||
aa43a221a0 | |||
cb63b305ad | |||
6fde0544f5 | |||
6ed2b6e993 | |||
4f1df305ed | |||
44fccc252e | |||
0854207a04 | |||
64b71dfb42 | |||
d846b100d2 | |||
cb4cfaca09 | |||
3232114c5b |
@ -37,18 +37,6 @@ snyk:
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
testLEGACY:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install legacy
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
allow_failure: true
|
||||
|
||||
testLTS:
|
||||
stage: test
|
||||
@ -135,13 +123,3 @@ pages:
|
||||
paths:
|
||||
- public
|
||||
allow_failure: true
|
||||
|
||||
windowsCompatibility:
|
||||
image: stefanscherer/node-windows:10-build-tools
|
||||
stage: metadata
|
||||
script:
|
||||
- npm install & npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- windows
|
||||
allow_failure: true
|
||||
|
4
.snyk
Normal file
4
.snyk
Normal file
@ -0,0 +1,4 @@
|
||||
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
|
||||
version: v1.12.0
|
||||
ignore: {}
|
||||
patch: {}
|
31
dist/elasticlog.classes.elasticlog.d.ts
vendored
31
dist/elasticlog.classes.elasticlog.d.ts
vendored
@ -1,31 +0,0 @@
|
||||
import { Client as ElasticClient } from 'elasticsearch';
|
||||
import { ILogContext } from 'smartlog-interfaces';
|
||||
import { LogScheduler } from './elasticlog.classes.logscheduler';
|
||||
export interface IStandardLogParams {
|
||||
message: string;
|
||||
severity: string;
|
||||
}
|
||||
export interface IElasticLogConstructorOptions {
|
||||
port: number;
|
||||
domain: string;
|
||||
ssl: boolean;
|
||||
user?: string;
|
||||
pass?: string;
|
||||
logContext: ILogContext;
|
||||
}
|
||||
export declare class ElasticLog<T> {
|
||||
client: ElasticClient;
|
||||
logContext: ILogContext;
|
||||
logScheduler: LogScheduler;
|
||||
/**
|
||||
* sets up an instance of Elastic log
|
||||
* @param optionsArg
|
||||
*/
|
||||
constructor(optionsArg: IElasticLogConstructorOptions);
|
||||
/**
|
||||
* computes the host string from the constructor options
|
||||
* @param optionsArg
|
||||
*/
|
||||
private computeHostString(optionsArg);
|
||||
log(logObject: IStandardLogParams, scheduleOverwrite?: boolean): Promise<void>;
|
||||
}
|
77
dist/elasticlog.classes.elasticlog.js
vendored
77
dist/elasticlog.classes.elasticlog.js
vendored
@ -1,77 +0,0 @@
|
||||
"use strict";
|
||||
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 });
|
||||
// interfaces
|
||||
const elasticsearch_1 = require("elasticsearch");
|
||||
// other classes
|
||||
const elasticlog_classes_logscheduler_1 = require("./elasticlog.classes.logscheduler");
|
||||
class ElasticLog {
|
||||
/**
|
||||
* sets up an instance of Elastic log
|
||||
* @param optionsArg
|
||||
*/
|
||||
constructor(optionsArg) {
|
||||
this.logScheduler = new elasticlog_classes_logscheduler_1.LogScheduler(this);
|
||||
this.logContext = optionsArg.logContext;
|
||||
this.client = new elasticsearch_1.Client({
|
||||
host: this.computeHostString(optionsArg),
|
||||
log: 'trace'
|
||||
});
|
||||
}
|
||||
/**
|
||||
* computes the host string from the constructor options
|
||||
* @param optionsArg
|
||||
*/
|
||||
computeHostString(optionsArg) {
|
||||
let hostString = `${optionsArg.domain}:${optionsArg.port}`;
|
||||
if (optionsArg.user && optionsArg.pass) {
|
||||
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`;
|
||||
}
|
||||
if (optionsArg.ssl) {
|
||||
hostString = `https://${hostString}`;
|
||||
}
|
||||
else {
|
||||
hostString = `http://${hostString}`;
|
||||
}
|
||||
return hostString;
|
||||
}
|
||||
log(logObject, scheduleOverwrite = false) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const now = new Date();
|
||||
if (this.logScheduler.logsScheduled && !scheduleOverwrite) {
|
||||
this.logScheduler.scheduleLog(logObject);
|
||||
return;
|
||||
}
|
||||
this.client.index({
|
||||
index: `logs-${now.getFullYear()}.${('0' + (now.getMonth() + 1)).slice(-2)}.${now.getDate()}`,
|
||||
type: 'log',
|
||||
body: {
|
||||
'@timestamp': now.toISOString(),
|
||||
zone: this.logContext.zone,
|
||||
container: this.logContext.containerName,
|
||||
environment: this.logContext.environment,
|
||||
severity: logObject.severity,
|
||||
message: logObject.message
|
||||
}
|
||||
}, (error, response) => {
|
||||
if (error) {
|
||||
console.log('ElasticLog encountered an error:');
|
||||
console.log(error);
|
||||
this.logScheduler.addFailedLog(logObject);
|
||||
}
|
||||
else {
|
||||
console.log(`ElasticLog: ${logObject.message}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.ElasticLog = ElasticLog;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5jbGFzc2VzLmVsYXN0aWNsb2cuanMiLCJzb3VyY2VSb290IjoiIiwic291cm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BQzNCO2FBQ0YsRUFDRCxDQUFDLEtBQUssRUFBRSxRQUFRLEVBQUUsRUFBRTtnQkFDbEIsRUFBRSxDQUFDLENBQUMsS0FBSyxDQUFDLENBQUMsQ0FBQztvQkFDVixPQUFPLENBQUMsR0FBRyxDQUFDLGtDQUFrQyxDQUFDLENBQUM7b0JBQ2hELE9BQU8sQ0FBQyxHQUFHLENBQUMsS0FBSyxDQUFDLENBQUM7b0JBQ25CLElBQUksQ0FBQyxZQUFZLENBQUMsWUFBWSxDQUFDLFNBQVMsQ0FBQyxDQUFDO2dCQUM1QyxDQUFDO2dCQUFDLElBQUksQ0FBQyxDQUFDO29CQUNOLE9BQU8sQ0FBQyxHQUFHLENBQUMsZUFBZSxTQUFTLENBQUMsT0FBTyxFQUFFLENBQUMsQ0FBQztnQkFDbEQsQ0FBQztZQUNILENBQUMsQ0FDRixDQUFDO1FBQ0osQ0FBQztLQUFBO0NBQ0Y7QUFsRUQsZ0NBa0VDIn0=
|
11
dist/elasticlog.classes.logscheduler.d.ts
vendored
11
dist/elasticlog.classes.logscheduler.d.ts
vendored
@ -1,11 +0,0 @@
|
||||
import { ElasticLog, IStandardLogParams } from './elasticlog.classes.elasticlog';
|
||||
export declare class LogScheduler {
|
||||
elasticLogRef: ElasticLog<any>;
|
||||
logsScheduled: boolean;
|
||||
logStorage: any[];
|
||||
constructor(elasticLogRefArg: ElasticLog<any>);
|
||||
addFailedLog(objectArg: any | IStandardLogParams): void;
|
||||
scheduleLog(logObject: any): void;
|
||||
setRetry(): void;
|
||||
deferSend(): void;
|
||||
}
|
42
dist/elasticlog.classes.logscheduler.js
vendored
42
dist/elasticlog.classes.logscheduler.js
vendored
@ -1,42 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
class LogScheduler {
|
||||
constructor(elasticLogRefArg) {
|
||||
this.logsScheduled = false;
|
||||
this.logStorage = [];
|
||||
this.elasticLogRef = elasticLogRefArg;
|
||||
}
|
||||
addFailedLog(objectArg) {
|
||||
this.logStorage.push(objectArg);
|
||||
this.setRetry();
|
||||
}
|
||||
scheduleLog(logObject) {
|
||||
this.logStorage.push(logObject);
|
||||
}
|
||||
setRetry() {
|
||||
setTimeout(() => {
|
||||
const oldStorage = this.logStorage;
|
||||
this.logStorage = [];
|
||||
for (let logObject of oldStorage) {
|
||||
this.elasticLogRef.log(logObject, true);
|
||||
}
|
||||
if (this.logStorage.length === 0) {
|
||||
console.log('ElasticLog retry success!!!');
|
||||
this.logsScheduled = false;
|
||||
}
|
||||
else {
|
||||
console.log('ElasticLog retry failed');
|
||||
this.setRetry();
|
||||
}
|
||||
}, 5000);
|
||||
}
|
||||
deferSend() {
|
||||
if (!this.logsScheduled) {
|
||||
console.log('Retry ElasticLog in 5 seconds!');
|
||||
this.logsScheduled = true;
|
||||
this.setRetry();
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.LogScheduler = LogScheduler;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5jbGFzc2VzLmxvZ3NjaGVkdWxlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL2VsYXN0aWNsb2cuY2xhc3Nlcy5sb2dzY2hlZHVsZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFFQTtJQUtFLFlBQVksZ0JBQWlDO1FBSDdDLGtCQUFhLEdBQUcsS0FBSyxDQUFDO1FBQ3RCLGVBQVUsR0FBVSxFQUFFLENBQUM7UUFHckIsSUFBSSxDQUFDLGFBQWEsR0FBRyxnQkFBZ0IsQ0FBQztJQUN4QyxDQUFDO0lBRUQsWUFBWSxDQUFDLFNBQW1DO1FBQzlDLElBQUksQ0FBQyxVQUFVLENBQUMsSUFBSSxDQUFDLFNBQVMsQ0FBQyxDQUFDO1FBQ2hDLElBQUksQ0FBQyxRQUFRLEVBQUUsQ0FBQztJQUNsQixDQUFDO0l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zFCLElBQUksQ0FBQyxRQUFRLEVBQUUsQ0FBQztRQUNsQixDQUFDO0lBQ0gsQ0FBQztDQUNGO0FBekNELG9DQXlDQyJ9
|
4
dist/elasticlog.plugins.d.ts
vendored
4
dist/elasticlog.plugins.d.ts
vendored
@ -1,4 +0,0 @@
|
||||
import * as elasticsearch from 'elasticsearch';
|
||||
import * as smartdelay from 'smartdelay';
|
||||
import * as smartlogInterfaces from 'smartlog-interfaces';
|
||||
export { elasticsearch, smartdelay, smartlogInterfaces };
|
9
dist/elasticlog.plugins.js
vendored
9
dist/elasticlog.plugins.js
vendored
@ -1,9 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const elasticsearch = require("elasticsearch");
|
||||
exports.elasticsearch = elasticsearch;
|
||||
const smartdelay = require("smartdelay");
|
||||
exports.smartdelay = smartdelay;
|
||||
const smartlogInterfaces = require("smartlog-interfaces");
|
||||
exports.smartlogInterfaces = smartlogInterfaces;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5wbHVnaW5zLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvZWxhc3RpY2xvZy5wbHVnaW5zLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsK0NBQStDO0FBR3RDLHNDQUFhO0FBRnRCLHlDQUF5QztBQUVqQixnQ0FBVTtBQURsQywwREFBMEQ7QUFDdEIsZ0RBQWtCIn0=
|
1
dist/index.d.ts
vendored
1
dist/index.d.ts
vendored
@ -1 +0,0 @@
|
||||
export * from './elasticlog.classes.elasticlog';
|
7
dist/index.js
vendored
7
dist/index.js
vendored
@ -1,7 +0,0 @@
|
||||
"use strict";
|
||||
function __export(m) {
|
||||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
||||
}
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
__export(require("./elasticlog.classes.elasticlog"));
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLHFEQUFnRCJ9
|
1591
package-lock.json
generated
1591
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
33
package.json
33
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/elasticlog",
|
||||
"version": "1.0.13",
|
||||
"name": "@mojoio/elasticsearch",
|
||||
"version": "1.0.27",
|
||||
"private": false,
|
||||
"description": "log to elasticsearch in a kibana compatible format",
|
||||
"main": "dist/index.js",
|
||||
@ -10,22 +10,25 @@
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"format": "(gitzone format)",
|
||||
"build": "echo \"Not needed for now\""
|
||||
"build": "(tsbuild)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tsrun": "^1.1.13",
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/qenv": "^2.0.2",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^10.12.2",
|
||||
"tslint": "^5.11.0",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
"@gitzone/tsbuild": "^2.1.17",
|
||||
"@gitzone/tsrun": "^1.2.8",
|
||||
"@gitzone/tstest": "^1.0.28",
|
||||
"@pushrocks/qenv": "^4.0.6",
|
||||
"@pushrocks/tapbundle": "^3.0.13",
|
||||
"@types/node": "^12.12.5",
|
||||
"tslint": "^5.20.0",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartdelay": "^2.0.2",
|
||||
"@pushrocks/smartlog-interfaces": "^1.0.15",
|
||||
"@types/elasticsearch": "^5.0.28",
|
||||
"elasticsearch": "^15.2.0"
|
||||
"@pushrocks/lik": "^3.0.11",
|
||||
"@pushrocks/smartdelay": "^2.0.3",
|
||||
"@pushrocks/smartlog-interfaces": "^2.0.9",
|
||||
"@pushrocks/smartpromise": "^3.0.6",
|
||||
"@pushrocks/smarttime": "^3.0.12",
|
||||
"@types/elasticsearch": "^5.0.35",
|
||||
"elasticsearch": "^16.5.0"
|
||||
}
|
||||
}
|
||||
|
36
test/test.ts
36
test/test.ts
@ -1,31 +1,35 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import { Qenv } from '@pushrocks/qenv';
|
||||
import * as elasticlog from '../ts/index';
|
||||
import * as elasticsearch from '../ts/index';
|
||||
|
||||
const testQenv = new Qenv('./', './.nogit/');
|
||||
|
||||
let testElasticLog: elasticlog.ElasticLog<any>;
|
||||
let testElasticLog: elasticsearch.ElasticSearch<any>;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
testElasticLog = new elasticlog.ElasticLog({
|
||||
domain: process.env.ELK_DOMAIN,
|
||||
port: parseInt(process.env.ELK_PORT, 10),
|
||||
ssl: true,
|
||||
user: process.env.ELK_USER,
|
||||
pass: process.env.ELK_PASS,
|
||||
logContext: {
|
||||
company: 'Lossless GmbH',
|
||||
runtime: 'node',
|
||||
containerName: 'testContainer',
|
||||
environment: 'test'
|
||||
}
|
||||
testElasticLog = new elasticsearch.ElasticSearch({
|
||||
indexPrefix: 'smartlog',
|
||||
indexRetention: 7,
|
||||
domain: testQenv.getEnvVarOnDemand('ELK_DOMAIN'),
|
||||
port: parseInt(testQenv.getEnvVarOnDemand('ELK_PORT'), 10),
|
||||
ssl: true
|
||||
});
|
||||
expect(testElasticLog).to.be.instanceOf(elasticlog.ElasticLog);
|
||||
expect(testElasticLog).to.be.instanceOf(elasticsearch.ElasticSearch);
|
||||
});
|
||||
|
||||
tap.test('should send a message to Elasticsearch', async () => {
|
||||
testElasticLog.log({
|
||||
severity: 'log',
|
||||
timestamp: Date.now(),
|
||||
type: 'increment',
|
||||
level: 'info',
|
||||
context: {
|
||||
company: 'Lossless GmbH',
|
||||
companyunit: 'lossless.cloud',
|
||||
containerName: 'testcontainer',
|
||||
environment: 'test',
|
||||
runtime: 'node',
|
||||
zone: 'ship.zone'
|
||||
},
|
||||
message: 'hi, this is a testMessage'
|
||||
});
|
||||
});
|
||||
|
@ -1,88 +0,0 @@
|
||||
// interfaces
|
||||
import { Client as ElasticClient } from 'elasticsearch';
|
||||
import { ILogContext } from '@pushrocks/smartlog-interfaces';
|
||||
|
||||
// other classes
|
||||
import { LogScheduler } from './elasticlog.classes.logscheduler';
|
||||
|
||||
export interface IStandardLogParams {
|
||||
message: string;
|
||||
severity: string;
|
||||
}
|
||||
|
||||
export interface IElasticLogConstructorOptions {
|
||||
port: number;
|
||||
domain: string;
|
||||
ssl: boolean;
|
||||
user?: string;
|
||||
pass?: string;
|
||||
logContext: ILogContext;
|
||||
}
|
||||
|
||||
export class ElasticLog<T> {
|
||||
client: ElasticClient;
|
||||
logContext: ILogContext;
|
||||
logScheduler = new LogScheduler(this);
|
||||
|
||||
/**
|
||||
* sets up an instance of Elastic log
|
||||
* @param optionsArg
|
||||
*/
|
||||
constructor(optionsArg: IElasticLogConstructorOptions) {
|
||||
this.logContext = optionsArg.logContext;
|
||||
this.client = new ElasticClient({
|
||||
host: this.computeHostString(optionsArg),
|
||||
log: 'trace'
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* computes the host string from the constructor options
|
||||
* @param optionsArg
|
||||
*/
|
||||
private computeHostString(optionsArg: IElasticLogConstructorOptions): string {
|
||||
let hostString = `${optionsArg.domain}:${optionsArg.port}`;
|
||||
if (optionsArg.user && optionsArg.pass) {
|
||||
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`;
|
||||
}
|
||||
if (optionsArg.ssl) {
|
||||
hostString = `https://${hostString}`;
|
||||
} else {
|
||||
hostString = `http://${hostString}`;
|
||||
}
|
||||
return hostString;
|
||||
}
|
||||
|
||||
async log(logObject: IStandardLogParams, scheduleOverwrite = false) {
|
||||
const now = new Date();
|
||||
if (this.logScheduler.logsScheduled && !scheduleOverwrite) {
|
||||
this.logScheduler.scheduleLog(logObject);
|
||||
return;
|
||||
}
|
||||
this.client.index(
|
||||
{
|
||||
index: `logstash-${now.getFullYear()}.${('0' + (now.getMonth() + 1)).slice(-2)}.${(
|
||||
'0' + now.getDate()
|
||||
).slice(-2)}`,
|
||||
type: 'log',
|
||||
body: {
|
||||
'@timestamp': now.toISOString(),
|
||||
zone: this.logContext.zone,
|
||||
container: this.logContext.containerName,
|
||||
environment: this.logContext.environment,
|
||||
severity: logObject.severity,
|
||||
message: logObject.message
|
||||
}
|
||||
},
|
||||
(error, response) => {
|
||||
if (error) {
|
||||
console.log('ElasticLog encountered an error:');
|
||||
console.log(error);
|
||||
this.logScheduler.addFailedLog(logObject);
|
||||
} else {
|
||||
console.log(`ElasticLog: ${logObject.message}`);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
import { ElasticLog, IStandardLogParams } from './elasticlog.classes.elasticlog';
|
||||
|
||||
export class LogScheduler {
|
||||
elasticLogRef: ElasticLog<any>;
|
||||
logsScheduled = false;
|
||||
logStorage: any[] = [];
|
||||
|
||||
constructor(elasticLogRefArg: ElasticLog<any>) {
|
||||
this.elasticLogRef = elasticLogRefArg;
|
||||
}
|
||||
|
||||
addFailedLog(objectArg: any | IStandardLogParams) {
|
||||
this.logStorage.push(objectArg);
|
||||
this.setRetry();
|
||||
}
|
||||
scheduleLog(logObject: any) {
|
||||
this.logStorage.push(logObject);
|
||||
}
|
||||
|
||||
setRetry() {
|
||||
setTimeout(() => {
|
||||
const oldStorage = this.logStorage;
|
||||
this.logStorage = [];
|
||||
for (let logObject of oldStorage) {
|
||||
this.elasticLogRef.log(logObject, true);
|
||||
}
|
||||
if (this.logStorage.length === 0) {
|
||||
console.log('ElasticLog retry success!!!');
|
||||
this.logsScheduled = false;
|
||||
} else {
|
||||
console.log('ElasticLog retry failed');
|
||||
this.setRetry();
|
||||
}
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
deferSend() {
|
||||
if (!this.logsScheduled) {
|
||||
console.log('Retry ElasticLog in 5 seconds!');
|
||||
this.logsScheduled = true;
|
||||
this.setRetry();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
import * as elasticsearch from 'elasticsearch';
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartlogInterfaces from '@pushrocks/smartlog-interfaces';
|
||||
export { elasticsearch, smartdelay, smartlogInterfaces };
|
106
ts/elasticsearch.classes.elasticindex.ts
Normal file
106
ts/elasticsearch.classes.elasticindex.ts
Normal file
@ -0,0 +1,106 @@
|
||||
import * as plugins from './elasticsearch.plugins';
|
||||
import { ElasticSearch } from './elasticsearch.classes.elasticsearch';
|
||||
import { ILogPackage } from '@pushrocks/smartlog-interfaces';
|
||||
|
||||
import { Stringmap } from '@pushrocks/lik';
|
||||
|
||||
export class ElasticIndex {
|
||||
private stringmap = new Stringmap();
|
||||
private elasticSearchRef: ElasticSearch<any>;
|
||||
|
||||
constructor(elasticSearchInstanceArg: ElasticSearch<ILogPackage>) {
|
||||
this.elasticSearchRef = elasticSearchInstanceArg;
|
||||
}
|
||||
|
||||
public async ensureIndex(indexArg: string) {
|
||||
const done = plugins.smartpromise.defer();
|
||||
if (this.stringmap.checkString(indexArg)) {
|
||||
done.resolve();
|
||||
return;
|
||||
}
|
||||
this.elasticSearchRef.client.cat.indices(
|
||||
{
|
||||
format: 'json',
|
||||
bytes: 'm'
|
||||
},
|
||||
async (err, responseArg: any[]) => {
|
||||
if(err) {
|
||||
console.log(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// lets delete indexes that violate the retention
|
||||
if(Array.isArray(responseArg)) {
|
||||
const filteredIndices = responseArg.filter(indexObjectArg => {
|
||||
return indexObjectArg.index.startsWith('smartlog');
|
||||
});
|
||||
const filteredIndexNames = filteredIndices.map(indexObjectArg => {
|
||||
return indexObjectArg.index;
|
||||
});
|
||||
this.deleteOldIndices(filteredIndexNames);
|
||||
}
|
||||
|
||||
let index = null;
|
||||
|
||||
if(Array.isArray(responseArg)) {
|
||||
index = responseArg.find(indexObject => {
|
||||
return indexObject.index === indexArg;
|
||||
});
|
||||
}
|
||||
|
||||
if (!index) {
|
||||
const done2 = plugins.smartpromise.defer();
|
||||
this.elasticSearchRef.client.indices.create(
|
||||
{
|
||||
|
||||
waitForActiveShards: '1',
|
||||
index: indexArg
|
||||
},
|
||||
(error, response) => {
|
||||
// console.lof(response)
|
||||
done2.resolve();
|
||||
}
|
||||
);
|
||||
await done2.promise;
|
||||
}
|
||||
this.stringmap.addString(indexArg);
|
||||
done.resolve();
|
||||
}
|
||||
);
|
||||
await done.promise;
|
||||
}
|
||||
|
||||
public createNewIndex(indexNameArg: string) {
|
||||
|
||||
}
|
||||
|
||||
public async deleteOldIndices(indicesArray: string[]) {
|
||||
const todayAsUnix: number = Date.now();
|
||||
const rententionPeriodAsUnix: number = plugins.smarttime.units.days(
|
||||
this.elasticSearchRef.indexRetention
|
||||
);
|
||||
for (const indexName of indicesArray) {
|
||||
const regexResult = /^smartlog-([0-9]*)\.([0-9]*)\.([0-9]*)$/.exec(indexName);
|
||||
const dateAsUnix: number = new Date(
|
||||
`${regexResult[1]}-${regexResult[2]}-${regexResult[3]}`
|
||||
).getTime();
|
||||
if (todayAsUnix - rententionPeriodAsUnix > dateAsUnix) {
|
||||
console.log(`found old index ${indexName}`);
|
||||
const done2 = plugins.smartpromise.defer();
|
||||
this.elasticSearchRef.client.indices.delete(
|
||||
{
|
||||
index: indexName
|
||||
},
|
||||
(err2, response2) => {
|
||||
if (err2) {
|
||||
console.log(err2);
|
||||
}
|
||||
console.log(`deleted ${indexName}`);
|
||||
done2.resolve();
|
||||
}
|
||||
);
|
||||
await done2.promise;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
44
ts/elasticsearch.classes.elasticscheduler.ts
Normal file
44
ts/elasticsearch.classes.elasticscheduler.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { ElasticSearch, IStandardLogParams } from './elasticsearch.classes.elasticsearch';
|
||||
|
||||
export class ElasticScheduler {
|
||||
elasticSearchRef: ElasticSearch<any>;
|
||||
docsScheduled = false;
|
||||
docsStorage: any[] = [];
|
||||
|
||||
constructor(elasticLogRefArg: ElasticSearch<any>) {
|
||||
this.elasticSearchRef = elasticLogRefArg;
|
||||
}
|
||||
|
||||
public addFailedDoc(objectArg: any | IStandardLogParams) {
|
||||
this.docsStorage.push(objectArg);
|
||||
this.setRetry();
|
||||
}
|
||||
public scheduleDoc(logObject: any) {
|
||||
this.docsStorage.push(logObject);
|
||||
}
|
||||
|
||||
public setRetry() {
|
||||
setTimeout(() => {
|
||||
const oldStorage = this.docsStorage;
|
||||
this.docsStorage = [];
|
||||
for (let logObject of oldStorage) {
|
||||
this.elasticSearchRef.log(logObject, true);
|
||||
}
|
||||
if (this.docsStorage.length === 0) {
|
||||
console.log('ElasticLog retry success!!!');
|
||||
this.docsScheduled = false;
|
||||
} else {
|
||||
console.log('ElasticLog retry failed');
|
||||
this.setRetry();
|
||||
}
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
public deferSend() {
|
||||
if (!this.docsScheduled) {
|
||||
console.log('Retry ElasticLog in 5 seconds!');
|
||||
this.docsScheduled = true;
|
||||
this.setRetry();
|
||||
}
|
||||
}
|
||||
}
|
105
ts/elasticsearch.classes.elasticsearch.ts
Normal file
105
ts/elasticsearch.classes.elasticsearch.ts
Normal file
@ -0,0 +1,105 @@
|
||||
// interfaces
|
||||
import { Client as ElasticClient } from 'elasticsearch';
|
||||
import { ILogContext, ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
|
||||
|
||||
// other classes
|
||||
import { ElasticScheduler } from './elasticsearch.classes.elasticscheduler';
|
||||
import { ElasticIndex } from './elasticsearch.classes.elasticindex';
|
||||
|
||||
export interface IStandardLogParams {
|
||||
message: string;
|
||||
severity: string;
|
||||
}
|
||||
|
||||
export interface IElasticSearchConstructorOptions {
|
||||
indexPrefix: string;
|
||||
indexRetention: number;
|
||||
port: number;
|
||||
domain: string;
|
||||
ssl: boolean;
|
||||
user?: string;
|
||||
pass?: string;
|
||||
}
|
||||
|
||||
export class ElasticSearch<T> {
|
||||
public client: ElasticClient;
|
||||
public elasticScheduler = new ElasticScheduler(this);
|
||||
public elasticIndex: ElasticIndex = new ElasticIndex(this);
|
||||
|
||||
public indexPrefix: string;
|
||||
public indexRetention: number;
|
||||
|
||||
/**
|
||||
* sets up an instance of Elastic log
|
||||
* @param optionsArg
|
||||
*/
|
||||
constructor(optionsArg: IElasticSearchConstructorOptions) {
|
||||
this.client = new ElasticClient({
|
||||
host: this.computeHostString(optionsArg),
|
||||
// log: 'trace'
|
||||
});
|
||||
this.indexPrefix = optionsArg.indexPrefix;
|
||||
this.indexRetention = optionsArg.indexRetention;
|
||||
}
|
||||
|
||||
/**
|
||||
* computes the host string from the constructor options
|
||||
* @param optionsArg
|
||||
*/
|
||||
private computeHostString(optionsArg: IElasticSearchConstructorOptions): string {
|
||||
let hostString = `${optionsArg.domain}:${optionsArg.port}`;
|
||||
if (optionsArg.user && optionsArg.pass) {
|
||||
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`;
|
||||
}
|
||||
if (optionsArg.ssl) {
|
||||
hostString = `https://${hostString}`;
|
||||
} else {
|
||||
hostString = `http://${hostString}`;
|
||||
}
|
||||
console.log(hostString);
|
||||
return hostString;
|
||||
}
|
||||
|
||||
public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) {
|
||||
const now = new Date();
|
||||
const indexToUse = `${this.indexPrefix}-${now.getFullYear()}.${(
|
||||
'0' +
|
||||
(now.getMonth() + 1)
|
||||
).slice(-2)}.${('0' + now.getDate()).slice(-2)}`;
|
||||
|
||||
if (this.elasticScheduler.docsScheduled && !scheduleOverwrite) {
|
||||
this.elasticScheduler.scheduleDoc(logPackageArg);
|
||||
return;
|
||||
}
|
||||
|
||||
await this.elasticIndex.ensureIndex(indexToUse);
|
||||
|
||||
this.client.index(
|
||||
{
|
||||
index: indexToUse,
|
||||
type: 'log',
|
||||
body: {
|
||||
'@timestamp': new Date(logPackageArg.timestamp).toISOString(),
|
||||
...logPackageArg
|
||||
}
|
||||
},
|
||||
(error, response) => {
|
||||
if (error) {
|
||||
console.log('ElasticLog encountered an error:');
|
||||
console.log(error);
|
||||
this.elasticScheduler.addFailedDoc(logPackageArg);
|
||||
} else {
|
||||
// console.log(`ElasticLog: ${logPackageArg.message}`);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
get logDestination(): ILogDestination {
|
||||
return {
|
||||
handleLog: (smartlogPackageArg: ILogPackage) => {
|
||||
this.log(smartlogPackageArg);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
8
ts/elasticsearch.plugins.ts
Normal file
8
ts/elasticsearch.plugins.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import * as elasticsearch from 'elasticsearch';
|
||||
import * as lik from '@pushrocks/lik';
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartlogInterfaces from '@pushrocks/smartlog-interfaces';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smarttime from '@pushrocks/smarttime';
|
||||
|
||||
export { elasticsearch, lik, smartdelay, smartlogInterfaces, smartpromise, smarttime };
|
@ -1 +1 @@
|
||||
export * from './elasticlog.classes.elasticlog';
|
||||
export * from './elasticsearch.classes.elasticsearch';
|
||||
|
Reference in New Issue
Block a user