-
Notifications
You must be signed in to change notification settings - Fork 0
/
RouteGuideServer.kt
90 lines (75 loc) · 3.02 KB
/
RouteGuideServer.kt
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
package me.nevi.opentracingtest.rpc
import io.grpc.*
import io.grpc.examples.routeguide.Feature
import io.grpc.examples.routeguide.Point
import io.grpc.examples.routeguide.RouteGuideGrpc
import io.grpc.netty.NettyChannelBuilder
import io.grpc.netty.NettyServerBuilder
import io.grpc.stub.StreamObserver
import io.opentracing.contrib.grpc.ServerTracingInterceptor
import io.opentracing.util.GlobalTracer
import java.io.IOException
import java.net.InetSocketAddress
import java.util.logging.Logger
class RouteGuideServer(serverBuilder: ManagedChannel, host: String, port: Int) {
private val server: Server
/**
* create server to listen on {@code host} {@code port}.
*/
@Throws(IOException::class)
constructor(host: String, port: Int) : this(NettyChannelBuilder.forAddress(host, port).usePlaintext().build(), host, port)
init {
val tracingInterceptor = ServerTracingInterceptor(GlobalTracer.get())
// I can't find a way of specifying this when creating a tracer
val tracedAttributes = setOf(
ServerTracingInterceptor.ServerRequestAttribute.CALL_ATTRIBUTES,
ServerTracingInterceptor.ServerRequestAttribute.HEADERS,
ServerTracingInterceptor.ServerRequestAttribute.METHOD_NAME,
ServerTracingInterceptor.ServerRequestAttribute.METHOD_TYPE
)
server = NettyServerBuilder.forAddress(InetSocketAddress(host, port)).apply {
addService(tracingInterceptor.intercept(RouteGuideService()))
}.build()
}
/**
* Start serving requests.
*/
@Throws(IOException::class)
fun start() {
server.start()
logger.info("Server started, listening on ${server.port}")
Runtime.getRuntime().addShutdownHook(object:Thread() {
override fun run() {
// Use stderr here since the logger may has been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down")
this@RouteGuideServer.stop()
System.err.println("*** server shut down")
}
})
}
/**
* Stop serving requests and shutdown resources.
*/
fun stop() {
server.shutdown()
}
/**
* Await termination on the main thread since the grpc library uses daemon threads.
*/
@Throws(InterruptedException::class)
fun blockUntilShutdown() {
server.awaitTermination()
}
private class RouteGuideService: RouteGuideGrpc.RouteGuideImplBase() {
override fun getFeature(request: Point, responseObserver: StreamObserver<Feature>) {
val feature = Feature.newBuilder().setName("Test Feature")
val tracer = GlobalTracer.get()
println("Expecting an active span, but found ${tracer.activeSpan()}")
responseObserver.onNext(feature.build())
responseObserver.onCompleted()
}
}
companion object {
private val logger = Logger.getLogger(RouteGuideServer::class.java.name)
}
}