-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzipkin-simple.js
186 lines (148 loc) · 4.1 KB
/
zipkin-simple.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"use strict"
var HttpSimpleTransport = require("./transports/http-simple")
var HttpBatchTransport = require("./transports/http-batch")
var TO_MICROSECONDS = 1000
var ID_LENGTH = 16
var ID_DIGITS = "0123456789abcdef"
var DEFAULT_OPTIONS = {
sampling: 0.1,
debug: false,
host: "127.0.0.1",
port: "9411",
path: "/api/v1/spans"
}
function generateTimestamp () {
// use process.hrtime?
return new Date().getTime() * TO_MICROSECONDS
}
function generateId () {
// copied over from zipkin-js
var n = ""
for (var i = 0; i < ID_LENGTH; i++) {
var rand = Math.floor(Math.random() * ID_DIGITS.length)
// avoid leading zeroes
if (rand !== 0 || n.length > 0) {
n += ID_DIGITS[rand]
}
}
return n
}
function zipkinSimple (options) {
this.counter = 0
this.opts = Object.assign({}, DEFAULT_OPTIONS, options)
this.buildOptions()
}
zipkinSimple.prototype.buildOptions = function buildOptions () {
this.send = HttpBatchTransport
if (this.opts.transport === "http-simple") {
this.send = HttpSimpleTransport
}
if (typeof this.opts.transport === "function") {
this.send = this.opts.transport
}
}
zipkinSimple.prototype.shouldSample = function shouldSample () {
this.counter++
if (this.counter * this.opts.sampling >= 1) {
this.counter = 0
return true
}
return false
}
zipkinSimple.prototype.createRootTrace = function createRootTrace () {
var id = generateId()
return {
traceId: id,
spanId: id,
parentSpanId: null,
sampled: this.shouldSample(),
timestamp: generateTimestamp()
}
}
zipkinSimple.prototype.getChild = function getChild (traceData) {
if (!traceData) {
return this.createRootTrace()
}
return {
traceId: traceData.traceId,
parentSpanId: traceData.spanId,
spanId: generateId(),
sampled: traceData.sampled,
timestamp: generateTimestamp()
}
}
zipkinSimple.prototype.sendTrace = function sendTrace (trace, data) {
if (!trace.sampled) {
return
}
var body = {
traceId: trace.traceId,
name: data.name,
id: trace.spanId,
annotations: [],
binaryAnnotations: []
}
if (trace.parentSpanId) {
body.parentId = trace.parentSpanId
}
var time = generateTimestamp()
for (var i = 0; i < data.annotations.length; i++) {
body.annotations.push({
endpoint: {
serviceName: data.service,
ipv4: 0,
port: 0
},
timestamp: time,
value: data.annotations[i]
})
if (data.annotations[i] === "cr" || (data.annotations[i] === "ss" && trace.serverOnly)) {
body.duration = time - trace.timestamp
}
if (data.annotations[i] === "cs" || (data.annotations[i] === "sr" && trace.serverOnly)) {
body.timestamp = trace.timestamp || generateTimestamp()
}
}
this.send(body, this.opts)
}
zipkinSimple.prototype.traceWithAnnotation = function traceWithAnnotation (trace, data, annotation) {
if (!trace) {
trace = this.createRootTrace()
}
if (!trace.sampled) {
return trace
}
data.annotations = data.annotations || []
data.annotations.push(annotation)
this.sendTrace(trace, data)
return trace
}
zipkinSimple.prototype.sendClientSend = function sendClientSend (trace, data) {
return this.traceWithAnnotation(trace, data, "cs")
}
zipkinSimple.prototype.sendClientRecv = function sendClientRecv (trace, data) {
return this.traceWithAnnotation(trace, data, "cr")
}
zipkinSimple.prototype.sendServerSend = function sendServerSend (trace, data) {
return this.traceWithAnnotation(trace, data, "ss")
}
zipkinSimple.prototype.sendServerRecv = function sendServerRecv (trace, data) {
if (!trace) {
trace = this.createRootTrace()
trace.serverOnly = true
}
return this.traceWithAnnotation(trace, data, "sr")
}
zipkinSimple.prototype.options = function setOptions (opts) {
if (opts) {
Object.assign(this.opts, opts)
this.buildOptions()
}
return this.opts
}
zipkinSimple.prototype.get_child = zipkinSimple.prototype.getChild
zipkinSimple.prototype.send_client_send = zipkinSimple.prototype.sendClientSend
zipkinSimple.prototype.send_client_recv = zipkinSimple.prototype.sendClientRecv
zipkinSimple.prototype.send_server_send = zipkinSimple.prototype.sendServerSend
zipkinSimple.prototype.send_server_recv = zipkinSimple.prototype.sendServerRecv
module.exports = zipkinSimple