-
Notifications
You must be signed in to change notification settings - Fork 1
/
praven.js
163 lines (140 loc) · 3.46 KB
/
praven.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
'use strict'
const nopt = require('nopt')
const path = require('path')
const pump = require('pump')
const split2 = require('split2')
const parseJson = require('fast-json-parse')
const through2 = require('through2')
const Raven = require('raven')
const longOpts = {
config: String,
dsn: String
}
const shortOpts = {
c: '--config'
}
let options = {
dsn: null,
logger: 'node',
name: null,
release: null,
environment: process.env.NODE_ENV || 'development',
tags: {},
extra: {},
sampleRate: 1.0,
sendTimeout: 1,
includeModules: true
}
const args = nopt(longOpts, shortOpts, process.argv)
options = Object.assign(options, args)
if (options.version) {
console.log('pino-raven', require('./package.json').version)
process.exit(0)
}
if (options.config) {
try {
const loadedConfig = require(path.resolve(options.config))
options = Object.assign(options, loadedConfig)
} catch (e) {
console.error('`config` parameter specified but could not load file: %s', e.message)
process.exit(1)
}
}
function levelToSeverity (level) {
let result
switch (level) {
case 10: // pino: trace
result = 'debug'
break
case 20: // pino: debug
result = 'debug'
break
case 30: // pino: info
result = 'info'
break
case 40: // pino: warn
result = 'warning'
break
case 50: // pino: error
result = 'error'
break
default:
case 60: // pino: fatal
result = 'fatal'
break
}
return result
}
function install (data) {
Raven.config(options.dsn, {
logger: options.logger,
environment: options.environment,
release: options.release,
tags: options.tags,
extra: options.extra,
sampleRate: options.sampleRate,
sendTimeout: options.sendTimeout,
dataCallback: (dataOrigin) => {
delete dataOrigin.modules
if (options.includeModules && data.modules) {
dataOrigin.modules = data.modules
}
return dataOrigin
}
}).install()
}
function myTransport () {
let isInstalled = false
return through2.obj(function transport (data, enc, cb) {
!isInstalled && install(data)
isInstalled = true
const severity = levelToSeverity(data.level)
const tags = data.tags || {}
if (data.reqId) {
tags.uuid = data.reqId
}
const request = data.req || null
const response = data.res || null
if (response && response.routePath) {
tags.route = response.routePath
}
const extra = data.extra || {}
if (data.responseTime) {
extra.responseTime = data.responseTime
}
const user = data.user || {}
let ravenMethod = Raven.captureMessage.bind(Raven)
let message = data.msg
if (data.err && data.err.stack) {
const error = new global[data['err']['type']](data.err.message)
error.stack = data.err.stack
ravenMethod = Raven.captureException.bind(Raven)
message = error
}
if (request && response) {
Raven.captureBreadcrumb({
type: 'http',
category: 'http',
data: {
method: request.method,
url: request.url,
status_code: response.statusCode
}
})
}
setImmediate(() => ravenMethod(message, {
level: severity,
tags,
extra,
request,
user
}, cb))
})
}
function parser (str) {
const result = parseJson(str)
if (result.err) return
return result.value
}
pump(process.stdin, split2(parser), myTransport())
process.on('SIGINT', () => { process.exit(0) })