-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
305 lines (285 loc) · 11.2 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { httpreq } from 'h2tp';
// https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html
interface IBaseField {
analyzer?: string;
boost?: number;
coerce?: boolean;
copy_to?: string;
doc_values?: boolean;
dynamically?: boolean | 'strict';
enabled?: boolean;
format?: string;
ignore_above?: number;
ignore_malformed?: boolean;
index_options?: 'docs' | 'freqs' | 'positions' | 'offsets';
index_prefixes?: {
min_chars: number;
max_chars: number;
};
index?: boolean;
null_value?: any;
search_analyzer?: string;
similarity?: 'BM25' | 'classic' | 'boolean';
store?: boolean;
term_vector?: 'no' | 'yes' | 'with_offsets' | 'with_positions' | 'with_positions_offsets' | 'with_positions_payloads' | 'with_positions_offsets_payloads';
type: "text" | "keyword" | "date" | "long" | "double" | "boolean" | "ip" | "object" | "nested" | "geo_point" | "geo_shape" | "completion";
}
interface IBaseWithFieldsField extends IBaseField {
fields?: {[field: string]: IBaseField};
}
interface IPropertiesField {
properties: MappingProperties;
}
export type IField = IBaseWithFieldsField | IPropertiesField;
export declare type MappingProperties = {
[field: string]: IField;
};
interface ESBulk {
bulk: string;
bulkSize: number;
obsolete: number;
}
interface ESInfo {
name: string;
cluster_name: string;
cluster_uuid: string;
version: {
number: string;
build_flavor: string;
build_type: string;
build_hash: string;
build_date: string;
build_snapshot: boolean;
lucene_version: string;
minimum_wire_compatibility_version: string;
minimum_index_compatibility_version: string;
},
tagline: string;
}
interface ESVersion {
major: number;
minor: number;
patch: number;
};
export interface Config {
/**Elasticsearch host */
esHost: string;
/**Elasticsearch request timeout in milliseconds. Default is `30000`. */
esRequestTimeout_ms?: number;
/**Flush interval in milliseconds. Default is `5000`. */
flushInterval_ms?: number;
/**Interval in seconds at which new index with suffix gets created.
* Suffix is calculated as current timestamp divided by this interval
* at the moment of `log` method call.
* When set to `0` index will not get any suffix.
*
* Default is `3600` (1 hour).*/
indexSplitInterval_sec?: number;
/**Log errors on stderr. Default is `true`. */
logErrors?: boolean;
}
/**
* Elasticsearch bulk logger
*/
export class Logger {
public esHost: string;
public logErrors = true;
public flushInterval = 5000;
public esRequestTimeout = 30000;
public indexSplitInterval = 3600000;
private dying = false;
private autoFlushTID: any = null;
private cleanUpTID: any;
private indexBulks: Map<string, ESBulk> = new Map();
private nonEmptyBulks: Set<string> = new Set();
private esVersion!: ESVersion;
private pendingMessages = 0;
private createIndexPromises: Map<string, Promise<ESBulk>> = new Map();
constructor(config: Config | string) {
if (typeof(config) === 'string') {
this.esHost = config;
} else {
this.esHost = config.esHost;
this.flushInterval = (config.flushInterval_ms !== undefined) ? config.flushInterval_ms : this.flushInterval;
this.esRequestTimeout = (config.esRequestTimeout_ms !== undefined) ? config.esRequestTimeout_ms : this.esRequestTimeout;
this.indexSplitInterval = (config.indexSplitInterval_sec !== undefined) ? 1000 * config.indexSplitInterval_sec : this.indexSplitInterval;
this.logErrors = (config.logErrors !== undefined) ? !!config.logErrors : this.logErrors;
}
this.autoFlush();
this.cleanUpOldIndexBulks();
}
/**Initialize Logger class */
public initialize() {
return httpreq(`http://${this.esHost}`)
.then(r => {
if (r.response.statusCode === 200) {
const info: ESInfo = JSON.parse(r.body);
const rex = /^(\d+)\.(\d+)\.(\d+)/.exec(info.version.number);
if (rex) {
this.esVersion = {
major: +rex[1],
minor: +rex[2],
patch: +rex[3],
}
return Promise.resolve();
}
return Promise.reject(new Error(`Invalid elasticsearch server:\n${JSON.stringify(info, null, 4)}`));
}
return Promise.reject(new Error(`Got server contacting elasticsearch server`));
})
.catch(err => {
this.handleError(err);
return Promise.reject(new Error(`Can't connecto to the server`));
})
}
/**Close logger gracefully. Instance can't be reused after this call. */
public async close(): Promise<void> {
this.dying = true;
clearTimeout(this.autoFlushTID);
clearTimeout(this.cleanUpTID);
const delay = 100;
while (this.pendingMessages > 0) {
await (new Promise(res => setTimeout(() => res(), delay)));
}
return this.flush();
}
/**Flush logs to Elasticsearch */
public flush() {
if (this.nonEmptyBulks.size === 0) {
return Promise.resolve();
} else {
const all: Promise<void>[] = [];
this.nonEmptyBulks.forEach(index => {
const b = this.indexBulks.get(index);
if (b && b.bulkSize > 0) {
all.push(
httpreq({
url: `http://${this.esHost}/_bulk`,
method: 'POST',
payload: b.bulk,
headers: { "Content-Type": "application/x-ndjson" },
timeout: this.esRequestTimeout,
})
.then(r => {
return (r.response.statusCode !== 200 || /"errors":true/.exec(r.body)) ? Promise.reject(new Error(r.body || `Got status ${r.response.statusCode} from elasticsearch`)) : Promise.resolve();
})
);
b.bulk = '';
b.bulkSize = 0;
}
});
this.nonEmptyBulks.clear();
return Promise.all(all).then(() => Promise.resolve());
}
}
/**Log message to elastic */
public log(index: string, message: any, properties?: MappingProperties, type = "doc") {
if (this.dying) return;
if (!this.esVersion) {
this.handleError(new Error(`Logger is not initialized`));
return;
}
index = this.elasticIndexWithSufix(index);
const b = this.indexBulks.get(index);
if (!b) {
this.pendingMessages++;
this.createIndex(index, type, properties)
.then((b2) => {
this.addToBulk(index, b2, message, type);
this.pendingMessages--;
})
.catch(e => {
this.handleError(e);
this.pendingMessages--;
})
} else {
this.addToBulk(index, b, message, type);
}
}
private addToBulk(index: string, b: ESBulk, message: any, type: string) {
this.nonEmptyBulks.add(index);
b.bulk += (this.esVersion.major >= 7)
? `{"index":{"_index":"${index}"}}\n`
: `{"index":{"_index":"${index}","_type":"${type}"}}\n`;
b.bulk += JSON.stringify(message) + '\n';
b.bulkSize++;
}
private elasticIndexWithSufix(baseIndex: string) {
return this.indexSplitInterval === 0 ? baseIndex : `${baseIndex}-${Math.floor(Date.now() / this.indexSplitInterval)}`;
}
private autoFlush() {
if (this.autoFlushTID === null) {
this.autoFlushTID = setInterval(() => {
this.flush().catch(err => this.handleError(err));
this.autoFlush();
}, this.flushInterval);
}
}
private createIndex(index: string, type: string, properties?: MappingProperties): Promise<ESBulk> {
if (this.createIndexPromises.has(index)) {
return this.createIndexPromises.get(index)!;
}
const
indexUrl = `http://${this.esHost}/${index}`,
mappingUrl = (this.esVersion.major >= 7)
? `${indexUrl}/_mapping`
: `${indexUrl}/_mapping/${type}`;
const resolveESBulk = (obsolete?: number): Promise<ESBulk> => {
const b: ESBulk = { bulk: '', bulkSize: 0, obsolete: obsolete !== undefined ? obsolete : Date.now() + 2 * this.indexSplitInterval };
this.indexBulks.set(index, b);
return Promise.resolve(b);
};
// check if type exists
const promise = !properties
? resolveESBulk()
: httpreq({
url: mappingUrl,
method: 'GET',
timeout: this.esRequestTimeout,
})
.then((r) => {
if (r.response.statusCode === 200) {
// index exists
return resolveESBulk(this.indexSplitInterval > 0 ? +/\d+$/.exec(index)![0] * this.indexSplitInterval : undefined);
} else if (r.response.statusCode === 404) {
// index doesn't exist, create it
const propsJson = JSON.stringify(properties);
const payload = (this.esVersion.major >= 7)
? `{"mappings":{"properties":${propsJson}}}`
: `{"mappings":{"${type}":{"properties":${propsJson}}}}`;
return httpreq({
url: indexUrl,
method: 'PUT',
payload,
headers: { "Content-Type": "application/json" },
timeout: this.esRequestTimeout,
})
.then(r => (r.response.statusCode === 200) ? resolveESBulk() : Promise.reject(new Error("Can't create indice")))
} else {
// unknown error while checking existence
return Promise.reject(new Error("Can't create indice"));
}
})
promise
.catch(() => Promise.resolve())
.then(() => this.createIndexPromises.delete(index))
this.createIndexPromises.set(index, promise);
return promise;
}
private cleanUpOldIndexBulks() {
if (this.indexSplitInterval > 0) {
this.cleanUpTID = setTimeout(() => {
this.cleanUpOldIndexBulks();
}, this.indexSplitInterval);
const mintime = Date.now() - this.indexSplitInterval;
for (const entries of this.indexBulks.entries()) {
if (entries[1].obsolete < mintime) {
this.indexBulks.delete(entries[0]);
}
}
}
}
private handleError(e: Error) {
if (this.logErrors) console.error(`elastic-log: ${e.message}\n${e.stack}`);
}
}