Skip to main content

OpenTracing support for Flask applications

Project description

Installation

Run the following command:

$ pip install Flask-Opentracing

Useage

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

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

Trace All Requests

import opentracing
from flask_opentracing import FlaskTracer

app = Flask(__name__)

opentracing_tracer = ## some OpenTracing tracer implementation
tracer = FlaskTracer(opentracing_tracer, True, app, [optional_args])

Trace Individual Requests

import opentracing
from flask_opentracing import FlaskTracer

app = Flask(__name__)

opentracing_tracer = ## some OpenTracing tracer implementation
tracer = FlaskTracer(opentracing_tracer)

@app.route('/some_url')
@tracer.trace(optional_args)
def some_view_func():
    ...
    return some_view

Accessing Spans Manually

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.

Tracing an RPC

If you want to make an RPC and continue an existing trace, you can inject the current span into the RPC. For example, if making an http request, the following code will continue your trace across the wire:

@tracer.trace()
def some_view_func(request):
    new_request = some_http_request
    current_span = tracer.get_span(request)
    text_carrier = {}
    opentracing_tracer.inject(span, opentracing.Format.TEXT_MAP, text_carrier)
    for k, v in text_carrier.iteritems():
        request.add_header(k,v)
    ... # make request

Examples

See the examples folder to view and run an example of two Flask applications with integrated OpenTracing tracers.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-OpenTracing-0.1.0.tar.gz (4.0 kB view details)

Uploaded Source

File details

Details for the file Flask-OpenTracing-0.1.0.tar.gz.

File metadata

File hashes

Hashes for Flask-OpenTracing-0.1.0.tar.gz
Algorithm Hash digest
SHA256 023c311d384b7d0284d8bef58eea070ef8aa173e4e1fbbe5fd35c560a60b7b6e
MD5 453fe9682278c7d35bcc5a0db9fd40f9
BLAKE2b-256 fcf6ef830814c0b530013ccf343f30c14afb3063a2482c485f4f0b4d1dc18055

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page