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

Skip to content

Commit bc585d4

Browse files
committed
Make examples portable
1 parent fa18357 commit bc585d4

16 files changed

+131
-12
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
import importlib
16+
import sys
17+
18+
# gRPC-generated modules expect other generated modules to be on the path.
19+
sys.path.extend(importlib.util.find_spec(__name__).submodule_search_locations)

docs/examples/grpc/examples/hello_world_client.py renamed to docs/examples/opentelemetry-example-app/src/opentelemetry_example_app/grpc/hello_world_client.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import grpc
2323

24-
import helloworld_pb2
25-
import helloworld_pb2_grpc
2624
from opentelemetry import trace
2725
from opentelemetry.ext.grpc import client_interceptor
2826
from opentelemetry.ext.grpc.grpcext import intercept_channel
@@ -32,6 +30,16 @@
3230
SimpleExportSpanProcessor,
3331
)
3432

33+
try:
34+
# Relative imports should work in the context of the package, e.g.:
35+
# `python -m opentelemetry_example_app.grpc.hello_world_client`.
36+
from .gen import helloworld_pb2, helloworld_pb2_grpc
37+
except ImportError:
38+
# This will fail when running the file as a script, e.g.:
39+
# `./hello_world_client.py`
40+
# fall back to importing from the same directory in this case.
41+
from gen import helloworld_pb2, helloworld_pb2_grpc
42+
3543
trace.set_tracer_provider(TracerProvider())
3644
trace.get_tracer_provider().add_span_processor(
3745
SimpleExportSpanProcessor(ConsoleSpanExporter())

docs/examples/grpc/examples/hello_world_server.py renamed to docs/examples/opentelemetry-example-app/src/opentelemetry_example_app/grpc/hello_world_server.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import grpc
2323

24-
import helloworld_pb2
25-
import helloworld_pb2_grpc
2624
from opentelemetry import trace
2725
from opentelemetry.ext.grpc import server_interceptor
2826
from opentelemetry.ext.grpc.grpcext import intercept_server
@@ -32,6 +30,16 @@
3230
SimpleExportSpanProcessor,
3331
)
3432

33+
try:
34+
# Relative imports should work in the context of the package, e.g.:
35+
# `python -m opentelemetry_example_app.grpc.hello_world_server`.
36+
from .gen import helloworld_pb2, helloworld_pb2_grpc
37+
except ImportError:
38+
# This will fail when running the file as a script, e.g.:
39+
# `./hello_world_server.py`
40+
# fall back to importing from the same directory in this case.
41+
from gen import helloworld_pb2, helloworld_pb2_grpc
42+
3543
trace.set_tracer_provider(TracerProvider())
3644
trace.get_tracer_provider().add_span_processor(
3745
SimpleExportSpanProcessor(ConsoleSpanExporter())
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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/blob/master/examples/python/helloworld/greeter_server.py
17+
"""The Python implementation of the GRPC helloworld.Greeter server."""
18+
19+
import logging
20+
from concurrent import futures
21+
22+
import grpc
23+
24+
import helloworld_pb2
25+
import helloworld_pb2_grpc
26+
from opentelemetry import trace
27+
from opentelemetry.ext.grpc import _server
28+
from opentelemetry.sdk.trace import TracerProvider
29+
from opentelemetry.sdk.trace.export import (
30+
ConsoleSpanExporter,
31+
SimpleExportSpanProcessor,
32+
)
33+
34+
trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider())
35+
trace.tracer_provider().add_span_processor(
36+
SimpleExportSpanProcessor(ConsoleSpanExporter())
37+
)
38+
tracer = trace.get_tracer(__name__)
39+
40+
41+
class Greeter(helloworld_pb2_grpc.GreeterServicer):
42+
def SayHello(self, request, context):
43+
return helloworld_pb2.HelloReply(message="Hello, %s!" % request.name)
44+
45+
46+
def serve():
47+
48+
interceptor = _server.OpenTelemetryServerInterceptor(tracer)
49+
server = grpc.server(
50+
futures.ThreadPoolExecutor(max_workers=10), interceptors=(interceptor,)
51+
)
52+
53+
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
54+
server.add_insecure_port("[::]:50051")
55+
server.start()
56+
server.wait_for_termination()
57+
58+
59+
if __name__ == "__main__":
60+
logging.basicConfig()
61+
serve()

