Uptrace exporter for OpenTelemetry
Project description
Uptrace Python exporter for OpenTelemetry
Installation
pip install uptrace
Introduction
uptrace-go is an exporter for OpenTelemetry that sends your traces/spans and metrics to Uptrace.dev. Briefly the process is the following:
- OpenTelemetry API is used to instrument your application with spans and metrics.
- OpenTelemetry SDK and this exporter send collected information to Uptrace.dev.
- Uptrace.dev uses that information to help you pinpoint failures and find performance bottlenecks.
Instrumenting code
You instrument your application by wrapping potentially interesting operations with spans. Each span has:
- an operation name;
- a start time and end time;
- a set of key/value attributes containing data about the operation;
- a set of timed events representing events, errors, logs, etc.
You create spans using a tracer:
from opentelemetry.trace import get_tracer
# Create a named tracer using your module name as an identifier.
tracer = get_tracer(__name__, __version__)
To create a span and set it as the current span:
with tracer.start_as_current_span("operation-name") as span:
do_some_work()
Alternatively you can use start_span
which does roughly the same:
span = tracer.start_span(name)
with tracer.use_span(span, end_on_exit=True):
do_some_work()
To get an existing span from the tracer context:
span = tracer.get_current_span()
Once you have a span you can start adding attributes:
span.set_attribute("enduser.id", "123")
span.set_attribute("enduser.role", "admin")
or events:
span.add_event("log", {
"log.severity": "error",
"log.message": "User not found",
"enduser.id": "123",
})
Span exporter
Span exporter exports spans to Uptrace.dev backend. To configure span exporter add the following code to your main file (for Django it is manage.py):
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
import uptrace
# The preferred tracer implementation must be set, as the opentelemetry-api
# defines the interface with a no-op implementation.
trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(uptrace.trace.span_processor(
dsn="" # copy your project DSN here or use UPTRACE_DSN env var
))
Instrumenting Django
Install Django instrumentation extension:
pip install opentelemetry-ext-django
Set environment variables:
# Enable instrumentation.
export OPENTELEMETRY_PYTHON_DJANGO_INSTRUMENT=True
# Django settings that will be used to install OpenTelemetry middleware.
export DJANGO_SETTINGS_MODULE=app_name.settings
Edit manage.py
:
from opentelemetry.ext.django import DjangoInstrumentor
if __name__ == "__main__":
# Instrument Django by adding middleware etc.
DjangoInstrumentor().instrument()
...
Run the server:
export OPENTELEMETRY_PYTHON_DJANGO_INSTRUMENT=True
export DJANGO_SETTINGS_MODULE=app_name.settings
./manage.py runserver
Instrumenting PostgreSQL psycopg2
Install psycopg instrumentation extension:
pip install opentelemetry-ext-psycopg2
Update your main file (for Django it is manage.py):
from opentelemetry.ext.psycopg2 import Psycopg2Instrumentor
if __name__ == "__main__":
Psycopg2Instrumentor().instrument()
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file uptrace-0.1.0b3.tar.gz
.
File metadata
- Download URL: uptrace-0.1.0b3.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15f34c2c52438bf2a8d2395a2a46f27c166f8da17751b7a13d7bcbf33f5e92b5 |
|
MD5 | 29c6e0097e93e1eff434c5ccd4de2949 |
|
BLAKE2b-256 | 1ec9ca8fe3b2b23f98cfc4b5bec5503d342d3cdc72c03f8563ae1c8dfefb5908 |