forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
39 lines (33 loc) · 1.21 KB
/
server.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
'use strict';
const api = require('@opentelemetry/api');
const tracer = require('./tracer')(('example-grpc-server'));
// eslint-disable-next-line import/order
const grpc = require('grpc');
const messages = require('./helloworld_pb');
const services = require('./helloworld_grpc_pb');
const PORT = 50051;
/** Starts a gRPC server that receives requests on sample server port. */
function startServer() {
// Creates a server
const server = new grpc.Server();
server.addService(services.GreeterService, { sayHello });
server.bind(`0.0.0.0:${PORT}`, grpc.ServerCredentials.createInsecure());
console.log(`binding server on 0.0.0.0:${PORT}`);
server.start();
}
function sayHello(call, callback) {
const currentSpan = api.trace.getSpan(api.context.active());
// display traceid in the terminal
console.log(`traceid: ${currentSpan.spanContext().traceId}`);
const span = tracer.startSpan('server.js:sayHello()', {
parent: currentSpan,
kind: 1, // server
attributes: { key: 'value' },
});
span.addEvent(`invoking sayHello() to ${call.request.getName()}`);
const reply = new messages.HelloReply();
reply.setMessage(`Hello ${call.request.getName()}`);
callback(null, reply);
span.end();
}
startServer();