-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
92 lines (81 loc) · 2.88 KB
/
app.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
'use strict';
const OpenTracing = require('./lib/opentracing');
module.exports = app => {
app.opentracing = new OpenTracing(app);
const config = app.config.opentracing;
for (const key of Object.keys(config.carrier)) {
const carrier = config.carrier[key];
if (carrier === false) continue;
app.opentracing.setCarrier(key, carrier);
}
for (const key of Object.keys(config.collector)) {
const collector = config.collector[key];
if (collector === false) continue;
app.opentracing.setCollector(key, collector);
}
logHTTPClient(app);
logHTTPServer(app);
};
function logHTTPServer(app) {
const httpServerSpan = Symbol('Context#httpServerSpan');
app.on('request', ctx => {
const spanContext = ctx.tracer.extract('HTTP', ctx.header);
const span = ctx.tracer.startSpan('http_server', { childOf: spanContext });
span.setTag('span.kind', 'server');
ctx[httpServerSpan] = span;
});
app.on('response', ctx => {
const socket = ctx.req.connection;
const span = ctx[httpServerSpan];
// TODO: what's the service name of the remote server
// span.setTag('peer.service');
span.setTag('peer.port', socket.remotePort);
/* istanbul ignore if */
if (socket.remoteFamily === 'IPv4') {
span.setTag('peer.ipv4', socket.remoteAddress);
} else if (socket.remoteFamily === 'IPv6') {
span.setTag('peer.ipv6', socket.remoteAddress);
}
span.setTag('http.url', ctx.path);
span.setTag('http.method', ctx.method);
span.setTag('http.status_code', ctx.realStatus);
span.setTag('http.request_size', ctx.get('content-length') || 0);
span.setTag('http.response_size', ctx.length || 0);
span.finish();
});
}
function logHTTPClient(app) {
const _span = Symbol.for('Request#span');
app.httpclient.on('request', req => {
let ctx = req.ctx;
if (!ctx) {
ctx = app.createAnonymousContext();
req.ctx = ctx;
}
const args = req.args;
if (!args.headers) args.headers = {};
const span = ctx.tracer.startSpan('http_client');
span.setTag('span.kind', 'client');
ctx.tracer.inject(span.context(), 'HTTP', args.headers);
req[_span] = span;
});
app.httpclient.on('response', ({ req, res }) => {
const span = req[_span];
const address = req.socket.address();
span.setTag('peer.hostname', req.options.host);
span.setTag('peer.port', req.options.port);
span.setTag('peer.service', req.options.host);
/* istanbul ignore else */
if (address.family === 'IPv4') {
span.setTag('peer.ipv4', address.address);
} else if (address.family === 'IPv6') {
span.setTag('peer.ipv6', address.address);
}
span.setTag('http.url', req.url);
span.setTag('http.method', req.options.method);
span.setTag('http.status_code', res.status);
span.setTag('http.request_size', req.size || 0);
span.setTag('http.response_size', res.size || 0);
span.finish();
});
}