Skip to main content

OpenTracing support for Python DB API

Project description

This package enables distributed tracing in Python Database API 2.0 compatible-clients via The OpenTracing Project. Once a production system contends with real concurrency or splits into many services, crucial (and formerly easy) tasks become difficult: user-facing latency optimization, root-cause analysis of backend errors, communication about distinct pieces of a now-distributed system, etc. Distributed tracing follows a request on its journey from inception to completion from mobile/browser all the way to the microservices.

As core services and libraries adopt OpenTracing, the application builder is no longer burdened with the task of adding basic tracing instrumentation to their own code. In this way, developers can build their applications with the tools they prefer and benefit from built-in tracing instrumentation. OpenTracing implementations exist for major distributed tracing systems and can be bound or swapped with a one-line configuration change.

If you want to learn more about the underlying Python API, visit the Python source code.

Installation

Run the following command:

$ pip install dbapi-opentracing

Usage

This DB API extension allows the tracing of database queries using the OpenTracing API. All that it requires is for a ConnectionTracing tracer to be initialized using an instance of an OpenTracing tracer and a DB API Connection object. You can either trace all commands sent to your database, or use a Cursor to trace individual requests.

Initialize

ConnectionTracing wraps the Connection and Tracer instances that are supported by the Python DB API and OpenTracing, respectively. To create a ConnectionTracing object, you can either pass in a tracer object directly or default to the opentracing.tracer global tracer:

from dbapi_opentracing import ConnectionTracing
import db_api_compatible_client

opentracing_tracer = ## some OpenTracing tracer implementation
connection = db_api_compatible_client.connect(...)
tracing = ConnectionTracing(connection, opentracing_tracer)

or

from dbapi_opentracing import ConnectionTracing
import db_api_compatible_client
import opentracing

opentracing.tracer = ## some OpenTracing tracer implementation
connection = db_api_compatible_client.connect(...)
tracing = ConnectionTracing(connection)

Trace All Cursor Commands

from dbapi_opentracing import ConnectionTracing
import db_api_compatible_client

opentracing_tracer = ## some OpenTracing tracer implementation
connection = db_api_compatible_client.connect(...)
tracing = ConnectionTracing(connection, opentracing_tracer,
                            span_tags={'Custom': 'Tag'})  # span_tags will be used for all generated spans

with tracing.cursor() as cursor:
    cursor.execute('SELECT * FROM TABLE')
    vals = cursor.fetchall()
    cursor.executemany('INSERT INTO TABLE VALUES (%s, %s)',
                       [('one', 'two'), ('three', 'four')])
    cursor.callproc('MyStoredProcedure')
tracing.commit()

Trace All Connection Commands (implicit commit() and rollback())

from dbapi_opentracing import ConnectionTracing
import db_api_compatible_client

opentracing_tracer = ## some OpenTracing tracer implementation
connection = db_api_compatible_client.connect(...)
tracing = ConnectionTracing(connection, opentracing_tracer)

with tracing as cursor:  # If DB API client supports Connection as context manager
    cursor.execute('SELECT * FROM TABLE')
    vals = cursor.fetchall()
    cursor.executemany('INSERT INTO TABLE VALUES (%s, %s)',
                       [('one', 'two'), ('three', 'four')])
    cursor.callproc('MyStoredProcedure')

Trace Individual Commands

from dbapi_opentracing import Cursor
import db_api_compatible_client

opentracing_tracer = ## some OpenTracing tracer implementation
connection = db_api_compatible_client.connect(...)

with connection.cursor() as cursor:
    # Traced query
    Cursor(cursor, opentracing_tracer).execute('SELECT * FROM TABLE_ONE')
    # Traced query using opentracing.tracer default
    Cursor(cursor).execute('SELECT * FROM TABLE_TWO')
    # Traced query with custom tags
    Cursor(cursor, span_tags={'Query': 'Tag', 'Another': 'Tag'}).execute('SELECT * FROM TABLE_THREE')
    # Untraced command
    cursor.executemany('INSERT INTO TABLE VALUES (%s, %s)',
                       [('one', 'two'), ('three', 'four')])

Further Information

If you’re interested in learning more about the OpenTracing standard, please visit opentracing.io or join the mailing list. If you would like to implement OpenTracing in your project and need help, feel free to send us a note at community@opentracing.io.

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

DBAPI-OpenTracing-0.0.1.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file DBAPI-OpenTracing-0.0.1.tar.gz.

File metadata

  • Download URL: DBAPI-OpenTracing-0.0.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.15

File hashes

Hashes for DBAPI-OpenTracing-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d6719c4cb9e066eeb19f22b983f80d2610b964754470048bacdc59d3af50641d
MD5 3f6ff502a3d40b19e0def9041b16c7a2
BLAKE2b-256 ff5b5f84c8aa2dd7dd877220aee263ad7c7bce0e31febe934b0555868c8f8d65

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