|
| 1 | +# Copyright 2017, OpenCensus Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from concurrent import futures |
| 16 | +import time |
| 17 | + |
| 18 | +import grpc |
| 19 | + |
| 20 | +import hello_world_pb2 |
| 21 | +import hello_world_pb2_grpc |
| 22 | + |
| 23 | +from opencensus.trace.ext.grpc import server_interceptor |
| 24 | +from opencensus.trace.samplers import always_on |
| 25 | +from opencensus.trace.exporters import stackdriver_exporter |
| 26 | + |
| 27 | +_ONE_DAY_IN_SECONDS = 60 * 60 * 24 |
| 28 | + |
| 29 | + |
| 30 | +class HelloWorld(hello_world_pb2_grpc.HelloWorldServicer): |
| 31 | + |
| 32 | + def SayHello(self, request, context): |
| 33 | + return hello_world_pb2.HelloReply(message='Hello, %s!' % request.name) |
| 34 | + |
| 35 | + |
| 36 | +def serve(): |
| 37 | + sampler = always_on.AlwaysOnSampler() |
| 38 | + exporter = stackdriver_exporter.StackdriverExporter() |
| 39 | + tracer_interceptor = server_interceptor.OpenCensusServerInterceptor( |
| 40 | + sampler, exporter) |
| 41 | + server = grpc.server( |
| 42 | + futures.ThreadPoolExecutor(max_workers=10), |
| 43 | + interceptors=(tracer_interceptor,)) |
| 44 | + hello_world_pb2_grpc.add_HelloWorldServicer_to_server(HelloWorld(), server) |
| 45 | + server.add_insecure_port('[::]:50051') |
| 46 | + server.start() |
| 47 | + try: |
| 48 | + while True: |
| 49 | + time.sleep(_ONE_DAY_IN_SECONDS) |
| 50 | + except KeyboardInterrupt: |
| 51 | + server.stop(0) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == '__main__': |
| 55 | + serve() |
0 commit comments