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

Add OpenTracing shim example #282

Merged
merged 21 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 16 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
90 changes: 90 additions & 0 deletions examples/opentracing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Overview
mauriciovasquezbernal marked this conversation as resolved.
Show resolved Hide resolved

This example shows how to use the [`opentelemetry-ext-opentracing-shim`
package](https://github.com/open-telemetry/opentelemetry-python/tree/master/ext/opentelemetry-ext-opentracing-shim)
to interact with libraries instrumented with
[`opentracing-python`](https://github.com/opentracing/opentracing-python).

The included `rediscache` library creates spans via the OpenTracing Redis
integration,
[`redis_opentracing`](https://github.com/opentracing-contrib/python-redis).
Spans are exported via the Jaeger exporter, which is attached to the
OpenTelemetry tracer.

## Installation

### Jaeger

Install and run
[Jaeger](https://www.jaegertracing.io/docs/latest/getting-started/#all-in-one).
See the [basic tracer
example](https://github.com/open-telemetry/opentelemetry-python/tree/master/examples/basic-tracer)
for more detail.

### Redis

Install Redis following the [instructions](https://redis.io/topics/quickstart).

Make sure that the Redis server is running by executing this:

```sh
$ redis-server
```

### Python Dependencies

Install the Python dependencies in [`requirements.txt`](requirements.txt):

```sh
$ pip install -r requirements.txt
```

Alternatively, you can install the Python dependencies separately:

```sh
$ pip install \
opentelemetry-api \
c24t marked this conversation as resolved.
Show resolved Hide resolved
opentelemetry-sdk \
opentelemetry-ext-jaeger \
opentelemetry-opentracing-shim \
redis \
redis_opentracing
```

## Run the Application

The example script calculates a few Fibonacci numbers and stores the results in
Redis. The script, the `rediscache` library, and the OpenTracing Redis
integration all contribute spans to the trace.

To run the script:

```sh
$ python main.py
```

After running, you can view the generated trace in the Jaeger UI.

#### Jaeger UI

Open the Jaeger UI in your browser at
[http://localhost:16686](http://localhost:16686) and view traces for the
c24t marked this conversation as resolved.
Show resolved Hide resolved
"OpenTracing Shim Example" service.

Each `main.py` run should generate a trace, and each trace should include
multiple spans that represent calls to Redis.

<p align="center"><img src="./images/jaeger-trace-full.png?raw=true"/></p>

Note that tags and logs (OpenTracing) and attributes and events (OpenTelemetry)
from both tracing systems appear in the exported trace.

<p align="center"><img src="./images/jaeger-span-expanded.png?raw=true"/></p>
mauriciovasquezbernal marked this conversation as resolved.
Show resolved Hide resolved

## Useful links
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
- For more information on tracing in Python, visit: <https://github.com/open-telemetry/opentelemetry-python>

## LICENSE

Apache License 2.0
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions examples/opentracing/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python

from opentelemetry import trace
from opentelemetry.ext import opentracing_shim
from opentelemetry.ext.jaeger import JaegerSpanExporter
from opentelemetry.sdk.trace import TracerSource
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
from rediscache import RedisCache

# Configure the tracer using the default implementation
trace.set_preferred_tracer_source_implementation(lambda T: TracerSource())
tracer_source = trace.tracer_source()

# Configure the tracer to export traces to Jaeger
jaeger_exporter = JaegerSpanExporter(
service_name="OpenTracing Shim Example",
agent_host_name="localhost",
agent_port=6831,
)
span_processor = SimpleExportSpanProcessor(jaeger_exporter)
tracer_source.add_span_processor(span_processor)

# Create an OpenTracing shim. This implements the OpenTracing tracer API, but
# forwards calls to the underlying OpenTelemetry tracer.
opentracing_tracer = opentracing_shim.create_tracer(tracer_source)

# Our example caching library expects an OpenTracing-compliant tracer.
redis_cache = RedisCache(opentracing_tracer)

# Appication code uses an OpenTelemetry Tracer as usual.
tracer = trace.tracer_source().get_tracer(__name__)


@redis_cache
def fib(number):
"""Get the Nth Fibonacci number, cache intermediate results in Redis."""
if number < 0:
raise ValueError
if number in (0, 1):
return number
return fib(number - 1) + fib(number - 2)


with tracer.start_as_current_span("Fibonacci") as span:
span.set_attribute("is_example", "yes :)")
fib(4)
61 changes: 61 additions & 0 deletions examples/opentracing/rediscache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
This is an example of a library written to work with opentracing-python. It
provides a simple caching decorator backed by Redis, and uses the OpenTracing
Redis integartion to automatically generate spans for each call to Redis.
c24t marked this conversation as resolved.
Show resolved Hide resolved
"""

import pickle
from functools import wraps

# FIXME The pylint disablings are needed here because the code of this
# example is being executed against the tox.ini of the main
# opentelemetry-python project. Find a way to separate the two.
import redis # pylint: disable=import-error
import redis_opentracing # pylint: disable=import-error


class RedisCache:
"""Redis-backed caching decorator, using OpenTracing!

Args:
tracer: an opentracing.tracer.Tracer
"""

def __init__(self, tracer):
redis_opentracing.init_tracing(tracer)
self.tracer = tracer
self.client = redis.StrictRedis()

def __call__(self, func):
@wraps(func)
def inner(*args, **kwargs):
with self.tracer.start_active_span("Caching decorator") as scope1:

# Pickle the call args to get a canonical key. Don't do this in
# prod!
key = pickle.dumps((func.__qualname__, args, kwargs))

pval = self.client.get(key)
if pval is not None:
val = pickle.loads(pval)
scope1.span.log_kv(
{"msg": "Found cached value", "val": val}
)
return val

scope1.span.log_kv({"msg": "Cache miss, calling function"})
with self.tracer.start_active_span(
'Call "{}"'.format(func.__name__)
) as scope2:
scope2.span.set_tag("func", func.__name__)
scope2.span.set_tag("args", str(args))
scope2.span.set_tag("kwargs", str(kwargs))

val = func(*args, **kwargs)
scope2.span.set_tag("val", str(val))

# Let keys expire after 10 seconds
self.client.setex(key, 10, pickle.dumps(val))
return val

return inner
6 changes: 6 additions & 0 deletions examples/opentracing/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
opentelemetry-api
c24t marked this conversation as resolved.
Show resolved Hide resolved
opentelemetry-sdk
opentelemetry-ext-jaeger
opentelemetry-opentracing-shim
c24t marked this conversation as resolved.
Show resolved Hide resolved
redis
redis_opentracing
mauriciovasquezbernal marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,16 @@

import logging

import opentracing
from deprecated import deprecated
from opentracing import ( # pylint: disable=no-name-in-module
codeboten marked this conversation as resolved.
Show resolved Hide resolved
Format,
Scope,
ScopeManager,
Span,
SpanContext,
Tracer,
UnsupportedFormatException,
)

import opentelemetry.trace as trace_api
from opentelemetry import propagators
Expand Down Expand Up @@ -117,7 +125,7 @@ def create_tracer(otel_tracer_source):
return TracerShim(otel_tracer_source.get_tracer(__name__, __version__))


class SpanContextShim(opentracing.SpanContext):
class SpanContextShim(SpanContext):
"""Implements :class:`opentracing.SpanContext` by wrapping a
:class:`opentelemetry.trace.SpanContext` object.

Expand Down Expand Up @@ -155,7 +163,7 @@ def baggage(self):
# TODO: Implement.


class SpanShim(opentracing.Span):
class SpanShim(Span):
"""Implements :class:`opentracing.Span` by wrapping a
:class:`opentelemetry.trace.Span` object.

Expand Down Expand Up @@ -296,7 +304,7 @@ def get_baggage_item(self, key): # pylint:disable=unused-argument
# TODO: Implement.


class ScopeShim(opentracing.Scope):
class ScopeShim(Scope):
"""A `ScopeShim` wraps the OpenTelemetry functionality related to span
activation/deactivation while using OpenTracing :class:`opentracing.Scope`
objects for presentation.
Expand Down Expand Up @@ -405,7 +413,7 @@ def close(self):
self._span.unwrap().end()


class ScopeManagerShim(opentracing.ScopeManager):
class ScopeManagerShim(ScopeManager):
"""Implements :class:`opentracing.ScopeManager` by setting and getting the
active `opentelemetry.trace.Span` in the OpenTelemetry tracer.

Expand Down Expand Up @@ -500,7 +508,7 @@ def tracer(self):
return self._tracer


class TracerShim(opentracing.Tracer):
class TracerShim(Tracer):
"""Implements :class:`opentracing.Tracer` by wrapping a
:class:`opentelemetry.trace.Tracer` object.

Expand All @@ -522,8 +530,8 @@ def __init__(self, tracer):
super().__init__(scope_manager=ScopeManagerShim(self))
self._otel_tracer = tracer
self._supported_formats = (
opentracing.Format.TEXT_MAP,
opentracing.Format.HTTP_HEADERS,
Format.TEXT_MAP,
Format.HTTP_HEADERS,
)

def unwrap(self):
Expand Down Expand Up @@ -673,7 +681,7 @@ def inject(self, span_context, format, carrier):
# opentelemetry-python.

if format not in self._supported_formats:
raise opentracing.UnsupportedFormatException
raise UnsupportedFormatException

propagator = propagators.get_global_httptextformat()

Expand All @@ -693,7 +701,7 @@ def extract(self, format, carrier):
# TODO: Support Format.BINARY once it is supported in
# opentelemetry-python.
if format not in self._supported_formats:
raise opentracing.UnsupportedFormatException
raise UnsupportedFormatException

def get_as_list(dict_object, key):
value = dict_object.get(key)
Expand Down
Loading