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 @log-drop/api package.
Add the package
The package is published on npm. It has no dependencies and includes its TypeScript types.
npm install @log-drop/api@0.1.0-alpha1It works in CommonJS and ECMAScript modules (require('@log-drop/api') or import { LogDrop } from '@log-drop/api').
Add a span
A custom span is a span your own code starts and ends, around an operation the agent cannot see on its own. Wrap the operation in inSpan and give the span a name:
import { LogDrop } from '@log-drop/api';
const total = await LogDrop.tracing().inSpan('checkout.price', async (span) => {
span.attribute('order.type', 'delivery'); // custom keys need LOGDROP_ALLOWED_ATTRIBUTE_KEYS=order.type
return pricing.total(order);
});inSpan makes the span current while your code runs, so the calls inside it (database, HTTP, messaging) appear under it in the same trace, also after an await. It ends the span when your function returns or, for an async function, when its promise settles, and records the error type when it throws or rejects. The result or the error is passed on unchanged.
For work that does not fit in one function, start the span yourself and end it:
const span = LogDrop.tracing().startSpan('import.file', { kind: 'CONSUMER' });
try {
await span.run(() => importFile(path)); // spans started inside are its children
span.success();
} catch (error) {
span.recordException(error);
throw error;
} finally {
span.end();
}Attributes and events
Attributes are key-value details that you add to a span, such as order.type. Events mark something that happened while the span ran.
- 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, numbers 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'). recordExceptionrecords only the error type, never its message or stack trace.- Span names are at most 256 characters and not empty.
Other calls
Besides spans, the package has calls that work with the running agent and the current span: send pending spans now, read the export counters, switch the ingest key, check that the agent is attached, or carry the current span into code the agent does not trace.
| Call | What it does |
|---|---|
LogDrop.flush(ms) | Sends the spans that ended before the call, for example before a batch job exits. Resolves false when it did not finish in time, an export failed, or no agent is attached. The timeout is at most 60,000 ms |
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 the agent is attached |
LogDrop.updateIngestCredential(key) | Uses this key for later requests, without a restart, until the key file changes again |
LogDrop.isAgentAttached() | Whether the LogDrop Node.js agent runs in this process |
tracing.currentSpanContext() | The trace and span ID of the current span, or null |
tracing.captureContext() | Keeps the current span for later: run(fn) or wrap(fn) runs code with it as current, for example in a callback of a library the agent does not trace |
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: inSpan still runs your code and returns its result, flush resolves false, exportStatus() reports that no agent is attached, and updateIngestCredential throws an Error.
.png)