-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
315 lines (291 loc) · 9.59 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
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
306
307
308
309
310
311
312
313
314
315
/*
* moleculer
* Copyright (c) 2019 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/
/**
* @typedef {Object} ClickHouseLoggerOptions
* @property {string} [url='http://localhost'] Url to your CH server like http://localhost
* @property {number} [port=8123] Port to connect to server. Default is 8123
* @property {string} [dbName='default'] Database name, default is 'default'
* @property {string} [dbUser='default'] Database user, default is 'default'
* @property {string} [dbPassword=''] Database user password, default is empty string
* @property {string} [dbTableName='logs'] Database table, default is 'logs'
* @property {string} [source='moleculer'] Use source like single TAG, its default is 'moleculer' or env MOL_NODE_NAME
* @property {string} [hostname='hostname'] Hostname, default is machine hostname 'os.hostname()'
* @property {Function} [objectPrinter] Callback function for object printer, default is 'util.inspect()'
* @property {number} [interval=10000] Date uploading interval in milliseconds, default is 10000
* @property {string} [timeZone='Europe/Istanbul'] Time zone for save in database, default is 'Europe/Istanbul'
* @property {string} [tableTTL='date + INTERVAL 1 DAY RECOMPRESS CODEC(ZSTD(3))'] TTL for table in ClickHouse, default is Recompress after 1 day with CODEC(ZSTD(3))
* @property {boolean} [useBuffer=false] Use buffer table for reading and writing data, default false
*/
'use strict'
require('isomorphic-fetch')
const _ = require('lodash')
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
const BaseLogger = require('moleculer').Loggers.Base
const { hostname } = require('os')
dayjs.extend(utc)
dayjs.extend(timezone)
fetch.Promise = Promise
const isObject = (o) => o !== null && typeof o === 'object' && !(o instanceof String)
/**
* ClickHouseLogger logger for Moleculer
*
* @class ClickHouseLogger
* @constructor
* @extends {BaseLogger}
*/
class ClickHouseLogger extends BaseLogger {
/**
* Creates an instance of ClickHouseLogger.
* @param {ClickHouseLoggerOptions} opts
* @memberof ClickHouseLogger
*/
constructor(opts = {}) {
super(opts)
/**
* @type {ClickHouseLoggerOptions}
*/
const defaultOptions = {
url: 'http://localhost',
port: 8123,
dbName: 'default',
dbUser: 'default',
dbPassword: '',
dbTableName: 'logs',
// use source like TAG
source: process.env.MOL_NODE_NAME || 'moleculer',
hostname: hostname(),
objectPrinter: null,
interval: 10 * 1000,
timeZone: 'Europe/Istanbul',
tableTTL: 'date + INTERVAL 1 DAY RECOMPRESS CODEC(ZSTD(3))',
useBuffer: false,
}
this.opts = _.defaultsDeep(this.opts, defaultOptions)
this.queue = []
this.timer = null
this.host = `${this.opts.url}:${this.opts.port}`
}
/**
* Initialize logger.
*
* @param {LoggerFactory} loggerFactory
*/
init(loggerFactory) {
super.init(loggerFactory)
try {
dayjs.extend(utc)
dayjs.extend(timezone)
} catch (e) {
throw new Error(
"The 'dayjs' package is missing! Please install it with 'npm install dayjs --save' command."
)
}
this.createDB()
.then((res) => {
console.info(`Database ${this.opts.dbName} created successfully`, res)
})
.then(() => {
this.createTable()
.then((res) => {
console.info(
`Table ${this.opts.dbTableName} in db ${this.opts.dbName} created successfully`,
res
)
})
.then(() => {
if (!this.opts.useBuffer) return
this.createBufferTable()
.then((res) => {
console.info(`Buffer table ${this.opts.dbName}_buffer created successfully`, res)
})
.catch((err) => {
/* istanbul ignore next */
// eslint-disable-next-line no-console
console.warn(
`Unable to create table ${this.opts.dbTableName}_buffer in database ${this.opts.dbName}. Error:${err.message}`,
err
)
})
})
.catch((err) => {
/* istanbul ignore next */
// eslint-disable-next-line no-console
console.warn(
`Unable to create table ${this.opts.dbTableName} in database ${this.opts.dbName}. Error:${err.message}`,
err
)
})
})
.catch((err) => {
/* istanbul ignore next */
// eslint-disable-next-line no-console
console.warn(`Unable to create database ${this.opts.dbName}. Error:${err.message}`, err)
})
this.objectPrinter = this.opts.objectPrinter
? this.opts.objectPrinter
: (o) => JSON.stringify(o)
if (this.opts.interval > 0) {
this.timer = setInterval(() => this.flush(), this.opts.interval)
this.timer.unref()
}
}
/**
* Stopping logger
*/
stop() {
if (this.timer) {
clearInterval(this.timer)
this.timer = null
}
return this.flush()
}
/**
* Generate a new log handler.
*
* @param {object} bindings
*/
getLogHandler(bindings) {
const level = bindings ? this.getLogLevel(bindings.mod) : null
if (!level) return null
let requestID = ''
let subdomain = ''
let caller = ''
const printArgs = (args) => {
return args.map((p) => {
if (isObject(p) && p.requestID !== undefined) {
requestID = p.requestID
subdomain = p.subdomain
caller = p.caller
delete p.span
return this.objectPrinter(p)
}
if (isObject(p) || Array.isArray(p)) return this.objectPrinter(p)
return p
})
}
const levelIdx = BaseLogger.LEVELS.indexOf(level)
return (type, args) => {
const typeIdx = BaseLogger.LEVELS.indexOf(type)
if (typeIdx > levelIdx) return
this.queue.push({
ts: Date.now(),
level: type,
msg: printArgs(args).join(' '),
requestID,
subdomain,
caller,
bindings,
})
requestID = ''
subdomain = ''
caller = ''
if (!this.opts.interval) this.flush()
}
}
/**
* Flush queued log entries to ClickHouseLogger.
*/
flush() {
if (this.queue.length > 0) {
const rows = Array.from(this.queue)
this.queue.length = 0
const data = rows
.map((row) =>
JSON.stringify({
timestamp: row.ts,
requestID: row.requestID,
subdomain: row.subdomain,
caller: row.caller,
date: dayjs(row.ts).tz(this.opts.timeZone).format('YYYY-MM-DD'),
level: row.level,
message: row.msg,
nodeID: row.bindings.nodeID,
namespace: row.bindings.ns,
service: row.bindings.svc,
version: row.bindings.ver ? String(row.bindings.ver) : '',
source: this.opts.source,
hostname: this.opts.hostname,
})
)
.join('\n')
return fetch(this.host, {
method: 'POST',
body: `INSERT INTO ${this.opts.useBuffer ? `${this.opts.dbTableName}_buffer` : this.opts.dbTableName} FORMAT JSONEachRow ${data}`,
headers: {
'X-ClickHouse-Database': this.opts.dbName,
'X-ClickHouse-User': this.opts.dbUser,
'X-ClickHouse-Key': this.opts.dbPassword,
},
})
.then((/* res */) => {
// console.info("Logs are uploaded to ClickHouse. Status: ", res.statusText);
})
.catch((err) => {
// eslint-disable-next-line no-console
console.warn(`Unable to upload logs to ClickHouse server. Error:${err.message}`, err)
})
}
return this.broker.Promise.resolve()
}
createDB() {
return fetch(this.host, {
method: 'POST',
body: `CREATE DATABASE IF NOT EXISTS ${this.opts.dbName}`,
headers: {
'X-ClickHouse-User': this.opts.dbUser,
'X-ClickHouse-Key': this.opts.dbPassword,
},
})
}
createTable() {
const body = `CREATE TABLE IF NOT EXISTS ${this.opts.dbTableName} (
timestamp DateTime64(3, '${this.opts.timeZone}') DEFAULT now(),
requestID String,
subdomain String,
caller String,
level String,
message String,
nodeID String,
namespace String,
service String,
version String,
source String,
hostname String,
date Date DEFAULT today())
ENGINE = MergeTree()
ORDER BY (toStartOfHour(timestamp), service, level, subdomain, requestID, timestamp)
PRIMARY KEY (toStartOfHour(timestamp), service, level, subdomain, requestID)
PARTITION BY (date, toStartOfDay(timestamp))
TTL ${this.opts.tableTTL}
SETTINGS index_granularity = 8192;`
return fetch(this.host, {
method: 'POST',
body,
headers: {
'X-ClickHouse-Database': this.opts.dbName,
'X-ClickHouse-User': this.opts.dbUser,
'X-ClickHouse-Key': this.opts.dbPassword,
},
})
}
createBufferTable() {
const body = `CREATE TABLE IF NOT EXISTS ${this.opts.dbTableName}_buffer
as ${this.opts.dbTableName}
ENGINE = Buffer('${this.opts.dbName}', '${this.opts.dbTableName}', 16, 10, 100, 1000, 10000, 10000, 100000);`
return fetch(this.host, {
method: 'POST',
body,
headers: {
'X-ClickHouse-Database': this.opts.dbName,
'X-ClickHouse-User': this.opts.dbUser,
'X-ClickHouse-Key': this.opts.dbPassword,
},
})
}
}
module.exports = ClickHouseLogger