Skip to content

Tracing

Import from loggily (main entry) or loggily/tracing.

ID Format

typescript
function setIdFormat(format: IdFormat): void
function getIdFormat(): IdFormat

type IdFormat = "simple" | "w3c"

Switch between lightweight and W3C-compatible ID formats.

FormatSpan IDTrace IDUse Case
"simple"sp_1, sp_2tr_1, tr_2Development, testing
"w3c"16-char hex32-char hexDistributed tracing
typescript
setIdFormat("w3c")
const span = log.span("request")
// span.spanData.id    → "a1b2c3d4e5f6a7b8"
// span.spanData.traceId → "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6"

traceparent

typescript
function traceparent(spanData: SpanData, options?: TraceparentOptions): string

interface TraceparentOptions {
  /** Whether this span is sampled. Defaults to true. */
  sampled?: boolean
}

Format a W3C traceparent header.

typescript
const span = log.span("http-request")
const header = traceparent(span.spanData)
// → "00-a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6-1a2b3c4d5e6f7a8b-01"

fetch(url, { headers: { traceparent: header } })

// Mark as not sampled
traceparent(span.spanData, { sampled: false })
// → "00-...-...-00"

Works with both "simple" and "w3c" ID formats. Simple IDs are zero-padded to spec length.

Sampling

typescript
function setSampleRate(rate: number): void
function getSampleRate(): number

Head-based sampling: the decision is made when a trace starts and inherited by all child spans.

typescript
setSampleRate(0.1) // Sample 10% of traces
setSampleRate(1.0) // Sample everything (default)
setSampleRate(0.0) // Sample nothing
  • Rate must be between 0.0 and 1.0 (throws otherwise)
  • Child spans always inherit the parent's sampling decision
  • Unsampled spans still have valid spanData -- only output is suppressed

Released under the MIT License.