-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
63 lines (56 loc) · 1.63 KB
/
index.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
const protoLoader = require('@grpc/proto-loader')
const grpc = require('@grpc/grpc-js')
const pkgDef = protoLoader.loadSync('./inifinite-trace.proto', {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
const proto = grpc.loadPackageDefinition(pkgDef)
const traceApi = proto.com.newrelic.trace.v1
// Simply just reflect back the data getting passed from client stream
let spans = 0
const infiniteTraceServer = {
recordSpan: function(call) {
console.log('recordSpan')
const { metadata } = call
console.log('meta', metadata)
call.on('data', (clientStream) => {
spans++
console.log(`got span ${clientStream.trace_id}`)
console.log(`processed ${spans} spans thus far`)
call.write({ messages_seen: 1 })
})
call.on('end', () => {
call.end()
})
},
recordSpanBatch: function(call) {
console.log('recordSpanBatch')
const { metadata } = call
console.log('meta', metadata)
call.on('data', (clientStream) => {
console.log(`saw ${clientStream.spans.length} spans`)
call.write({ messages_seen: clientStream.spans.length })
})
call.on('end', () => {
call.end()
})
}
}
async function connect() {
const server = new grpc.Server()
const credentials = grpc.ServerCredentials.createInsecure()
server.addService(traceApi.IngestService.service, infiniteTraceServer)
await new Promise((resolve, reject) => {
server.bindAsync('0.0.0.0:50051', credentials, (err, port) => {
if (err) {
reject(err)
} else resolve(port)
})
})
server.start()
console.log('started at localhost:50051!')
}
connect()