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 8 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
3 changes: 3 additions & 0 deletions ext/opentelemetry-ext-grpc/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ test =

[options.packages.find]
where = src

opentelemetry_instrumentor =
grpc = opentelemetry.ext.grpc:GrpcInstrumentor
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved
61 changes: 61 additions & 0 deletions ext/opentelemetry-ext-grpc/src/opentelemetry/ext/grpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,70 @@
# pylint:disable=import-self
# pylint:disable=no-name-in-module
# pylint:disable=relative-beyond-top-level
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 concurrent import futures
from opentelemetry import trace
from opentelemetry.ext.grpc.version import __version__
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.trace import get_tracer
from wrapt import ObjectProxy
from wrapt import wrap_function_wrapper as _wrap
from opentelemetry.ext.grpc.grpcext import intercept_channel, intercept_server
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved

import pdb
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved
from opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
SimpleExportSpanProcessor,
)


class GrpcInstrumentorServer(BaseInstrumentor):
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer_provider()
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved
server = None
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved

def _instrument(self, **kwargs):
self.server = grpc.server(futures.ThreadPoolExecutor())
self.server = intercept_server(self.server, server_interceptor())
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved

def _uninstrument(self, **kwargs):
if hasattr(self.server, 'stop'):
return self.server.stop(kwargs.get('grace'))


class GrpcInstrumentorClient(BaseInstrumentor):
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer_provider()
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved
channel = None

def _instrument(self, **kwargs):
hostport = kwargs.get('hostport')

if kwargs.get('channel_type') == 'secure':
self.channel = secure_channel_wrapper(hostport, kwargs.get("credentials"))

else:
self.channel = insecure_channel_wrapper(hostport)

def _uninstrument(self, **kwargs):

aravinsiva marked this conversation as resolved.
Show resolved Hide resolved
if hasattr(self.channel, 'close'):
return self.channel.close()


@contextmanager
def insecure_channel_wrapper(hostport):
with grpc.insecure_channel(hostport) as channel:
yield intercept_channel(channel, client_interceptor())


@contextmanager
def secure_channel_wrapper(hostport, credentials):
with grpc.secure_channel(hostport, credentials) as channel:
yield intercept_channel(channel, client_interceptor())


def client_interceptor(tracer_provider=None):
Expand Down
35 changes: 35 additions & 0 deletions ext/opentelemetry-ext-grpc/tests/test_server_interceptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from opentelemetry.ext.grpc.grpcext import intercept_server
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.test.test_base import TestBase
from opentelemetry.ext.grpc import GrpcInstrumentorServer, GrpcInstrumentorClient



class UnaryUnaryMethodHandler(grpc.RpcMethodHandler):
Expand All @@ -49,6 +51,39 @@ 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_instrumentor.server
aravinsiva marked this conversation as resolved.
Show resolved Hide resolved
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)


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

Expand Down