Skip to content

Commit b2d3e50

Browse files
V1.0.0 (#25)
* Do a linting step. * Rename FlaskTracer to FlaskTracing. * Use OT 2.0. * Do not log failed header extraction. * Add standard tags. (#21) * Let the tracer be optional. * Expose tracer as a property without trailing _. * Reorganize the tests into tracing an API files. * Add a start_span_cb callback. * trace_all_requests defaults to True if an app is specified. * Properly handle and report errors.
1 parent c003984 commit b2d3e50

15 files changed

+523
-225
lines changed

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ python:
44
- "3.6"
55
# command to install dependencies
66
install:
7-
- pip install -r requirements.txt
7+
- make bootstrap
88
# command to run tests
99
script:
10-
- pytest
10+
- make test lint

Makefile

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1+
project := flask_opentracing
2+
3+
pytest := PYTHONDONTWRITEBYTECODE=1 py.test --tb short -rxs \
4+
--cov-report term-missing:skip-covered --cov=$(project) tests
5+
16
.PHONY: test publish install clean clean-build clean-pyc clean-test build upload-docs
27

38
install:
49
python setup.py install
510

11+
check-virtual-env:
12+
@echo virtual-env: $${VIRTUAL_ENV?"Please run in virtual-env"}
13+
14+
bootstrap: check-virtual-env
15+
pip install -r requirements.txt
16+
pip install -r requirements-test.txt
17+
python setup.py develop
18+
619
clean: clean-build clean-pyc clean-test
720

821
clean-build:
@@ -25,13 +38,18 @@ clean-test:
2538
rm -f coverage.xml
2639
rm -fr htmlcov/
2740

28-
test:
29-
python setup.py test
30-
make -C docs doctest
41+
lint:
42+
flake8 $(project) tests
43+
44+
test:
45+
$(pytest)
3146

3247
build:
3348
python setup.py build
3449

50+
docs:
51+
make -C docs doctest
52+
3553
upload-docs:
3654
git submodule update --init # for sphinx flask theme
3755
python setup.py build_sphinx

README.rst

+29-14
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ As core services and libraries adopt OpenTracing, the application builder is no
88

99
If you want to learn more about the underlying python API, visit the python `source code`_.
1010

11+
If you are migrating from the 0.x series, you may want to read the list of `breaking changes`_.
12+
1113
.. _The OpenTracing Project: http://opentracing.io/
1214
.. _source code: https://github.com/opentracing/opentracing-python
15+
.. _breaking changes: #breaking-changes-from-0-x
1316

1417
Installation
1518
============
@@ -24,36 +27,36 @@ Usage
2427
=====
2528

2629
This Flask extension allows for tracing of Flask apps using the OpenTracing API. All
27-
that it requires is for a FlaskTracing tracer to be initialized using an
30+
that it requires is for a ``FlaskTracing`` tracer to be initialized using an
2831
instance of an OpenTracing tracer. You can either trace all requests to your site, or use function decorators to trace certain individual requests.
2932

3033
**Note:** `optional_args` in both cases are any number of attributes (as strings) of `flask.Request` that you wish to set as tags on the created span
3134

3235
Initialize
3336
----------
3437

35-
`FlaskTracer` wraps the tracer instance that's supported by opentracing. To create a `FlaskTracer` object, you can either pass in a tracer object directly or a callable that returns the tracer object. For example:
38+
`FlaskTracing` wraps the tracer instance that's supported by opentracing. To create a `FlaskTracing` object, you can either pass in a tracer object directly or a callable that returns the tracer object. For example:
3639

3740
.. code-block:: python
3841
3942
import opentracing
40-
from flask_opentracing import FlaskTracer
43+
from flask_opentracing import FlaskTracing
4144
4245
opentracing_tracer = ## some OpenTracing tracer implementation
43-
tracer = FlaskTracer(opentracing_tracer, ...)
46+
tracing = FlaskTracing(opentracing_tracer, ...)
4447
4548
or
4649

4750
.. code-block:: python
4851
4952
import opentracing
50-
from flask_opentracing import FlaskTracer
53+
from flask_opentracing import FlaskTracing
5154
5255
def initialize_tracer():
5356
...
5457
return opentracing_tracer
5558
56-
tracer = FlaskTracer(initialize_tracer, ...)
59+
tracing = FlaskTracing(initialize_tracer, ...)
5760
5861
5962
Trace All Requests
@@ -62,36 +65,36 @@ Trace All Requests
6265
.. code-block:: python
6366
6467
import opentracing
65-
from flask_opentracing import FlaskTracer
68+
from flask_opentracing import FlaskTracing
6669
6770
app = Flask(__name__)
6871
6972
opentracing_tracer = ## some OpenTracing tracer implementation
70-
tracer = FlaskTracer(opentracing_tracer, True, app, [optional_args])
73+
tracing = FlaskTracing(opentracing_tracer, True, app, [optional_args])
7174
7275
Trace Individual Requests
7376
-------------------------
7477

7578
.. code-block:: python
7679
7780
import opentracing
78-
from flask_opentracing import FlaskTracer
81+
from flask_opentracing import FlaskTracing
7982
8083
app = Flask(__name__)
8184
8285
opentracing_tracer = ## some OpenTracing tracer implementation
83-
tracer = FlaskTracer(opentracing_tracer)
86+
tracing = FlaskTracing(opentracing_tracer)
8487
8588
@app.route('/some_url')
86-
@tracer.trace(optional_args)
89+
@tracing.trace(optional_args)
8790
def some_view_func():
8891
...
8992
return some_view
9093
9194
Accessing Spans Manually
9295
------------------------
9396

94-
In order to access the span for a request, we've provided an method `FlaskTracer.get_span(request)` that returns the span for the request, if it is exists and is not finished. This can be used to log important events to the span, set tags, or create child spans to trace non-RPC events. If no request is passed in, the current request will be used.
97+
In order to access the span for a request, we've provided an method `FlaskTracing.get_span(request)` that returns the span for the request, if it is exists and is not finished. This can be used to log important events to the span, set tags, or create child spans to trace non-RPC events. If no request is passed in, the current request will be used.
9598

9699
Tracing an RPC
97100
--------------
@@ -100,10 +103,10 @@ If you want to make an RPC and continue an existing trace, you can inject the cu
100103

101104
.. code-block:: python
102105
103-
@tracer.trace()
106+
@tracing.trace()
104107
def some_view_func(request):
105108
new_request = some_http_request
106-
current_span = tracer.get_span(request)
109+
current_span = tracing.get_span(request)
107110
text_carrier = {}
108111
opentracing_tracer.inject(span, opentracing.Format.TEXT_MAP, text_carrier)
109112
for k, v in text_carrier.iteritems():
@@ -120,6 +123,18 @@ with integrated OpenTracing tracers.
120123

121124
`This tutorial <http://blog.scoutapp.com/articles/2018/01/15/tutorial-tracing-python-flask-requests-with-opentracing>`_ has a step-by-step guide for using `Flask-Opentracing` with `Jaeger <https://github.com/jaegertracing/jaeger>`_.
122125

126+
Breaking changes from 0.x
127+
=========================
128+
129+
Starting with the 1.0 version, a few changes have taken place from previous versions:
130+
131+
* ``FlaskTracer`` has been renamed to ``FlaskTracing``, although ``FlaskTracing``
132+
can be used still as a deprecated name.
133+
* When passing an ``Application`` object at ``FlaskTracing`` creation time,
134+
``trace_all_requests`` defaults to ``True``.
135+
* When no ``opentracing.Tracer`` is provided, ``FlaskTracing`` will rely on the
136+
global tracer.
137+
123138
Further Information
124139
===================
125140

docs/index.rst

+8-8
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,36 @@ Trace All Requests
3434
.. code-block:: python
3535
3636
import opentracing
37-
from flask_opentracing import FlaskTracer
37+
from flask_opentracing import FlaskTracing
3838
3939
app = Flask(__name__)
4040
4141
opentracing_tracer = ## some OpenTracing tracer implementation
42-
tracer = FlaskTracer(opentracing_tracer, True, app, [optional_args])
42+
tracing = FlaskTracing(opentracing_tracer, True, app, [optional_args])
4343
4444
Trace Individual Requests
4545
~~~~~~~~~~~~~~~~~~~~~~~~~
4646

4747
.. code-block:: python
4848
4949
import opentracing
50-
from flask_opentracing import FlaskTracer
50+
from flask_opentracing import FlaskTracing
5151
5252
app = Flask(__name__)
5353
5454
opentracing_tracer = ## some OpenTracing tracer implementation
55-
tracer = FlaskTracer(opentracing_tracer)
55+
tracing = FlaskTracing(opentracing_tracer)
5656
5757
@app.route('/some_url')
58-
@tracer.trace(optional_args)
58+
@tracing.trace(optional_args)
5959
def some_view_func():
6060
...
6161
return some_view
6262
6363
Accessing Spans Manually
6464
~~~~~~~~~~~~~~~~~~~~~~~~
6565

66-
In order to access the span for a request, we've provided an method `FlaskTracer.get_span(request)` that returns the span for the request, if it is exists and is not finished. This can be used to log important events to the span, set tags, or create child spans to trace non-RPC events. If no request is passed in, the current request will be used.
66+
In order to access the span for a request, we've provided an method `FlaskTracing.get_span(request)` that returns the span for the request, if it is exists and is not finished. This can be used to log important events to the span, set tags, or create child spans to trace non-RPC events. If no request is passed in, the current request will be used.
6767

6868
Tracing an RPC
6969
~~~~~~~~~~~~~~
@@ -72,10 +72,10 @@ If you want to make an RPC and continue an existing trace, you can inject the cu
7272

7373
.. code-block:: python
7474
75-
@tracer.trace()
75+
@tracing.trace()
7676
def some_view_func(request):
7777
new_request = some_http_request
78-
current_span = tracer.get_span(request)
78+
current_span = tracing.get_span(request)
7979
text_carrier = {}
8080
opentracing_tracer.inject(span, opentracing.Format.TEXT_MAP, text_carrier)
8181
for k, v in text_carrier.iteritems():

example/README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ your MySQLdb, SQLAlchemy, Redis queries without writing boilerplate code.
7676
Following code shows how to create a span based on already existing one.
7777

7878
```python
79-
parent_span = tracer.get_span()
80-
child_span = jaeger_tracer.start_span("inside create_child_span", parent_span)
79+
# child_scope.span automatically inherits from the Span created
80+
# for this request by the Flask instrumentation.
81+
child_scope = jaeger_tracer.start_active_span('inside create_child_span')
8182
... do some stuff
82-
child_span.finish()
83+
child_scope.close()
8384
```
8485

8586
![traced request](https://raw.githubusercontent.com/opentracing-contrib/python-flask/example/example/img/jaeger_1.png)

example/server.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from jaeger_client import Config
3-
from flask_opentracing import FlaskTracer
3+
from flask_opentracing import FlaskTracing
44
from flask import Flask, request
55
from os import getenv
66
JAEGER_HOST = getenv('JAEGER_HOST', 'localhost')
@@ -19,22 +19,21 @@
1919
# Service name can be arbitrary string describing this particular web service.
2020
service_name="jaeger_opentracing_example")
2121
jaeger_tracer = config.initialize_tracer()
22-
tracer = FlaskTracer(jaeger_tracer)
22+
tracing = FlaskTracing(jaeger_tracer)
2323

2424
@app.route('/log')
25-
@tracer.trace() # Indicate that /log endpoint should be traced
25+
@tracing.trace() # Indicate that /log endpoint should be traced
2626
def log():
27-
parent_span = tracer.get_span(request)
2827
# Extract the span information for request object.
29-
with jaeger_tracer.start_span("python webserver internal span of log method",
30-
child_of=parent_span) as span:
28+
with jaeger_tracer.start_active_span(
29+
'python webserver internal span of log method') as scope:
3130
# Perform some computations to be traced.
3231

3332
a = 1
3433
b = 2
3534
c = a + b
3635

37-
span.log_kv({'event': 'my computer knows math!', 'result': c})
36+
scope.span.log_kv({'event': 'my computer knows math!', 'result': c})
3837

3938
return "log"
4039

flask_opentracing/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from .tracer import FlaskTracer
1+
from .tracing import FlaskTracing # noqa
2+
from .tracing import FlaskTracing as FlaskTracer # noqa, deprecated

flask_opentracing/tracer.py

-98
This file was deleted.

0 commit comments

Comments
 (0)