|
| 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