-
Notifications
You must be signed in to change notification settings - Fork 9
/
zipkin-tracer.js
123 lines (92 loc) · 2.65 KB
/
zipkin-tracer.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
// FIX: this uses an injected msg property __tracer__ to encode the
// zipkin tracking ids. This is incorrect, the message meta data should be used
// instead. However, it needs to be verified that Seeneca will maintain the meta
// context properly (which it should, but this needs to be tested).
var Zipkin = require('zipkin-simple')
var Tracer = new Zipkin({
host: '127.0.0.1',
port: 9411
})
function internal_action (msg) {
return msg.role === 'seneca' ||
msg.role === 'transport' ||
msg.role === 'options' ||
msg.role === 'mesh' ||
msg.init
}
function get_pattern(msg, meta) {
return meta ? meta.pattern : (msg.meta$ ? msg.meta$.pattern : null)
}
function get_plugin(msg, meta) {
return meta ? meta.plugin_name : (msg.meta$ ? msg.meta$.plugin_name : null)
}
function client_inward (ctx, msg, meta) {
var service = ctx.seneca.options().tag
var pin = get_pattern(msg, meta)
var trace_data = Tracer.get_child(ctx.seneca.fixedargs.__tracer__)
trace_data = Tracer.send_client_send(trace_data, {
service: service,
name: pin
})
ctx.__tracer__ = msg.__tracer__ = ctx.seneca.fixedargs.__tracer__ = trace_data
}
function server_inward (ctx, msg, meta) {
var service = ctx.seneca.options().tag
var pin = get_pattern(msg, meta)
var trace_data = Tracer.send_server_recv(msg.__tracer__, {
service: service,
name: pin
})
ctx.__tracer__ = msg.__tracer__ = ctx.seneca.fixedargs.__tracer__ = trace_data
}
function client_outward (ctx, msg, meta) {
var service = ctx.seneca.options().tag
var pin = get_pattern(msg, meta)
var trace_data = ctx.__tracer__
Tracer.send_client_recv(trace_data, {
service: service,
name: pin
})
}
function server_outward (ctx, msg, meta) {
var service = ctx.seneca.options().tag
var pin = get_pattern(msg, meta)
var trace_data = ctx.__tracer__
Tracer.send_server_send(trace_data, {
service: service,
name: pin
})
}
function zipkin_inward (ctx, data) {
if (internal_action(data.msg)) {
return
}
var msg = data.msg
var meta = data.meta
ctx.server = msg.transport$ && get_plugin(msg, meta) !== 'client$'
if (ctx.server) {
return server_inward(ctx, msg, meta)
}
client_inward(ctx, msg, meta)
}
function zipkin_outward (ctx, data) {
if (internal_action(data.msg)) {
return
}
if (!ctx.__tracer__) {
return
}
var msg = data.msg
var meta = data.meta
if (ctx.server) {
return server_outward(ctx, msg, meta)
}
client_outward(ctx, msg, meta)
}
function tracer_plugin (options) {
Tracer.options(options)
var seneca = this
seneca.inward(zipkin_inward)
seneca.outward(zipkin_outward)
}
module.exports = tracer_plugin