Python Quickstart

The example demonstrates how to record traces for a simple Flask web application.

OpenCensus Python API Documentation

The OpenCensus libraries artifacts are released to PyPI. The API documentation is available here.

The Quickstart Example Code

  1. Clone the OpenCensus Python GitHub repo:
    a. git clone

  2. Code is in directory: examples/trace/helloworld/flask.

To Run The Example

  1. Install dependencies via pip:
    $ pip install opencensus

  2. The OpenCensus Python Quickstart example can be run as below:
    $ python

  3. Make a HTTP request to hit the application:
    $ curl http://localhost:8080/

The Example Code

import flask
from opencensus.trace.exporters import stackdriver_exporter
from opencensus.trace.ext.flask.flask_middleware import FlaskMiddleware

app = flask.Flask(__name__)

# Enable tracing the requests
middleware = FlaskMiddleware(app)

def hello():
   return 'Hello world!'

if __name__ == '__main__':'localhost', port=8080)

The Example Output (Raw)

      '/http/method': 'GET',
      '/http/url': 'http://localhost:8080/',
      '/http/status_code': '200