-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh2z.js
121 lines (106 loc) · 3.35 KB
/
h2z.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
import {WebSocketServer} from 'ws'
import axios from 'axios'
import { LRUCache } from 'lru-cache'
import microtime from 'microtime'
import crypto from 'crypto'
const debug = process.env.DEBUG || false
const options = {
max: process.env.MAX_CACHE || 10000,
ttl: 2000,
ttlAutopurge: true,
dispose: function(n, key) {
console.log('Batch disposal... ')
const payload = JSON.stringify(n)
if (debug) console.log(payload)
axios.post(process.env.HTTP_ENDPOINT || 'https://localhost:3100/tempo/api/push', payload, {
headers:{
'Content-Type': 'application/json'
}
})
.then((response) => {
console.log('Zipkin Data successfully sent', response.data)
})
.catch((error) => {
console.log('An error occurred while sending data', error)
})
}
}
let messages = new LRUCache(options)
function middleware(data) {
data = allStrings(data)
const traceId = hashString(data.callid, 32)
const parentId = hashString(data.uuid, 16)
let trace = [{
"id": parentId,
"traceId": traceId,
"timestamp": data.micro_ts || microtime.now(),
"duration": data.duration * 1000000 || 1000,
"name": `${data.callid}`,
"tags": data,
"localEndpoint": {
"serviceName": data.type || "hepic"
}
}]
if (debug) console.log(trace[0])
// Sub Span Generator
if (data.cdr_ringing > 0) {
trace.push({
"id": hashString(data.uuid.split('-')[0].slice(0, -2) + "10", 16),
"parentId": parentId,
"traceId": traceId,
"timestamp": data.cdr_start * 1000 || microtime.now(),
"duration": (data.cdr_ringing * 1000) - data.micro_ts || 1000,
"name": `Ringing`,
"tags": data,
"localEndpoint": {
"serviceName": data.type || "hepic"
}
})
}
if (data.cdr_connect > 0) {
trace.push({
"id": hashString(data.uuid.split('-')[0].slice(0, -2) + "20", 16),
"parentId": parentId,
"traceId": traceId,
"timestamp": data.cdr_start * 1000 || microtime.now(),
"duration": (data.cdr_connect * 1000 ) - data.micro_ts || 1000,
"name": `Connected`,
"tags": data,
"localEndpoint": {
"serviceName": data.type || "hepic"
}
})
}
return trace
}
const wss = new WebSocketServer({ port: process.env.WS_PORT || 18909 })
console.log(`h2z running, listening on ${process.env.WS_PORT || 18909}, sending traces to ${process.env.HTTP_ENDPOINT || 'https://localhost:3100/tempo/api/push'}. Debug is ${process.env.DEBUG}`)
wss.on('connection', (ws) => {
console.log('New WS connection established')
ws.on('error', (err) => {
console.log(`Websocket error: ${err}`)
})
ws.on('message', async (data) => {
data = JSON.parse(data.toString())
if (data.status < 10 ) return
if (messages.has(data.uuid)) return
const modifiedData = middleware(data)
messages.set(modifiedData.uuid, modifiedData)
})
ws.on('close', () => {
console.log('WS Connection closed')
})
})
/* Utils */
function hashString(str, max) {
const hash = crypto.createHash('sha256')
hash.update(str.toString())
const fullHash = hash.digest('hex')
return fullHash.substring(0, max || 32)
}
function allStrings(data){
for (let key in data) {
data[key] = data[key].toString()
}
return data
}