npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@worknice/instrumentation

v1.13.0

Published

Shared instrumentation management for Worknice applications

Downloads

1,363

Readme

@worknice/instrumentation

Shared instrumentation and correlation ID management for Worknice applications.

Features

  • 🔍 Correlation ID Tracking: Request tracing across async boundaries using Node.js AsyncLocalStorage
  • 📊 Next.js Instrumentation: Error reporting with Axiom
  • 🔄 Cross-Service Tracing: Track requests across services (Notebook, MYOB, Micropay, Basics)

Installation

This package is part of the Worknice monorepo and is automatically available to all apps:

pnpm add @worknice/instrumentation

Getting Started

App Instrumentation

For Next.js apps, create an instrumentation.ts file in the app root:

// apps/$app-name/instrumentation.ts
export { register, onRequestError } from "@worknice/instrumentation";
  • register() - Initialises instrumentation. Automatically called by Next.js when instrumentation is enabled.
  • onRequestError() - Handles and logs request errors with correlation tracking. Automatically called by Next.js on errors.

Correlation ID Usage

The package provides multiple ways to work with correlation IDs:

Getting the current correlation ID

import { getCorrelationId } from "@worknice/instrumentation";

const correlationId = getCorrelationId();

Running code with a specific correlation ID

import { runWithCorrelationId, getCorrelationId } from "@worknice/instrumentation";

await runWithCorrelationId("$correlationId", async () => {
  // All async operations here will have access to the correlation ID
  const id = getCorrelationId(); // Returns "$correlationId"
});

Getting or generating a correlation ID

import { getOrGenerateCorrelationId } from "@worknice/instrumentation";

const { correlationId, isNew } = getOrGenerateCorrelationId();

Extracting from HTTP headers

Extract correlation ID from HTTP headers in order:

  • x-request-id
  • x-correlation-id
  • request-id
  • correlation-id
import { extractCorrelationIdFromHeaders } from "@worknice/instrumentation";

const correlationId = extractCorrelationIdFromHeaders(request.headers);

Configuration

Environment Variables

The package supports the following environment variables:

  • AXIOM_DATASET: Axiom dataset name for error logging
  • AXIOM_TOKEN: Axiom API token for authentication
  • NODE_ENV: Environment name (development, staging, production)

When Axiom credentials are not provided, the package falls back to console logging.

Architecture

AsyncLocalStorage

The package uses Node.js AsyncLocalStorage to maintain correlation context across async boundaries without explicitly passing IDs through function calls. This allows correlation IDs to automatically flow through:

  • GraphQL operations
  • Database queries
  • API calls
  • Background jobs
  • Nested async operations

Development

Building

pnpm --filter=@worknice/instrumentation build

Development Mode

pnpm --filter=@worknice/instrumentation dev

Testing & Linting

pnpm --filter=@worknice/instrumentation test:lint
pnpm --filter=@worknice/instrumentation test:types
pnpm --filter=@worknice/instrumentation test:format

Use Cases

Cross-Service Request Tracing

When a request flows through multiple services (e.g., Notebook → MYOB → External API), the correlation ID helps trace the entire request chain:

In Notebook app:

import { getOrGenerateCorrelationId } from "@worknice/instrumentation";

const { correlationId } = getOrGenerateCorrelationId();
await fetch("https://myob-service/api/endpoint", {
  headers: {
    "x-correlation-id": correlationId,
  },
});

In MYOB service:

import { extractCorrelationIdFromHeaders, runWithCorrelationId } from "@worknice/instrumentation";

const correlationId = extractCorrelationIdFromHeaders(req.headers);
runWithCorrelationId(correlationId, async () => {
  // All operations here share the same correlation ID
  await processRequest();
});

Background Job Tracking

Track background jobs initiated from web requests:

In API route:

import { getCorrelationId } from "@worknice/instrumentation";

const correlationId = getCorrelationId();
await inngest.send({
  name: "process.data",
  data: { correlationId, ...payload },
});

In Inngest function:

import { runWithCorrelationId } from "@worknice/instrumentation";

export const processData = inngest.createFunction(
  { id: "process-data" },
  { event: "process.data" },
  async ({ event }) => {
    return runWithCorrelationId(event.data.correlationId, async () => {
      // Job execution with correlation tracking
    });
  }
);

Troubleshooting

Correlation ID Not Available

If getCorrelationId() returns undefined:

  1. Ensure you're within a correlation context (use runWithCorrelationId)
  2. Verify the context hasn't been lost (e.g., through setTimeout without binding)

Axiom Logs Not Appearing

  1. Verify AXIOM_DATASET and AXIOM_TOKEN environment variables are set
  2. Check console output on startup for "Axiom error logging enabled" message
  3. Ensure the Axiom dataset exists and token has write permissions