Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f24f826

Browse files
committed
Add gRPC route guide streaming examples
1 parent b597a80 commit f24f826

File tree

5 files changed

+1026
-0
lines changed

5 files changed

+1026
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2020, OpenTelemetry 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+
// https://github.com/grpc/grpc/blob/master/examples/protos/route_guide.proto
16+
17+
syntax = "proto3";
18+
19+
package routeguide;
20+
21+
// Interface exported by the server.
22+
service RouteGuide {
23+
// A simple RPC.
24+
//
25+
// Obtains the feature at a given position.
26+
//
27+
// A feature with an empty name is returned if there's no feature at the given
28+
// position.
29+
rpc GetFeature(Point) returns (Feature) {}
30+
31+
// A server-to-client streaming RPC.
32+
//
33+
// Obtains the Features available within the given Rectangle. Results are
34+
// streamed rather than returned at once (e.g. in a response message with a
35+
// repeated field), as the rectangle may cover a large area and contain a
36+
// huge number of features.
37+
rpc ListFeatures(Rectangle) returns (stream Feature) {}
38+
39+
// A client-to-server streaming RPC.
40+
//
41+
// Accepts a stream of Points on a route being traversed, returning a
42+
// RouteSummary when traversal is completed.
43+
rpc RecordRoute(stream Point) returns (RouteSummary) {}
44+
45+
// A Bidirectional streaming RPC.
46+
//
47+
// Accepts a stream of RouteNotes sent while a route is being traversed,
48+
// while receiving other RouteNotes (e.g. from other users).
49+
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
50+
}
51+
52+
// Points are represented as latitude-longitude pairs in the E7 representation
53+
// (degrees multiplied by 10**7 and rounded to the nearest integer).
54+
// Latitudes should be in the range +/- 90 degrees and longitude should be in
55+
// the range +/- 180 degrees (inclusive).
56+
message Point {
57+
int32 latitude = 1;
58+
int32 longitude = 2;
59+
}
60+
61+
// A latitude-longitude rectangle, represented as two diagonally opposite
62+
// points "lo" and "hi".
63+
message Rectangle {
64+
// One corner of the rectangle.
65+
Point lo = 1;
66+
67+
// The other corner of the rectangle.
68+
Point hi = 2;
69+
}
70+
71+
// A feature names something at a given point.
72+
//
73+
// If a feature could not be named, the name is empty.
74+
message Feature {
75+
// The name of the feature.
76+
string name = 1;
77+
78+
// The point where the feature is detected.
79+
Point location = 2;
80+
}
81+
82+
// A RouteNote is a message sent while at a given point.
83+
message RouteNote {
84+
// The location from which the message is sent.
85+
Point location = 1;
86+
87+
// The message to be sent.
88+
string message = 2;
89+
}
90+
91+
// A RouteSummary is received in response to a RecordRoute rpc.
92+
//
93+
// It contains the number of individual points received, the number of
94+
// detected features, and the total distance covered as the cumulative sum of
95+
// the distance between each point.
96+
message RouteSummary {
97+
// The number of points received.
98+
int32 point_count = 1;
99+
100+
// The number of known features passed while traversing the route.
101+
int32 feature_count = 2;
102+
103+
// The distance covered in metres.
104+
int32 distance = 3;
105+
106+
// The duration of the traversal in seconds.
107+
int32 elapsed_time = 4;
108+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/usr/bin/env python
2+
# Copyright 2020, OpenTelemetry Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# https://github.com/grpc/grpc/tree/master/examples/python/route_guide
17+
18+
"""The Python implementation of the gRPC route guide client."""
19+
20+
import random
21+
22+
import grpc
23+
24+
import route_guide_pb2
25+
import route_guide_pb2_grpc
26+
import route_guide_resources
27+
from opentelemetry import trace
28+
from opentelemetry.sdk.trace import TracerProvider
29+
from opentelemetry.sdk.trace.export import (
30+
ConsoleSpanExporter,
31+
SimpleExportSpanProcessor,
32+
)
33+
34+
trace.set_tracer_provider(TracerProvider())
35+
trace.get_tracer_provider().add_span_processor(
36+
SimpleExportSpanProcessor(ConsoleSpanExporter())
37+
)
38+
tracer = trace.get_tracer(__name__)
39+
40+
41+
def make_route_note(message, latitude, longitude):
42+
return route_guide_pb2.RouteNote(
43+
message=message,
44+
location=route_guide_pb2.Point(latitude=latitude, longitude=longitude),
45+
)
46+
47+
48+
def guide_get_one_feature(stub, point):
49+
feature = stub.GetFeature(point)
50+
if not feature.location:
51+
print("Server returned incomplete feature")
52+
return
53+
54+
if feature.name:
55+
print("Feature called %s at %s" % (feature.name, feature.location))
56+
else:
57+
print("Found no feature at %s" % feature.location)
58+
59+
60+
def guide_get_feature(stub):
61+
guide_get_one_feature(
62+
stub, route_guide_pb2.Point(latitude=409146138, longitude=-746188906)
63+
)
64+
guide_get_one_feature(stub, route_guide_pb2.Point(latitude=0, longitude=0))
65+
66+
67+
def guide_list_features(stub):
68+
rectangle = route_guide_pb2.Rectangle(
69+
lo=route_guide_pb2.Point(latitude=400000000, longitude=-750000000),
70+
hi=route_guide_pb2.Point(latitude=420000000, longitude=-730000000),
71+
)
72+
print("Looking for features between 40, -75 and 42, -73")
73+
74+
features = stub.ListFeatures(rectangle)
75+
76+
for feature in features:
77+
print("Feature called %s at %s" % (feature.name, feature.location))
78+
79+
80+
def generate_route(feature_list):
81+
for _ in range(0, 10):
82+
random_feature = feature_list[random.randint(0, len(feature_list) - 1)]
83+
print("Visiting point %s" % random_feature.location)
84+
yield random_feature.location
85+
86+
87+
def guide_record_route(stub):
88+
feature_list = route_guide_resources.read_route_guide_database()
89+
90+
route_iterator = generate_route(feature_list)
91+
route_summary = stub.RecordRoute(route_iterator)
92+
print("Finished trip with %s points " % route_summary.point_count)
93+
print("Passed %s features " % route_summary.feature_count)
94+
print("Travelled %s meters " % route_summary.distance)
95+
print("It took %s seconds " % route_summary.elapsed_time)
96+
97+
98+
def generate_messages():
99+
messages = [
100+
make_route_note("First message", 0, 0),
101+
make_route_note("Second message", 0, 1),
102+
make_route_note("Third message", 1, 0),
103+
make_route_note("Fourth message", 0, 0),
104+
make_route_note("Fifth message", 1, 0),
105+
]
106+
for msg in messages:
107+
print("Sending %s at %s" % (msg.message, msg.location))
108+
yield msg
109+
110+
111+
def guide_route_chat(stub):
112+
responses = stub.RouteChat(generate_messages())
113+
for response in responses:
114+
print(
115+
"Received message %s at %s" % (response.message, response.location)
116+
)
117+
118+
119+
def run():
120+
121+
from opentelemetry.ext.grpc.grpcext import intercept_channel
122+
from opentelemetry.ext.grpc import client_interceptor
123+
124+
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
125+
# used in circumstances in which the with statement does not fit the needs
126+
# of the code.
127+
with grpc.insecure_channel("localhost:50051") as channel:
128+
channel = intercept_channel(channel, client_interceptor(tracer))
129+
130+
stub = route_guide_pb2_grpc.RouteGuideStub(channel)
131+
print("-------------- GetFeature --------------")
132+
guide_get_feature(stub)
133+
print("-------------- ListFeatures --------------")
134+
guide_list_features(stub)
135+
print("-------------- RecordRoute --------------")
136+
guide_record_route(stub)
137+
print("-------------- RouteChat --------------")
138+
guide_route_chat(stub)
139+
140+
141+
if __name__ == "__main__":
142+
run()

0 commit comments

Comments
 (0)