Skip to content

Install the Node SDK

The spanslice package sets up OpenTelemetry for Node. It fetches the rules your project defines in SpanSlice, applies them to every span in your process, and picks up changes without a restart.

Terminal window
npm install spanslice

Each runtime environment (production, staging, …) has its own API keys. Create them under your project’s Settings › API keys in the app. SpanSlice shows a key once, when you create it, and keeps only its hash. Set the key as SPANSLICE_API_KEY wherever that environment runs. Without a key, the SDK stays off.

To replace a key, create a new one, deploy it, and then revoke the old one.

Create a file that starts the SDK, and load it with --import. Instrumentations can then patch libraries before your code imports them:

src/instrument.ts
import { spanSliceInit } from 'spanslice'
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'
const telemetry = spanSliceInit({
service: 'api', // the service in SpanSlice; also service.name
instrumentations: [new HttpInstrumentation()],
})
await telemetry.ready
Terminal window
node --import ./src/instrument.ts server.js

service must name a service deployed to the API key’s environment. Otherwise, the SDK stays off.

ready resolves when the first config applies. If the API can’t be reached, it resolves after readyTimeoutMs (5 seconds by default) instead. Until the first config arrives, the SDK holds spans. It doesn’t send them unfiltered.

Option / env Default Meaning
apiKey / SPANSLICE_API_KEY — The environment’s ss_… key.
service OTEL_SERVICE_NAME The service’s slug or name in SpanSlice.
exporter / SPANSLICE_EXPORTER remote remote (where the project sends data), otlp (your own collector), console, or none.
readyTimeoutMs 5000 How long ready waits for the first config.
apiUrl / SPANSLICE_API_URL https://api.spanslice.com Where the SDK fetches its config.
deployedSha / SPANSLICE_DEPLOYED_SHA your host’s commit variable The commit this process runs, shown per process in Live Tail.
repositoryUrl / SPANSLICE_REPOSITORY_URL — The repository deployedSha is in, so Live Tail links to the commit.

On Render, Vercel, Railway and Heroku, deployedSha defaults to the commit the platform sets (RENDER_GIT_COMMIT, VERCEL_GIT_COMMIT_SHA, RAILWAY_GIT_COMMIT_SHA, HEROKU_BUILD_COMMIT). You only need to set repositoryUrl, or set the service’s Repository on its page instead. The SDK puts both values on the resource as vcs.ref.head.revision and vcs.repository.url.full, so your spans and metrics carry the release too.

With the default remote exporter, SpanSlice tells the SDK where to send data, such as your Honeycomb environment. Your code names no vendor. Spans go straight from your process to that backend. They don’t pass through SpanSlice.

Spans are sent in batches. To change how, pass batch:

spanSliceInit({
service: 'api',
batch: { scheduledDelayMillis: 500, maxQueueSize: 40_000 },
})
Setting Default Env
scheduledDelayMillis 2000 OTEL_BSP_SCHEDULE_DELAY
exportTimeoutMillis 10000 OTEL_BSP_EXPORT_TIMEOUT
maxQueueSize 20000 OTEL_BSP_MAX_QUEUE_SIZE
maxExportBatchSize 2048 OTEL_BSP_MAX_EXPORT_BATCH_SIZE

A setting passed in batch wins over its variable. The queue holds four whole traces, because a kept trace is sent all at once. Spans that arrive while it’s full are dropped and counted as queue_full.

To run a span processor of your own, pass spanProcessors. It’s given the processor that exports and returns the processors kept spans go to:

spanSliceInit({
service: 'api',
spanProcessors: (exporting) => [new EnhancedSpanProcessor(exporting)],
})

Their onStart and onEnding run for every span before your rules do, so rules can match attributes they set. onEnd gets only the spans your rules keep. While the SDK is off, spanProcessors isn’t called.

Cron jobs, setInterval loops and scripts aren’t started by a request, so nothing opens a trace for them. newTrace(name, fn, options?) wraps fn so that each call runs in a span named name and starts a new trace:

import { CronJob } from 'cron'
import { newTrace } from 'spanslice'
new CronJob(
'*/2 * * * *',
newTrace('cron.cleanup', async () => {
await deleteExpiredSessions()
}),
null,
true,
)

The span is always a root, even if another span is active when the job runs. A timer keeps the context it was scheduled in. If you create the job inside a span, a plain span around each tick would make every tick a child of that one, and all ticks would share its trace.

The span is active while fn runs, so spans started inside it are its children. It ends when fn returns or its promise settles. If fn throws or rejects, the span records the error with status ERROR, and the error continues to your code. this and the arguments reach fn unchanged. options.attributes and options.kind set the span’s attributes and kind.

Give each job one fixed name, such as cron.cleanup. Don’t put a date or an id in it: rules match on the name. Spans from newTrace have the instrumentation scope spanslice. While the SDK is off, fn runs as written.

The SDK prints only when it can’t do its job: a service it can’t find, a rejected API key, another tracer provider registered first, an environment with nowhere to export. Everything else is logged with debug, under one namespace per area:

Terminal window
DEBUG=spanslice:* node --import ./src/instrument.ts server.js
Namespace Logs
spanslice:sdk Startup, shutdown, and errors thrown while deciding spans
spanslice:config Each config applied, and failed fetches
spanslice:rules Rules skipped because they don’t compile
spanslice:live The live connection
spanslice:tail Live Tail sessions
spanslice:archive The span archive and its uploads
spanslice:alerts Alerts that couldn’t be sent
spanslice:instrumentations Instrumentations switched on, off or reconfigured
spanslice:metrics Span metrics and metrics settings
spanslice:spans One line per kept and dropped span, with the rules involved

debug: true (or SPANSLICE_DEBUG=1) turns on all of them. To send everything to your own logger instead, pass logger: { error, warn, info }. Without error, those messages go to warn.

Next: write your first rule.