-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathindex.js
183 lines (159 loc) · 5.2 KB
/
index.js
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
'use strict';
const winston = require('winston');
const Transport = require('winston-transport');
const dayjs = require('dayjs');
const defaults = require('lodash.defaults');
const omit = require('lodash.omit');
const { Client } = require('@elastic/elasticsearch');
const defaultTransformer = require('./transformer');
const BulkWriter = require('./bulk_writer');
const mappingTemplate = require('./index-template-mapping.json');
class ElasticsearchTransport extends Transport {
constructor(opts) {
super(opts);
this.name = 'elasticsearch';
this.handleExceptions = opts.handleExceptions || false;
this.handleRejections = opts.handleRejections || false;
this.exitOnError = false;
this.source = null;
this.on('pipe', (source) => {
this.source = source;
});
this.on('error', (err) => {
this.source.pipe(this); // re-pipes readable
});
this.opts = opts || {};
// Set defaults
defaults(opts, {
level: 'info',
index: opts.dataStream ? 'logs-app-default' : null,
indexPrefix: 'logs',
indexSuffixPattern: 'YYYY.MM.DD',
transformer: defaultTransformer,
useTransformer: true,
ensureIndexTemplate: true,
flushInterval: 2000,
waitForActiveShards: 1,
handleExceptions: false,
exitOnError: false,
pipeline: null,
bufferLimit: null,
buffering: true,
healthCheckTimeout: '30s',
healthCheckWaitForStatus: 'yellow',
healthCheckWaitForNodes: '>=1',
dataStream: false,
internalLogger: console.error,
});
// Use given client or create one
if (opts.client) {
this.client = opts.client;
} else {
defaults(opts, {
clientOpts: {
log: [
{
type: 'console',
level: 'error',
}
]
}
});
// Create a new ES client
// http://localhost:9200 is the default of the client already
const copts = { ...this.opts.clientOpts };
this.client = new Client(copts);
}
const bulkWriterOpts = {
index: opts.index,
interval: opts.flushInterval,
waitForActiveShards: opts.waitForActiveShards,
pipeline: opts.pipeline,
ensureIndexTemplate: opts.ensureIndexTemplate,
indexTemplate: opts.indexTemplate || mappingTemplate,
indexPrefix: opts.indexPrefix,
buffering: opts.buffering,
bufferLimit: opts.buffering ? opts.bufferLimit : 0,
healthCheckTimeout: opts.healthCheckTimeout,
healthCheckWaitForStatus: opts.healthCheckWaitForStatus,
healthCheckWaitForNodes: opts.healthCheckWaitForNodes,
dataStream: opts.dataStream,
retryLimit: opts.retryLimit,
internalLogger: opts.internalLogger,
};
this.bulkWriter = new BulkWriter(this, this.client, bulkWriterOpts);
this.bulkWriter.start();
}
async flush() {
await this.bulkWriter.flush();
}
// end() will be called from here: https://github.com/winstonjs/winston/blob/master/lib/winston/logger.js#L328
end(chunk, encoding, callback) {
this.bulkWriter.schedule = () => { };
this.bulkWriter.flush().then(() => {
setImmediate(() => {
super.end(chunk, encoding, callback); // this emits finish event from stream
});
});
}
async log(info, callback) {
const { level, message, timestamp } = info;
const meta = Object.assign({}, omit(info, ['level', 'message']));
setImmediate(() => {
this.emit('logged', level);
});
const logData = {
message,
level,
timestamp,
meta,
};
const entry = this.opts.useTransformer
? await this.opts.transformer(logData)
: info;
let index = this.opts.dataStream
? this.opts.index
: this.getIndexName(this.opts);
if (this.opts.source) {
entry.source = this.opts.source;
}
if (entry.indexInterfix !== undefined) {
index = this.opts.dataStream
? this.getDataStreamName(this.opts, entry.indexInterfix)
: this.getIndexName(this.opts, entry.indexInterfix);
delete entry.indexInterfix;
}
if (this.opts.apm) {
const apm = this.opts.apm.currentTraceIds;
if (apm['transaction.id']) entry.transaction = { id: apm['transaction.id'], ...entry.transaction };
if (apm['trace.id']) entry.trace = { id: apm['trace.id'], ...entry.trace };
if (apm['span.id']) entry.span = { id: apm['span.id'], ...entry.span };
}
this.bulkWriter.append(index, entry);
callback();
}
getIndexName(opts, indexInterfix) {
this.test = 'test';
let indexName = opts.index;
if (indexName === null) {
// eslint-disable-next-line prefer-destructuring
let indexPrefix = opts.indexPrefix;
if (typeof indexPrefix === 'function') {
// eslint-disable-next-line prefer-destructuring
indexPrefix = opts.indexPrefix();
}
const now = dayjs();
const dateString = now.format(opts.indexSuffixPattern);
indexName = indexPrefix
+ (indexInterfix !== undefined ? '-' + indexInterfix : '')
+ '-'
+ dateString;
}
return indexName;
}
}
winston.transports.Elasticsearch = ElasticsearchTransport;
module.exports = {
ElasticsearchTransport,
ElasticsearchTransformer: defaultTransformer
};