Honeycomb with libhoney
If your service sends libhoney events and sets their trace fields itself,
spanslice/libhoney applies your SpanSlice rules to them. Each event you start becomes an open span for the SDK. Rules
drop, sample, rename and redact it like any OpenTelemetry span, and stuck-span checks watch it while it’s open. Kept
events still go out through libhoney.
Set up
Section titled “Set up”npm install spanslice libhoneymakeSpanSliceLibhoney takes your Libhoney class and the handle that spanSliceInit returns. It returns a subclass
of your class that takes the same options:
import Libhoney from 'libhoney'import { spanSliceInit } from 'spanslice'import { makeSpanSliceLibhoney } from 'spanslice/libhoney'
export const spanslice = spanSliceInit({ service: 'api' })
const SpanSliceLibhoney = makeSpanSliceLibhoney(Libhoney, { spanslice })
export const hny = new SpanSliceLibhoney({ writeKey: process.env.HONEYCOMB_WRITE_KEY, dataset: 'api',})SpanSlice runs on the class you pass. A patched copy of libhoney, another version, or your own subclass keeps working.
spanslice/libhoney doesn’t load libhoney itself.
Set SPANSLICE_API_KEY to the environment’s key from Settings › API keys, and use a service that’s deployed to
that environment. In this setup, the SDK only fetches rules. Events still go to the writeKey and dataset you pass to
libhoney, not to the ingest key and dataset under Project settings › Honeycomb.
Start and send events
Section titled “Start and send events”Set an event’s trace fields, call hny.start(ev) when the work begins, and call ev.send() when it’s done:
import { randomUUID } from 'node:crypto'
async function loadReport(traceId: string, parentId: string, id: string) { const ev = hny.newEvent() ev.add({ 'trace.trace_id': traceId, 'trace.parent_id': parentId, 'trace.span_id': randomUUID(), name: 'reports.load', 'report.id': id, }) hny.start(ev) try { const report = await db.reports.find(id) ev.addField('report.rows', report.rows.length) return report } catch (err) { ev.addField('error', String(err)) throw err } finally { ev.send() }}- Call
start()oncetrace.trace_idandtrace.span_idare set. Settrace.parent_idtoo if the span has a parent. Ifstart()returnsfalse, it didn’t take the event, and the event goes out as a plain libhoney send. - Call
start()when the work begins, not when it ends. An event built after its work is done, withtimestampandduration_msalready set, still gets the rules. It isn’t a synchronous span, however soon you send it. Its created-stage rules run duringstart(), so they see only the fields the event has by then. Stuck-span checks can’t catch it, because it ends right after it starts. - Events from
hny.newBuilder()work too. Start them withhny.start(ev). ev.sendPresampled()ends the span the same way. Use it for events your app already sampled: theirsampleRatecounts toward the weight Honeycomb gives them (see What changes on the event).
Field names
Section titled “Field names”To name the ID, duration and error fields in your events differently, pass fields. These are the defaults:
const SpanSliceLibhoney = makeSpanSliceLibhoney(Libhoney, { spanslice, fields: { traceId: 'trace.trace_id', spanId: 'trace.span_id', parentId: 'trace.parent_id', durationMs: 'duration_ms', error: 'error', },})error also takes a list, such as ['error', 'exception', 'error.message']. Any of them set makes the span an error,
and the first that holds a string becomes its message. All of them still go out as fields.
SpanSlice reads the IDs and duration from those fields. It writes kept events, stuck-span snapshots and roll-ups in the same names. If different datasets use different names, make one class for each.
How fields become a span
Section titled “How fields become a span”The table uses the default field names. To change them, see Field names.
| Event field | Span |
|---|---|
trace.trace_id, trace.span_id |
the trace and span IDs |
trace.parent_id |
the parent span |
name |
the span name |
timestamp |
the start time; set to now by start() if it’s empty |
duration_ms |
the end time, set from the clock at send() if it’s empty |
error |
any truthy value makes the span an error (a string is its message) |
span.kind |
the span kind: server, client, producer, consumer, or internal |
| anything else | a span attribute (objects and arrays of mixed types as JSON) |
Spans carry the instrumentation library spanslice/libhoney, so a rule can match them. Every field is a span attribute,
so match fields with Span Attribute. The rule editor hides Resource Attribute for a project that sends only
libhoney events. Their resource is the SpanSlice SDK’s own process, which never reaches Honeycomb.
SpanSlice hashes IDs that aren’t OpenTelemetry-shaped hex, and every process hashes an ID the same way. So a trace that crosses services still samples alike. A UUID used as a trace ID only loses its dashes, and isn’t hashed. Honeycomb still receives your own IDs.
What changes on the event
Section titled “What changes on the event”send() doesn’t send a started event right away. SpanSlice holds the event until its trace is decided, which happens
when the trace’s root ends in this process. Then SpanSlice sends the event through libhoney, with the rules’ changes
written onto it:
- Renamed, set, redacted and removed fields. A field the rules left alone keeps its original value, including objects.
trace.parent_id, when a dropped span’s children move up to its parent. The value is your own ID.sampleRate: the rate your rules sampled the trace at. A started event skips libhoney’s own sampling, and thesampleRateset on the libhoney client or a builder is ignored. Only an event sent withsendPresampled()keeps its own rate, multiplied by the rules’ rate, since your app had already sampled it.
Stuck-span snapshots and Roll Up Spans results are new events, each with a span ID
that the SDK makes. SpanSlice builds them from what the rules saw, and weights them by the rules’ rate. A field you add after start() isn’t in a snapshot
until the event ends.
Things to know
Section titled “Things to know”- Synchronous spans: an event that’s started and sent with no
awaitin between is a synchronous span. By default, SpanSlice drops it, like any other synchronous span. An event that already has itsduration_mswhen you callstart()isn’t one. - Shut down in order: call
await spanslice.shutdown()beforeawait hny.flush(). The SDK then sends the traces it still holds before libhoney flushes. - SDK off: without an API key or a service, or after shutdown,
start()returnsfalse, and every event goes out untouched. - OpenTelemetry in the same process: OpenTelemetry spans go out through the SDK’s exporter, as described in Honeycomb with OpenTelemetry. libhoney events never go through that exporter.