-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (86 loc) · 3.43 KB
/
Copy pathindex.js
File metadata and controls
107 lines (86 loc) · 3.43 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
const initTracer = require('jaeger-client').initTracer;
const request = require('request');
const opentracing = require('opentracing');
const http = require('http');
const https = require('https');
const shimmer = require('shimmer');
module.exports = function(app) {
var serviceName = JSON.parse(require('fs').readFileSync("package.json")).name;
var config = {
'serviceName': serviceName
};
var options = {
'tags': {
'version': '1.1.2'
}
};
console.log("Initing tracer ", serviceName);
var tracer = initTracer(config, options);
var createNamespace = require('cls-hooked').createNamespace;
var getNamespace = require('cls-hooked').getNamespace;
var tracer_context = createNamespace('tracer context');
app.locals.traceSpan = (name, callback) => {
tracer_context.run(() => {
tracer_context.set('tracer', tracer);
var span = tracer.startSpan(name);
span.setTag(opentracing.Tags.SAMPLING_PRIORITY, 1);
tracer_context.set('span', span);
callback(span);
});
}
app.use((req, res, next) => {
tracer_context.run(() => {
tracer_context.set('tracer', tracer);
// Extract upstream Span context from the request if present
var span_ctx = tracer.extract(opentracing.FORMAT_HTTP_HEADERS, req.headers);
var opts = span_ctx ? {childOf: span_ctx} : {}
var span = tracer.startSpan(`${req.method} ${req.path}`, opts);
span.setTag('method', req.method);
span.setTag('path', req.path);
span.setTag('query', req.query);
if (req.body) {
span.setTag('body', req.body);
}
console.log("Started express span");
tracer_context.set('span', span)
span.setTag(opentracing.Tags.SAMPLING_PRIORITY, 1);
span.log({"path": req.path});
res.on('finish', function(){
console.log("Requst finished, finishing span");
span.finish()
});
next();
});
})
// Patch outbound calls
shimmer.wrap(http, 'request', function (original) {
return function (options, callback) {
var tctx = getNamespace("tracer context");
var tracer = tctx.get('tracer');
if (!tracer) {
//console.log("No tracer found");
return original(options, callback);
}
const span = tracer.startSpan(`${options.method} ${options.href}${options.path}`, { childOf: tracer_context.get('span') });
span.setTag('http_opts', {host: options.host, path: options.path});
// Send span context with the outgoing request
tracer.inject(span, opentracing.FORMAT_HTTP_HEADERS, options.headers);
var req = original(options, (res) => {
res.on('end', () => {
span.log({'event': 'body_received'});
span.finish()
});
if (callback) {
callback(res);
}
});
req.on('error', (err) => {
console.log("error event");
span.setTag(opentracing.Tags.ERROR, true);
span.log({'event': 'error', 'error.object': err, 'message': err.message});
span.finish();
});
return req;
}
});
}