Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 8059f54

Browse files
authored
Add a hello world example for grpc integration (#99)
1 parent f534b6e commit 8059f54

5 files changed

Lines changed: 307 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
syntax = "proto3";
16+
17+
// The hello world service definition.
18+
service HelloWorld {
19+
// Say hello to the request sender (unary-unary)
20+
rpc SayHello (HelloRequest) returns (HelloReply) {}
21+
}
22+
23+
// The request message containing the sender's name.
24+
message HelloRequest {
25+
string name = 1;
26+
}
27+
28+
// The response message containing the greetings
29+
message HelloReply {
30+
string message = 1;
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 __future__ import print_function
16+
17+
import grpc
18+
19+
import hello_world_pb2
20+
import hello_world_pb2_grpc
21+
22+
from opencensus.trace.tracer import Tracer
23+
from opencensus.trace.exporters import stackdriver_exporter
24+
from opencensus.trace.ext.grpc import client_interceptor
25+
26+
HOST_PORT = 'localhost:50051'
27+
28+
29+
def main():
30+
exporter = stackdriver_exporter.StackdriverExporter()
31+
tracer = Tracer(exporter=exporter)
32+
tracer_interceptor = client_interceptor.OpenCensusClientInterceptor(
33+
tracer,
34+
host_port=HOST_PORT)
35+
channel = grpc.insecure_channel(HOST_PORT)
36+
channel = grpc.intercept_channel(channel, tracer_interceptor)
37+
stub = hello_world_pb2_grpc.HelloWorldStub(channel)
38+
response = stub.SayHello(hello_world_pb2.HelloRequest(name='you'))
39+
print("Message received: " + response.message)
40+
41+
42+
if __name__ == '__main__':
43+
main()

examples/trace/grpc/hello_world_pb2.py

Lines changed: 132 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
2+
import grpc
3+
4+
import hello_world_pb2 as hello__world__pb2
5+
6+
7+
class HelloWorldStub(object):
8+
"""The hello world service definition.
9+
"""
10+
11+
def __init__(self, channel):
12+
"""Constructor.
13+
14+
Args:
15+
channel: A grpc.Channel.
16+
"""
17+
self.SayHello = channel.unary_unary(
18+
'/HelloWorld/SayHello',
19+
request_serializer=hello__world__pb2.HelloRequest.SerializeToString,
20+
response_deserializer=hello__world__pb2.HelloReply.FromString,
21+
)
22+
23+
24+
class HelloWorldServicer(object):
25+
"""The hello world service definition.
26+
"""
27+
28+
def SayHello(self, request, context):
29+
"""Say hello to the request sender (unary-unary)
30+
"""
31+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
32+
context.set_details('Method not implemented!')
33+
raise NotImplementedError('Method not implemented!')
34+
35+
36+
def add_HelloWorldServicer_to_server(servicer, server):
37+
rpc_method_handlers = {
38+
'SayHello': grpc.unary_unary_rpc_method_handler(
39+
servicer.SayHello,
40+
request_deserializer=hello__world__pb2.HelloRequest.FromString,
41+
response_serializer=hello__world__pb2.HelloReply.SerializeToString,
42+
),
43+
}
44+
generic_handler = grpc.method_handlers_generic_handler(
45+
'HelloWorld', rpc_method_handlers)
46+
server.add_generic_rpc_handlers((generic_handler,))
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)