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 NuGet. It has no dependencies and targets .NET 8 or newer.
dotnet add package LogDrop.Api --version 0.1.0-alpha1Add 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.
using LogDrop.Api;
using (var span = LogDropTracing.StartActiveSpan("checkout.price"))
{
span.Attribute("order.type", "delivery"); // custom keys need LOGDROP_ALLOWED_ATTRIBUTE_KEYS=order.type
total = await pricing.TotalAsync(order);
}The span is current inside the using block, so the calls inside it (database, HTTP, messaging) appear under it in the same trace, also after an await. It ends when the block ends. An exception thrown in a using block is not recorded on its own: call span.RecordException(e) in a catch, or use InSpan.
InSpan and InSpanAsync run your code in a new span, end it when the code returns, and record an exception (its type only) before passing it on:
var receipt = await LogDropTracing.InSpanAsync("checkout.pay", span => payments.PayAsync(order));For work that does not fit in one block, start the span yourself and end it:
var span = LogDropTracing.StartSpan("import.file", LogDropSpanKind.Consumer);
try
{
span.Run(() => ImportFile(path)); // spans started inside are its children
span.Success();
}
catch (Exception e)
{
span.RecordException(e);
throw;
}
finally
{
span.End();
}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
string,long,doubleorbool. Text is cut toLOGDROP_MAX_STRING_LENGTHcharacters (256 by default). - Use constant event names, without request or user data:
span.Event("payment.authorized"). RecordExceptionrecords only the exception type, for exampleSystem.InvalidOperationException, never its message or stack trace.- 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 |
|---|---|
LogDropTracing.Flush(timeout) | 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 |
LogDropTracing.ExportStatus() | 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 |
LogDropTracing.UpdateIngestCredential(key) | Uses this key for later requests, without a restart, until the key file changes again |
LogDropTracing.IsAgentAttached | Whether the LogDrop .NET agent runs in this process |
LogDropTracing.CurrentSpanContext | The trace and span ID of the current span, or null |
StartActiveSpan(name, kind, links), StartSpan(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: spans and InSpan still run your code, Flush returns false, ExportStatus() reports that no agent is attached, and UpdateIngestCredential throws InvalidOperationException.
.png)