Custom Spans
Add spans for business operations with the LogDrop Go package.
The libraries you wrap already record incoming requests and calls to databases, caches, brokers and other services. For business operations you want to see as their own step in a trace, add a custom span with the same logdrop package.
Add a span
Start a span where the operation begins, pass its context to the calls inside the operation, and end the span when the operation ends:
ctx, span := logdrop.StartSpan(ctx, "checkout.price")
defer span.End()
span.Attribute("order.type", "delivery") // custom keys need LOGDROP_ALLOWED_ATTRIBUTE_KEYS=order.type
total, err := pricing.Total(ctx, order) // calls that get ctx appear under the span
if err != nil {
span.RecordError(err)
return err
}
span.Success()StartSpan returns a context that carries the new span: pass it to the calls inside the operation (database, HTTP, messaging) and they appear under it in the same trace. The span ends when you call End.
InSpan runs your function in a new span, ends it when the function returns, records a returned error (its type only) and, on a panic, records it and panics again:
err := logdrop.InSpan(ctx, "checkout.pay", func(ctx context.Context, span *logdrop.Span) error {
return payments.Pay(ctx, order)
})For a span of another kind, or one that points at related spans:
ctx, span := logdrop.StartSpan(ctx, "import.file",
logdrop.WithKind(logdrop.KindConsumer),
logdrop.WithLinks(batchContext))Kind is KindInternal (the default), KindServer, KindClient, KindProducer or KindConsumer.
Attributes and events
An attribute is a key and value on a span, such as order.type = delivery. An event marks a named moment inside a span, such as payment.authorized. These rules apply to both, and to a span's errors, status and name:
- 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 a
string, abool, an integer or a float; other values are ignored. Text is cut toLOGDROP_MAX_STRING_LENGTHcharacters (256 by default). - Use constant event names, without request or user data:
span.Event("payment.authorized"). RecordErrorrecords only the error's type, for example*fs.PathError, never its message.SuccessandErrorset the span's status without an error.- Span names are at most 256 characters and not empty; a span with another name records nothing.
Other calls
Besides spans, the package has calls to send queued spans, read its state and switch the ingest key without a restart. A batch job, for example, calls logdrop.Flush before it exits.
| Call | What it does |
|---|---|
logdrop.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 tracing does not run. The timeout is more than 0 and at most one minute |
logdrop.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 tracing runs (AgentAttached) |
logdrop.UpdateIngestCredential(key) | Uses this key for later requests, without a restart, until the key file changes again. Returns ErrNotAttached, ErrInvalidCredential or ErrClosed when it cannot |
logdrop.IsAgentAttached() | Whether tracing runs in this process |
logdrop.CurrentSpanContext(ctx) | The trace and span ID of the span in ctx, and whether there is one |
span.Context() | The trace and span ID of this span, for example to link to it |
logdrop.Shutdown() | Sends the spans still queued, for at most 10 seconds, and stops tracing. The function logdrop.Start returns is the same |
Without tracing
Without settings, or before logdrop.Start, every call is safe and records nothing: spans and InSpan still run your code, Flush returns false, ExportStatus() reports that tracing does not run, and UpdateIngestCredential returns ErrNotAttached.
.png)