2023-07-05 09:38:43 +02:00
|
|
|
import { Client as ElasticClient } from '@elastic/elasticsearch';
|
2025-11-29 09:48:31 +00:00
|
|
|
import type {
|
|
|
|
|
ILogContext,
|
|
|
|
|
ILogPackage,
|
|
|
|
|
ILogDestination,
|
|
|
|
|
} from '@push.rocks/smartlog-interfaces';
|
2023-08-17 19:21:26 +02:00
|
|
|
import { ElasticScheduler } from './els.classes.elasticscheduler.js';
|
|
|
|
|
import { ElasticIndex } from './els.classes.elasticindex.js';
|
2018-01-25 00:38:07 +01:00
|
|
|
|
|
|
|
|
export interface IStandardLogParams {
|
2018-01-27 19:16:05 +01:00
|
|
|
message: string;
|
|
|
|
|
severity: string;
|
2018-01-25 00:38:07 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-10 01:48:44 +01:00
|
|
|
export interface IElasticSearchConstructorOptions {
|
|
|
|
|
indexPrefix: string;
|
|
|
|
|
indexRetention: number;
|
2023-07-05 10:22:53 +02:00
|
|
|
node: string;
|
2023-07-05 17:27:24 +02:00
|
|
|
auth?: {
|
|
|
|
|
username: string;
|
|
|
|
|
password: string;
|
|
|
|
|
};
|
2018-01-25 00:38:07 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-01 12:24:22 +02:00
|
|
|
export class ElsSmartlogDestination<T> {
|
2018-11-09 23:15:11 +01:00
|
|
|
public client: ElasticClient;
|
|
|
|
|
public elasticScheduler = new ElasticScheduler(this);
|
|
|
|
|
public elasticIndex: ElasticIndex = new ElasticIndex(this);
|
2018-01-25 00:38:07 +01:00
|
|
|
|
2018-11-10 01:48:44 +01:00
|
|
|
public indexPrefix: string;
|
|
|
|
|
public indexRetention: number;
|
|
|
|
|
|
|
|
|
|
constructor(optionsArg: IElasticSearchConstructorOptions) {
|
2018-01-25 00:38:07 +01:00
|
|
|
this.client = new ElasticClient({
|
2023-07-05 10:22:53 +02:00
|
|
|
node: optionsArg.node,
|
2023-07-05 17:27:24 +02:00
|
|
|
...(optionsArg.auth && { auth: optionsArg.auth }),
|
2018-01-25 00:38:07 +01:00
|
|
|
});
|
2023-08-01 12:24:22 +02:00
|
|
|
this.indexPrefix = `${optionsArg.indexPrefix}`;
|
2018-11-10 01:48:44 +01:00
|
|
|
this.indexRetention = optionsArg.indexRetention;
|
2023-08-29 11:11:25 +02:00
|
|
|
this.setupDataStream();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async setupDataStream() {
|
|
|
|
|
// Define an index template
|
|
|
|
|
await this.client.indices.putIndexTemplate({
|
|
|
|
|
name: `${this.indexPrefix}_template`,
|
|
|
|
|
index_patterns: [`${this.indexPrefix}-*`],
|
|
|
|
|
data_stream: {},
|
|
|
|
|
});
|
2018-01-27 19:16:05 +01:00
|
|
|
}
|
2018-01-25 00:38:07 +01:00
|
|
|
|
2018-11-03 23:45:21 +01:00
|
|
|
public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) {
|
2018-01-27 19:16:05 +01:00
|
|
|
const now = new Date();
|
2023-08-29 11:11:25 +02:00
|
|
|
const indexToUse = `${this.indexPrefix}-data-stream`; // Use data stream name
|
2018-11-09 23:15:11 +01:00
|
|
|
|
|
|
|
|
if (this.elasticScheduler.docsScheduled && !scheduleOverwrite) {
|
|
|
|
|
this.elasticScheduler.scheduleDoc(logPackageArg);
|
2018-01-27 19:16:05 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2018-11-09 23:15:11 +01:00
|
|
|
|
2025-11-29 09:48:31 +00:00
|
|
|
await this.client.index({
|
|
|
|
|
index: indexToUse,
|
|
|
|
|
body: {
|
|
|
|
|
'@timestamp': new Date(logPackageArg.timestamp).toISOString(),
|
|
|
|
|
...logPackageArg,
|
|
|
|
|
},
|
|
|
|
|
});
|
2018-01-25 00:38:07 +01:00
|
|
|
}
|
2018-11-07 11:38:53 +01:00
|
|
|
|
2018-11-25 22:06:25 +01:00
|
|
|
get logDestination(): ILogDestination {
|
2018-11-07 11:38:53 +01:00
|
|
|
return {
|
2023-07-04 09:13:14 +02:00
|
|
|
handleLog: async (smartlogPackageArg: ILogPackage) => {
|
2023-08-29 11:43:07 +02:00
|
|
|
await this.log(smartlogPackageArg);
|
2023-07-04 09:13:14 +02:00
|
|
|
},
|
2018-11-25 22:06:25 +01:00
|
|
|
};
|
2018-11-07 11:38:53 +01:00
|
|
|
}
|
2018-01-27 19:16:05 +01:00
|
|
|
}
|