Skip to content

Commit 54b390f

Browse files
c24tcodebotenmauriciovasquezbernalmariojonkedgzlopes
authored
gRPC integration (#476)
This is a port of grpcio-opentracing, and borrows from opencensus-ext-grpc. It adds client and server interceptors that wrap each request in a span and use the new context API to inject/extract the traceparent header to/from gRPC metadata. Co-authored-by: alrex <[email protected]> Co-authored-by: Mauricio Vásquez <[email protected]> Co-authored-by: Mario Jonke <[email protected]> Co-authored-by: Daniel González <[email protected]> Co-authored-by: Alex Boten <[email protected]> Co-authored-by: Yusuke Tsutsumi <[email protected]>
1 parent 9320cfe commit 54b390f

37 files changed

+3530
-25
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ exclude =
1717
__pycache__
1818
ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/gen/
1919
ext/opentelemetry-ext-jaeger/build/*
20+
docs/examples/opentelemetry-example-app/src/opentelemetry_example_app/grpc/gen/

.isort.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ line_length=79
1313
; docs: https://github.com/timothycrosley/isort#multi-line-output-modes
1414
multi_line_output=3
1515
skip=target
16-
skip_glob=ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/gen/*,.venv*/*,venv*/*
17-
known_first_party=opentelemetry
16+
skip_glob=**/gen/*,.venv*/*,venv*/*
17+
known_first_party=opentelemetry,opentelemetry_example_app
1818
known_third_party=psutil,pytest

docs/examples/opentelemetry-example-app/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"opentelemetry-ext-flask",
4242
"flask",
4343
"requests",
44+
"protobuf~=3.11",
4445
],
4546
license="Apache-2.0",
4647
package_dir={"": "src"},

docs/examples/opentelemetry-example-app/src/opentelemetry_example_app/grpc/__init__.py

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright The 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)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# Copyright The 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+
from grpc_tools import protoc
17+
18+
19+
def main():
20+
return protoc.main(
21+
[
22+
"-I.",
23+
"--python_out=.",
24+
"--grpc_python_out=.",
25+
"helloworld.proto",
26+
"route_guide.proto",
27+
]
28+
)
29+
30+
31+
if __name__ == "__main__":
32+
main()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright The 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/helloworld.proto
16+
17+
syntax = "proto3";
18+
19+
package helloworld;
20+
21+
// The greeting service definition.
22+
service Greeter {
23+
// Sends a greeting
24+
rpc SayHello (HelloRequest) returns (HelloReply) {}
25+
}
26+
27+
// The request message containing the user's name.
28+
message HelloRequest {
29+
string name = 1;
30+
}
31+
32+
// The response message containing the greetings
33+
message HelloReply {
34+
string message = 1;
35+
}

docs/examples/opentelemetry-example-app/src/opentelemetry_example_app/grpc/gen/helloworld_pb2.py

Lines changed: 131 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 helloworld_pb2 as helloworld__pb2
5+
6+
7+
class GreeterStub(object):
8+
"""The greeting 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.Greeter/SayHello',
19+
request_serializer=helloworld__pb2.HelloRequest.SerializeToString,
20+
response_deserializer=helloworld__pb2.HelloReply.FromString,
21+
)
22+
23+
24+
class GreeterServicer(object):
25+
"""The greeting service definition.
26+
"""
27+
28+
def SayHello(self, request, context):
29+
"""Sends a greeting
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_GreeterServicer_to_server(servicer, server):
37+
rpc_method_handlers = {
38+
'SayHello': grpc.unary_unary_rpc_method_handler(
39+
servicer.SayHello,
40+
request_deserializer=helloworld__pb2.HelloRequest.FromString,
41+
response_serializer=helloworld__pb2.HelloReply.SerializeToString,
42+
),
43+
}
44+
generic_handler = grpc.method_handlers_generic_handler(
45+
'helloworld.Greeter', rpc_method_handlers)
46+
server.add_generic_rpc_handlers((generic_handler,))
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright The 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+
}

0 commit comments

Comments
 (0)