Custom Spans
Add spans for business operations with the optional LogDrop API package.
Most services need no code: the agent traces incoming requests and calls to databases, caches, brokers and other services on its own. For business operations you want to see as their own step in a trace, add the optional logdrop-api package.
Add the package
logdrop-api gives your code an API to create spans. The agent running in your service records and sends them.
The package is published on PyPI. It has no dependencies, includes type hints and runs on Python 3.8 or newer.
pip install logdrop-api==0.1.0a1Add a span
A custom span is a span your code starts and ends around a step of its own work, such as pricing an order. That step then shows in the trace as its own row, with its duration.
from logdrop_api import LogDrop
tracing = LogDrop.tracing()
with tracing.span("checkout.price") as span:
span.attribute("order.type", "delivery") # custom keys need LOGDROP_ALLOWED_ATTRIBUTE_KEYS=order.type
total = pricing.total(order)The span is current inside the with block, so the calls inside it (database, HTTP, messaging) appear under it in the same trace, also after an await (async with works too). It ends when the block ends. An exception raised in the block is recorded with its type only and passed on unchanged.
For a whole function, use the decorator. It works for functions and coroutine functions:
@tracing.traced("checkout.validate")
async def validate(order):
...For work that does not fit in one block, start the span yourself and end it:
span = tracing.start_span("import.file", kind="CONSUMER")
try:
span.run(lambda: import_file(path)) # spans started inside are its children
span.success()
except Exception as error:
span.record_exception(error)
raise
finally:
span.end()tracing.in_span(name, work) and await tracing.in_span_async(name, work) run a function that receives the span.
Attributes and events
Attributes add detail to a span as key and value pairs, such as order.type with the value delivery. Events mark a named moment inside a span, such as payment.authorized. What you add follows these rules:
- A custom attribute is sent only when its key is listed in
LOGDROP_ALLOWED_ATTRIBUTE_KEYS(comma-separated, at most 128 keys). A key starts with a letter and has at most 128 letters, digits,_,.or-. - Keys that contain
authorization,cookie,token,secret,password,credential,api_key,apikeyorapi-keyare always dropped, even when listed. - Values are strings, integers, floats or booleans. Text is cut to
LOGDROP_MAX_STRING_LENGTHcharacters (256 by default). - Use constant event names, without request or user data:
span.event("payment.authorized"). record_exceptionrecords only the exception type, never its message or traceback.- Span names are at most 256 characters and not empty.
Other calls
The package also has calls to send pending spans, check the agent's status, switch the ingest key and work with the current span. Use them, for example, to send a batch job's spans before it exits.
| Call | What it does |
|---|---|
LogDrop.flush(seconds) | Sends the spans that ended before the call, for example before a batch job exits. Returns False when it did not finish in time, an export failed, or no agent is attached. The timeout is more than 0 and at most 60 seconds |
LogDrop.export_status() | Counts of exported, failed, rejected (refused as invalid, or dropped before sending because LogDrop would refuse them), pending, refused (over the active-span limit) and active spans, and whether the agent is attached |
LogDrop.update_ingest_credential(key) | Uses this key for later requests, without a restart, until the key file changes again |
LogDrop.is_agent_attached() | Whether the LogDrop Python agent runs in this process |
tracing.current_span_context() | The trace and span ID of the current span, or None |
tracing.capture_context() | Keeps the current span for later: run(fn) or wrap(fn) runs code with it as current, for example in a thread pool or a callback |
start_span(name, kind=..., links=[...]) | kind is INTERNAL (default), SERVER, CLIENT, PRODUCER or CONSUMER; links points at related spans by their context |
Without the agent
The package sends nothing to LogDrop by itself. Without the agent every call is safe and records nothing: with tracing.span(...) and the decorator still run your code, flush returns False, export_status() reports that no agent is attached, and update_ingest_credential raises RuntimeError.
.png)