-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathapp.js
More file actions
248 lines (202 loc) · 6.22 KB
/
Copy pathapp.js
File metadata and controls
248 lines (202 loc) · 6.22 KB
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
/*
* (c) Copyright IBM Corp. 2021
* (c) Copyright Instana Inc. and contributors 2018
*/
/* eslint-disable no-console */
'use strict';
// NOTE: c8 bug https://github.com/bcoe/c8/issues/166
process.on('SIGTERM', () => {
process.disconnect();
process.exit(0);
});
require('./mockVersion');
if (!process.env.NODE_OPTIONS || !process.env.NODE_OPTIONS.includes('src/immediate')) {
require('../../../..')();
}
const fetch = require('node-fetch-v2');
const bodyParser = require('body-parser');
const express = require('express');
const morgan = require('morgan');
const pino = require('pino');
const port = require('../../../test_util/app-port')();
const agentPort = process.env.INSTANA_AGENT_PORT;
const pinoOptions = {
customLevels: {
customInfo: 31,
customError: 51
}
};
// NOTE: This is the same pino instance as in the uninstrumentedLogger.js!
// We already test against multiple pino versions. If we test with pino-v8,
// this loads a different npm pino version into the cache.
const plainVanillaPino = pino(pinoOptions);
let expressPino;
// requiring pino-http creates a false positive test, because this pino version is not in the node cache yet.
if (process.env.PINO_EXPRESS === 'true') {
expressPino = require('pino-http')(pinoOptions);
}
const app = express();
const logPrefix = `Pino App with pino-http: ${process.env.PINO_EXPRESS} (${process.pid}):\t`;
let secondPinoVersion;
if (process.env.PINO_SECOND_VERSION === 'true') {
const pinoSecondVersion = require('./lib/load-pino');
secondPinoVersion = pinoSecondVersion(pinoOptions);
log('Loaded second pino version.');
}
let secondPinoInstance;
if (process.env.PINO_SECOND_INSTANCE === 'true') {
const secondRequire = require('pino');
secondPinoInstance = secondRequire(pinoOptions);
log('Loaded second pino instance');
}
if (process.env.WITH_STDOUT) {
app.use(morgan(`${logPrefix}:method :url :status`));
}
if (process.env.PINO_EXPRESS === 'true') {
app.use(expressPino);
}
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.sendStatus(200);
});
app.get('/info', (req, res) => {
plainVanillaPino.info('Info message - must not be traced.');
finish(res);
});
app.get('/warn', (req, res) => {
plainVanillaPino.warn('Warn message - should be traced.');
finish(res);
});
app.get('/error', (req, res) => {
plainVanillaPino.error('Error message - should be traced.');
if (process.env.PINO_SECOND_VERSION === 'true') {
secondPinoVersion.error('Error from second pino version - should be traced.');
}
if (process.env.PINO_SECOND_INSTANCE === 'true') {
secondPinoInstance.error('Error from second pino instance - should be traced.');
}
finish(res);
});
app.get('/fatal', (req, res) => {
plainVanillaPino.fatal('Fatal message - should be traced.');
finish(res);
});
app.get('/custom-info', (req, res) => {
plainVanillaPino.customInfo('Custom info level message - should not be traced.');
finish(res);
});
app.get('/custom-error', (req, res) => {
plainVanillaPino.customError('Custom error level message - should be traced.');
finish(res);
});
app.get('/error-object-only', (req, res) => {
plainVanillaPino.error(new Error('This is an error.'));
finish(res);
});
app.get('/error-random-object-only', (req, res) => {
plainVanillaPino.error({
payload: {
statusCode: 404,
error: 'Not Found',
very: {
deeply: {
nested: {
data: 'that we will not serialize'
}
}
}
}
});
finish(res);
});
app.get('/error-object-and-string', (req, res) => {
plainVanillaPino.error(new Error('This is an error.'), 'Error message - should be traced.');
finish(res);
});
app.get('/error-random-object-and-string', (req, res) => {
plainVanillaPino.error({ foo: { bar: 'baz' } }, 'Error message - should be traced.');
finish(res);
});
app.get('/child-error', (req, res) => {
const child = plainVanillaPino.child({ a: 'property' });
child.error('Child logger error message - should be traced.');
finish(res);
});
app.get('/express-pino-info', (req, res) => {
req.log.info('Info message - must not be traced.');
finish(res);
});
app.get('/express-pino-warn', (req, res) => {
req.log.warn('Warn message - should be traced.');
finish(res);
});
app.get('/express-pino-error', (req, res) => {
req.log.error('Error message - should be traced.');
finish(res);
});
app.get('/express-pino-fatal', (req, res) => {
req.log.fatal('Fatal message - should be traced.');
finish(res);
});
app.get('/express-pino-custom-info', (req, res) => {
req.log.customInfo('Custom info level message - should not be traced.');
finish(res);
});
app.get('/express-pino-custom-error', (req, res) => {
req.log.customError('Custom error level message - should be traced.');
finish(res);
});
app.get('/express-pino-error-object-only', (req, res) => {
req.log.error(new Error('This is an error.'));
finish(res);
});
app.get('/express-pino-error-random-object-only', (req, res) => {
req.log.error({
payload: {
statusCode: 404,
error: 'Not Found',
very: {
deeply: {
nested: {
data: 'that we will not serialize'
}
}
}
}
});
finish(res);
});
app.get('/express-pino-error-object-and-string', (req, res) => {
req.log.error(new Error('This is an error.'), 'Error message - should be traced.');
finish(res);
});
app.get('/express-pino-error-random-object-and-string', (req, res) => {
req.log.error({ foo: { bar: 'baz' } }, 'Error message - should be traced.');
finish(res);
});
app.get('/thread-stream-test', (req, res) => {
try {
const mode = process.env.PINO_WORKER_MODE || 'transport';
const logger =
mode === 'transport'
? pino({ transport: { target: 'pino-pretty', options: { destination: 1 } } })
: pino({ destination: 1 });
logger.error('Pino worker test error log');
res.sendStatus(200);
} catch (e) {
res.status(500).send(`Failed: ${e.message}`);
}
});
function finish(res) {
fetch(`http://127.0.0.1:${agentPort}`).then(() => {
res.sendStatus(200);
});
}
app.listen(port, () => {
log(`Listening on port: ${port}`);
});
function log() {
const args = Array.prototype.slice.call(arguments);
args[0] = logPrefix + args[0];
console.log.apply(console, args);
}