Collection of Python logging and tracing tools
Project description
🪵
Troncos
Collection of Python logging and tracing tools
Etymology
"Troncos" is the plural of the spanish word "Tronco", which translates to "trunk" or "log".
Installation
# With pip
pip install troncos
Tracing
Troncos is designed to take advantage of ddtrace made by DataDog.
The ddtrace docs can be found here.
Best practices for traces is a good guide to get started.
Span vs resource attributes
- A
span attributeis a key/value pair that provides context for its span. - A
resource attributeis a key/value pair that describes the context of how the span was collected.
For more information, read the Attribute and Resource sections in the OpenTelemetry specification.
Enabling the tracer
Configure ddtrace as usual and run configure_tracer to send spans to Tempo.
This is typically done in settings.py of you want to profile a Django application,
or in __init__.py in the root project package.
TRACE_HOST is usually the host IP of the K8s pod, TRACE_PORT is usually 4318
when the Grafana agent is used to collect spans using HTTP.
import ddtrace
from ddtrace.trace import tracer
from troncos.tracing import configure_tracer, Exporter
# Configure tracer as described in the ddtrace docs.
ddtrace.config.django["service_name"] = 'SERVICE_NAME'
# These are added as span attributes
tracer.set_tags(
tags={
"key": "value",
}
)
# Patch third-party modules
ddtrace.patch(django=True)
# Configure the ddtrace tracer to send traces to Tempo.
configure_tracer(
service_name='SERVICE_NAME',
exporter=Exporter(
host = "127.0.0.1", # Usually obtained from env variables.
),
resource_attributes={
"app": "app",
"component": "component",
"role": "role",
"tenant": "tenant",
"owner": "owner",
"version": "version",
},
enabled=True,
)
ddtrace also uses env variables to configure the service name, environment and version etc.
Add the following environment variables to your application.
DD_ENV="{{ environment }}"
DD_SERVICE="{{ app }}"
DD_VERSION="{{ version }}"
# tracecontext/w3c is usually used to propagate distributed traces across services.
DD_TRACE_PROPAGATION_STYLE_EXTRACT="tracecontext"
DD_TRACE_PROPAGATION_STYLE_INJECT="tracecontext"
Debugging during development
By setting the environment variable OTEL_TRACE_DEBUG=True you will enable traces
to be printed to stdout via the ConsoleSpanExporter as well as through http/grpc.
Also specifying OTEL_TRACE_DEBUG_FILE=/some/file/path will output traces to the
specified file path instead of the console/stdout.
Using the GRPC span exporter
Using the GRPC span exporter gives you significant performance gains. If you are running a critical service with high load in production, we recommend using GRPC.
The port is usually 4317 when the Grafana agent is used to collect
spans using GRPC.
poetry add troncos -E grpc
or
[tool.poetry.dependencies]
troncos = {version="?", extras = ["grpc"]}
from troncos.tracing import configure_tracer, Exporter
configure_tracer(
service_name='SERVICE_NAME',
exporter=Exporter(
host = "127.0.0.1", # Usually obtained from env variables.
port = "4317",
),
enabled=True,
)
Setting headers for the exporter
from troncos.tracing import configure_tracer, Exporter
configure_tracer(
service_name='SERVICE_NAME',
exporter=Exporter(
host = "127.0.0.1", # Usually obtained from env variables.
headers={"my": "header"},
),
enabled=True,
)
Instrument your code
Manual instrumentation of your code is described in the ddtrace docs.
Add tracing context to your log
Adding the tracing context to your log makes it easier to find relevant traces in Grafana. Troncos include a Structlog processor designed to do this.
import structlog
from troncos.contrib.structlog.processors import trace_injection_processor
structlog.configure(
processors=[
trace_injection_processor,
],
)
Logging of major actions in your application
Finding relevant traces in Grafana can be difficult. One way to make finding the relevant traces easier it to log every major action in your application. This typically means logging every incoming HTTP request to your server or every task executed by your Celery worker.
The structlog processor above needs to be enabled before logging your major actions is relevant.
ASGI middleware
Log ASGI requests.
from starlette.applications import Starlette
from troncos.contrib.asgi.logging.middleware import AsgiLoggingMiddleware
application = AsgiLoggingMiddleware(Starlette())
Django middleware
Log Django requests. This is not needed if you run Django with ASGI and use the ASGI middleware.
MIDDLEWARE = [
"troncos.contrib.django.logging.middleware.DjangoLoggingMiddleware",
...
]
Celery signals
` Log Celery tasks. Run the code bellow when you configure Celery.
from troncos.contrib.celery.logging.signals import (
connect_troncos_logging_celery_signals,
)
connect_troncos_logging_celery_signals()
Logging
Troncos is not designed to take control over your logger. But, we do include logging related tools to make instrumenting your code easier.
Configure Structlog
Troncos contains a helper method that lets you configure Structlog.
First, run poetry add structlog to install structlog in your project.
You can now replace your existing logger config with
from troncos.contrib.structlog import configure_structlog
configure_structlog(format="json", level="INFO")
Adding tracing context to your log
Troncos has a Structlog processor that can be used to add the span_id and trace_id
properties to your log. More information can be found in the Tracing
section in this document. This is used by the configure_structlog helper method
by default.
Request logging middleware
Finding the relevant traces in Tempo and Grafana can be difficult. The request logging middleware exist to make it easier to connect HTTP requests to traces. More information can be found in the Tracing section in this document.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file troncos-8.0.1.tar.gz.
File metadata
- Download URL: troncos-8.0.1.tar.gz
- Upload date:
- Size: 18.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.5 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
099df391c7701652d1cea946f56480c2fc044a8f329bde5a18fc89730fed581f
|
|
| MD5 |
40abc075cfa4ab4206887c5fb394b63e
|
|
| BLAKE2b-256 |
7600b2ad96ab0e1dd3be78fed131480730885ddcf3a23d29876f701adb70b743
|
File details
Details for the file troncos-8.0.1-py3-none-any.whl.
File metadata
- Download URL: troncos-8.0.1-py3-none-any.whl
- Upload date:
- Size: 20.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.5 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f000a43bacca38cc2df2cfadd498c194a2475a8244167ff89bc2d212d425b23
|
|
| MD5 |
a826fe443cd25ec1fa2516b6d322d5f8
|
|
| BLAKE2b-256 |
336fbfe8e387753c075c886ba10249a7b0316f22a84bf244d66dd194f14bf272
|