docs/examples/grpc/examples/route_guide_client.py renamed to docs/examples/opentelemetry-example-app/src/opentelemetry_example_app/grpc/route_guide_client.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222

2323
import grpc
2424

25-
import route_guide_pb2
26-
import route_guide_pb2_grpc
27-
import route_guide_resources
2825
from opentelemetry import trace
2926
from opentelemetry.ext.grpc import client_interceptor
3027
from opentelemetry.ext.grpc.grpcext import intercept_channel
@@ -34,6 +31,18 @@
3431
SimpleExportSpanProcessor,
3532
)
3633

34+
try:
35+
# Relative imports should work in the context of the package, e.g.:
36+
# `python -m opentelemetry_example_app.grpc.route_guide_client`.
37+
from .gen import route_guide_pb2, route_guide_pb2_grpc
38+
from . import route_guide_resources
39+
except ImportError:
40+
# This will fail when running the file as a script, e.g.:
41+
# `./route_guide_client.py`
42+
# fall back to importing from the same directory in this case.
43+
from gen import route_guide_pb2, route_guide_pb2_grpc
44+
import route_guide_resources
45+
3746
trace.set_tracer_provider(TracerProvider())
3847
trace.get_tracer_provider().add_span_processor(
3948
SimpleExportSpanProcessor(ConsoleSpanExporter())

docs/examples/grpc/examples/route_guide_resources.py renamed to docs/examples/opentelemetry-example-app/src/opentelemetry_example_app/grpc/route_guide_resources.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"""Common resources used in the gRPC route guide example."""
1818

1919
import json
20+
import os
2021

2122
import route_guide_pb2
2223

@@ -29,12 +30,15 @@ def read_route_guide_database():
2930
route_guide_pb2.Features.
3031
"""
3132
feature_list = []
32-
with open("route_guide_db.json") as route_guide_db_file:
33+
db_file = os.path.join(os.path.dirname(__file__), "route_guide_db.json")
34+
with open(db_file) as route_guide_db_file:
3335
for item in json.load(route_guide_db_file):
3436
feature = route_guide_pb2.Feature(
3537
name=item["name"],
3638
location=route_guide_pb2.Point(
3739
latitude=item["location"]["latitude"],
38-
longitude=item["location"]["longitude"]))
40+
longitude=item["location"]["longitude"],
41+
),
42+
)
3943
feature_list.append(feature)
4044
return feature_list

docs/examples/grpc/examples/route_guide_server.py renamed to docs/examples/opentelemetry-example-app/src/opentelemetry_example_app/grpc/route_guide_server.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424

2525
import grpc
2626

27-
import route_guide_pb2
28-
import route_guide_pb2_grpc
29-
import route_guide_resources
27+
3028
from opentelemetry import trace
3129
from opentelemetry.ext.grpc import server_interceptor
3230
from opentelemetry.ext.grpc.grpcext import intercept_server
@@ -36,6 +34,18 @@
3634
SimpleExportSpanProcessor,
3735
)
3836

37+
try:
38+
# Relative imports should work in the context of the package, e.g.:
39+
# `python -m opentelemetry_example_app.grpc.route_guide_server`.
40+
from .gen import route_guide_pb2, route_guide_pb2_grpc
41+
from . import route_guide_resources
42+
except ImportError:
43+
# This will fail when running the file as a script, e.g.:
44+
# `./route_guide_server.py`
45+
# fall back to importing from the same directory in this case.
46+
from gen import route_guide_pb2, route_guide_pb2_grpc
47+
import route_guide_resources
48+
3949
trace.set_tracer_provider(TracerProvider())
4050
trace.get_tracer_provider().add_span_processor(
4151
SimpleExportSpanProcessor(ConsoleSpanExporter())

0 commit comments

Comments
 (0)