2018-01-24 23:38:07 +00:00
|
|
|
// interfaces
|
2023-07-05 07:38:43 +00:00
|
|
|
import { Client as ElasticClient } from '@elastic/elasticsearch';
|
2023-07-04 07:13:14 +00:00
|
|
|
import type { ILogContext, ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
|
2018-01-27 18:16:05 +00:00
|
|
|
|
|
|
|
// other classes
|
2023-08-17 17:21:26 +00:00
|
|
|
import { ElasticScheduler } from './els.classes.elasticscheduler.js';
|
|
|
|
import { ElasticIndex } from './els.classes.elasticindex.js';
|
2018-01-24 23:38:07 +00:00
|
|
|
|
|
|
|
export interface IStandardLogParams {
|
2018-01-27 18:16:05 +00:00
|
|
|
message: string;
|
|
|
|
severity: string;
|
2018-01-24 23:38:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 00:48:44 +00:00
|
|
|
export interface IElasticSearchConstructorOptions {
|
|
|
|
indexPrefix: string;
|
|
|
|
indexRetention: number;
|
2023-07-05 08:22:53 +00:00
|
|
|
node: string;
|
2023-07-05 15:27:24 +00:00
|
|
|
auth?: {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
};
|
2018-01-24 23:38:07 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 10:24:22 +00:00
|
|
|
export class ElsSmartlogDestination<T> {
|
2018-11-09 22:15:11 +00:00
|
|
|
public client: ElasticClient;
|
|
|
|
public elasticScheduler = new ElasticScheduler(this);
|
|
|
|
public elasticIndex: ElasticIndex = new ElasticIndex(this);
|
2018-01-24 23:38:07 +00:00
|
|
|
|
2018-11-10 00:48:44 +00:00
|
|
|
public indexPrefix: string;
|
|
|
|
public indexRetention: number;
|
|
|
|
|
2018-01-24 23:38:07 +00:00
|
|
|
/**
|
|
|
|
* sets up an instance of Elastic log
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
2018-11-10 00:48:44 +00:00
|
|
|
constructor(optionsArg: IElasticSearchConstructorOptions) {
|
2018-01-24 23:38:07 +00:00
|
|
|
this.client = new ElasticClient({
|
2023-07-05 08:22:53 +00:00
|
|
|
node: optionsArg.node,
|
2023-07-05 15:27:24 +00:00
|
|
|
...(optionsArg.auth && { auth: optionsArg.auth }),
|
2018-01-24 23:38:07 +00:00
|
|
|
});
|
2023-08-01 10:24:22 +00:00
|
|
|
this.indexPrefix = `${optionsArg.indexPrefix}`;
|
2018-11-10 00:48:44 +00:00
|
|
|
this.indexRetention = optionsArg.indexRetention;
|
2018-01-27 18:16:05 +00:00
|
|
|
}
|
2018-01-24 23:38:07 +00:00
|
|
|
|
2018-11-03 22:45:21 +00:00
|
|
|
public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) {
|
2018-01-27 18:16:05 +00:00
|
|
|
const now = new Date();
|
2023-07-05 13:31:00 +00:00
|
|
|
const indexToUse = `${this.indexPrefix}-${now.toISOString().split('T')[0]}`;
|
2018-11-09 22:15:11 +00:00
|
|
|
|
|
|
|
if (this.elasticScheduler.docsScheduled && !scheduleOverwrite) {
|
|
|
|
this.elasticScheduler.scheduleDoc(logPackageArg);
|
2018-01-27 18:16:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-11-09 22:15:11 +00:00
|
|
|
|
2023-08-01 10:24:22 +00:00
|
|
|
// Make sure the index is created with a mapping for dynamic JSON
|
|
|
|
const indexExists = await this.client.indices.exists({ index: indexToUse });
|
|
|
|
if (!indexExists.body) {
|
|
|
|
await this.client.indices.create({
|
|
|
|
index: indexToUse,
|
|
|
|
body: {
|
|
|
|
mappings: {
|
|
|
|
properties: {
|
|
|
|
'@timestamp': {
|
|
|
|
type: 'date',
|
|
|
|
},
|
|
|
|
logPackageArg: {
|
|
|
|
properties: {
|
|
|
|
payload: {
|
|
|
|
type: 'object',
|
|
|
|
dynamic: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-27 18:16:05 +00:00
|
|
|
this.client.index(
|
|
|
|
{
|
2018-11-09 22:15:11 +00:00
|
|
|
index: indexToUse,
|
2018-01-27 18:16:05 +00:00
|
|
|
body: {
|
2018-11-07 10:38:53 +00:00
|
|
|
'@timestamp': new Date(logPackageArg.timestamp).toISOString(),
|
2023-07-04 07:13:14 +00:00
|
|
|
...logPackageArg,
|
|
|
|
},
|
2018-01-27 18:16:05 +00:00
|
|
|
},
|
|
|
|
(error, response) => {
|
|
|
|
if (error) {
|
2018-08-12 14:42:09 +00:00
|
|
|
console.log('ElasticLog encountered an error:');
|
2018-01-27 18:16:05 +00:00
|
|
|
console.log(error);
|
2018-11-09 22:15:11 +00:00
|
|
|
this.elasticScheduler.addFailedDoc(logPackageArg);
|
2018-01-27 18:16:05 +00:00
|
|
|
} else {
|
2018-11-11 01:00:40 +00:00
|
|
|
// console.log(`ElasticLog: ${logPackageArg.message}`);
|
2018-01-27 18:16:05 +00:00
|
|
|
}
|
2018-01-24 23:38:07 +00:00
|
|
|
}
|
2018-01-27 18:16:05 +00:00
|
|
|
);
|
2018-01-24 23:38:07 +00:00
|
|
|
}
|
2018-11-07 10:38:53 +00:00
|
|
|
|
2018-11-25 21:06:25 +00:00
|
|
|
get logDestination(): ILogDestination {
|
2018-11-07 10:38:53 +00:00
|
|
|
return {
|
2023-07-04 07:13:14 +00:00
|
|
|
handleLog: async (smartlogPackageArg: ILogPackage) => {
|
2018-11-07 10:38:53 +00:00
|
|
|
this.log(smartlogPackageArg);
|
2023-07-04 07:13:14 +00:00
|
|
|
},
|
2018-11-25 21:06:25 +00:00
|
|
|
};
|
2018-11-07 10:38:53 +00:00
|
|
|
}
|
2018-01-27 18:16:05 +00:00
|
|
|
}
|