Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding gRPC instrumentor #788

Merged
merged 36 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
71ce915
adding instrumentors to grpc autoinstrumentor
aravinsiva Jun 5, 2020
17741ab
adding grpc instrumentor class
aravinsiva Jun 8, 2020
eebd8fd
adding grpc integration
aravinsiva Jun 12, 2020
7785468
writing tests to test grpc integration
aravinsiva Jun 15, 2020
594ed31
Merge branch 'master' into feature
aravinsiva Jun 15, 2020
1ca41f1
Merge branch 'master' into feature
aravinsiva Jun 16, 2020
3a51c6e
code clean up
aravinsiva Jun 16, 2020
effdcf4
Merge branch 'feature' of https://github.com/aravinsiva/opentelemetry…
aravinsiva Jun 16, 2020
8164779
makng requested review changes
aravinsiva Jun 17, 2020
a172c50
Merge branch 'master' into feature
aravinsiva Jun 17, 2020
10393ba
fixing lint issue
aravinsiva Jun 17, 2020
8f3c21c
Merge branch 'feature' of https://github.com/aravinsiva/opentelemetry…
aravinsiva Jun 17, 2020
7a6da4a
applying tox changes
aravinsiva Jun 17, 2020
f44eeba
wrapping server and chnnel calls
aravinsiva Jun 19, 2020
914ca0b
Merge branch 'feature' of https://github.com/aravinsiva/opentelemetry…
aravinsiva Jun 19, 2020
ff1dfb2
fixing tox issues
aravinsiva Jun 21, 2020
c3e4eca
Merge branch 'master' into feature
aravinsiva Jun 21, 2020
50c8bc0
fixing lint issues
aravinsiva Jun 23, 2020
5227512
Merge branch 'feature' of https://github.com/aravinsiva/opentelemetry…
aravinsiva Jun 23, 2020
687bf8a
fixing linter issue
aravinsiva Jun 23, 2020
750964b
making lint modifications
aravinsiva Jun 25, 2020
125429b
fixing build issue
aravinsiva Jun 25, 2020
7c4b31d
Merge branch 'master' into feature
aravinsiva Jul 6, 2020
9e94b06
Merge branch 'master' into feature
aravinsiva Jul 17, 2020
8b0e5cf
fixing issues with number of paramters
aravinsiva Jul 17, 2020
0b2ab99
fixing suggested changes
aravinsiva Jul 18, 2020
02cfe04
adding changes to changelog
aravinsiva Jul 20, 2020
a31d51d
Update ext/opentelemetry-ext-grpc/setup.cfg
aravinsiva Jul 20, 2020
80dd2b3
Merge branch 'master' into feature
lzchen Jul 21, 2020
6ceadd9
adding example code, removing self.server and self.channel as well as…
aravinsiva Jul 21, 2020
c4a1ac8
Merge branch 'feature' of https://github.com/aravinsiva/opentelemetry…
aravinsiva Jul 21, 2020
e035d77
adding uninstrument test
aravinsiva Jul 21, 2020
81cc13e
Merge branch 'master' into feature
aravinsiva Jul 22, 2020
309a6da
fixing lint issue
aravinsiva Jul 22, 2020
c7dcb05
Merge branch 'feature' of https://github.com/aravinsiva/opentelemetry…
aravinsiva Jul 22, 2020
bd45e62
fixing underline issue
aravinsiva Jul 22, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ext/opentelemetry-ext-grpc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Add status code to gRPC client spans
([896](https://github.com/open-telemetry/opentelemetry-python/pull/896))
- Add gRPC client and server instrumentors
([788](https://github.com/open-telemetry/opentelemetry-python/pull/788))

## 0.8b0

Expand Down
5 changes: 5 additions & 0 deletions ext/opentelemetry-ext-grpc/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ test =

[options.packages.find]
where = src

[options.entry_points]
opentelemetry_instrumentor =
grpc_client = opentelemetry.ext.grpc:GrpcInstrumentorClient
grpc_server = opentelemetry.ext.grpc:GrpcInstrumentorServer
57 changes: 55 additions & 2 deletions ext/opentelemetry-ext-grpc/src/opentelemetry/ext/grpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,66 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# pylint:disable=import-outside-toplevel
# pylint:disable=import-self
# pylint:disable=no-name-in-module
# pylint:disable=relative-beyond-top-level
# pylint:disable=import-error
# pylint:disable=no-self-use
from contextlib import contextmanager

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some example code in the comments would be nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added examples for both client and server

import grpc
from wrapt import wrap_function_wrapper as _wrap

from opentelemetry import trace
from opentelemetry.ext.grpc.grpcext import intercept_channel, intercept_server
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved
from opentelemetry.ext.grpc.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.utils import unwrap

# pylint:disable=import-outside-toplevel
# pylint:disable=import-self
# pylint:disable=unused-argument
# isort:skip


class GrpcInstrumentorServer(BaseInstrumentor):
def __init__(self):
self.server = None

def _instrument(self, **kwargs):
_wrap("grpc", "server", self.wrapper_fn)

def _uninstrument(self, **kwargs):
unwrap(grpc, "server")

def wrapper_fn(self, original_func, instance, args, kwargs):
self.server = original_func(*args, **kwargs)
self.server = intercept_server(self.server, server_interceptor())
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved


class GrpcInstrumentorClient(BaseInstrumentor):
def __init__(self):
self.channel = None

def _instrument(self, **kwargs):

if kwargs.get("channel_type") == "secure":
_wrap("grpc", "secure_channel", self.wrapper_fn)

else:
_wrap("grpc", "insecure_channel", self.wrapper_fn)

def _uninstrument(self, **kwargs):
if kwargs.get("channel_type") == "secure":
unwrap(grpc, "secure_channel")

aravinsiva marked this conversation as resolved.
Show resolved Hide resolved
else:
unwrap(grpc, "insecure_channel")

@contextmanager
def wrapper_fn(self, original_func, instance, args, kwargs):
with original_func(*args, **kwargs) as channel:
self.channel = intercept_channel(channel, client_interceptor())
yield self.channel
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved


def client_interceptor(tracer_provider=None):
Expand Down
37 changes: 36 additions & 1 deletion ext/opentelemetry-ext-grpc/tests/test_server_interceptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import opentelemetry.ext.grpc
from opentelemetry import trace
from opentelemetry.ext.grpc import server_interceptor
from opentelemetry.ext.grpc import GrpcInstrumentorServer, server_interceptor
from opentelemetry.ext.grpc.grpcext import intercept_server
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.test.test_base import TestBase
Expand All @@ -49,6 +49,41 @@ def service(self, handler_call_details):


class TestOpenTelemetryServerInterceptor(TestBase):
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved
def test_instrumentor(self):
def handler(request, context):
return b""

grpc_server_instrumentor = GrpcInstrumentorServer()
grpc_server_instrumentor.instrument()

server = grpc.server(
futures.ThreadPoolExecutor(max_workers=1),
options=(("grpc.so_reuseport", 0),),
)

server = grpc_server_instrumentor.server
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved

server.add_generic_rpc_handlers((UnaryUnaryRpcHandler(handler),))

port = server.add_insecure_port("[::]:0")
# grpc_client_instrumentor = GrpcInstrumentorClient()
# grpc_client_instrumentor.instrument(hostport=port)
channel = grpc.insecure_channel("localhost:{:d}".format(port))

try:
server.start()
channel.unary_unary("")(b"")
finally:
server.stop(None)

spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]
self.assertEqual(span.name, "")
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved
self.assertIs(span.kind, trace.SpanKind.SERVER)
self.check_span_instrumentation_info(span, opentelemetry.ext.grpc)
grpc_server_instrumentor.uninstrument()

def test_create_span(self):
"""Check that the interceptor wraps calls with spans server-side."""

Expand Down