Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 17 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,46 @@ pip install grpcio-opentracing

## Getting started

See the below code for basic usage or [examples/trivial](examples/trivial) for a
See the below code for basic usage or [examples/hello_world](examples/hello_world) for a
complete example.

### Client-side usage example

```python
import grpc
from grpc_opentracing import open_tracing_client_interceptor
from grpc_opentracing.grpcext import intercept_channel

tracer = # some OpenTracing Tracer instance
interceptor = open_tracing_client_interceptor(tracer)
tracer_interceptor = open_tracing_client_interceptor.OpenTracingClientInterceptor(tracer)
channel = # the grpc.Channel you created to invoke RPCs
channel = intercept_channel(channel, interceptor)
channel = grpc.intercept_channel(channel, tracer_interceptor)

# All future RPC activity involving `channel` will be automatically traced.
```

### Server-side usage example

```python
import grpc
from concurrent import futures
from grpc_opentracing import open_tracing_server_interceptor
from grpc_opentracing.grpcext import intercept_server

tracer = # some OpenTracing Tracer instance
interceptor = open_tracing_server_interceptor(tracer)
server = # the grpc.Server you created to receive RPCs
server = intercept_server(server, interceptor)

tracer_interceptor = open_tracing_server_interceptor.OpenTracingServerInterceptor(tracer)
server = grpc.server(
futures.ThreadPoolExecutor(max_workers=10),
interceptors=(tracer_interceptor,))
# All future RPC activity involving `server` will be automatically traced.
```

### Integrating with other spans.

`grpcio-opentracing` provides features that let you connect its span with other
tracing spans. On the client-side, you can write a class that derives from
`ActiveSpanSource` and provide it when creating the interceptor.

```python
class CustomActiveSpanSource(ActiveSpanSource):
@classmethod
def get_active_span(self):
# your custom method of getting the active span
tracer = # some OpenTracing Tracer instance
interceptor = open_tracing_client_interceptor(
tracer,
active_span_source=CustomActiveSpanSource)
...
```

On the server-side, the `context` argument passed into your service methods
packages the gRPC span created on the server-side.

```python
class CustomRpcService(...):
...
def Method1(self, request, context):
span = context.get_active_span()
...
```
from grpc_opentracing import scope

See [examples/integration](examples/integration) for a complete example.
span = scope.get_active_span()
span = tracer.start_span("do some thing", child_of=span)
# do some thing
span.finish()
...
```
Empty file.
23 changes: 23 additions & 0 deletions examples/hello_world/hello_world.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";

package helloworld;

// The hello world service definition.
service Greeter {
// Say hello to the request sender (unary-unary)
rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the sender's name.
message HelloRequest {
string name = 1;
}

// The response message containing the greetings
message HelloReply {
string message = 1;
}
50 changes: 50 additions & 0 deletions examples/hello_world/hello_world_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from __future__ import print_function

import argparse
import time

import grpc
from jaeger_client import Config

import hello_world_pb2
import hello_world_pb2_grpc
from grpc_opentracing import open_tracing_client_interceptor
from grpc_opentracing import scope

HOST_PORT = 'localhost:50051'


def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--log_payloads',
action='store_true',
default=True,
help='log request/response objects to open-tracing spans')
args = parser.parse_args()

config = Config(
config={
'sampler': {
'type': 'const',
'param': 1,
},
'logging': True,
},
service_name='hello_world_client')
tracer = config.initialize_tracer()
tracer_interceptor = open_tracing_client_interceptor.OpenTracingClientInterceptor(
tracer,
log_payloads=args.log_payloads)
with tracer.start_span("step1") as span:
scope.set_active_span(span)
time.sleep(0.01)
channel = grpc.insecure_channel(HOST_PORT)
channel = grpc.intercept_channel(channel, tracer_interceptor)
stub = hello_world_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(hello_world_pb2.HelloRequest(name='you'))
print("Message received: " + response.message)


if __name__ == '__main__':
main()
134 changes: 134 additions & 0 deletions examples/hello_world/hello_world_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions examples/hello_world/hello_world_pb2_grpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
import grpc

import hello_world_pb2 as hello__world__pb2


class GreeterStub(object):
"""The hello world service definition.
"""

def __init__(self, channel):
"""Constructor.

Args:
channel: A grpc.Channel.
"""
self.SayHello = channel.unary_unary(
'/helloworld.Greeter/SayHello',
request_serializer=hello__world__pb2.HelloRequest.SerializeToString,
response_deserializer=hello__world__pb2.HelloReply.FromString,
)


class GreeterServicer(object):
"""The hello world service definition.
"""

def SayHello(self, request, context):
"""Say hello to the request sender (unary-unary)
"""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')


def add_GreeterServicer_to_server(servicer, server):
rpc_method_handlers = {
'SayHello': grpc.unary_unary_rpc_method_handler(
servicer.SayHello,
request_deserializer=hello__world__pb2.HelloRequest.FromString,
response_serializer=hello__world__pb2.HelloReply.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'helloworld.Greeter', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
Loading