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

@tracegraph/trace-js

v0.3.1

Published

JavaScript/TypeScript instrumentation adapters for TraceGraph

Readme

@tracegraph/trace-js

JavaScript and TypeScript instrumentation adapters for TraceGraph. Provides everything needed to capture runtime events from Node.js applications and tests: Express middleware, manual function/method wrappers, automatic HTTP client patching, and CJS/ESM register hooks for zero-config auto-instrumentation.

What's in this package

| Export | Description | |--------|-------------| | traceExpress(options?) | Express middleware — captures http_request and http_response events, including method, path, status, sanitized headers and body | | traceFunction(name, fn) | Wraps any function to emit function_call events with timing and sanitized input/output | | traceMethod() | TypeScript decorator equivalent of traceFunction for class methods | | patchGlobalFetch() | Patches the global fetch to capture external_http_call events | | subscribeUndiciChannel() | Subscribes to the Node.js diagnostics channel for undici (the native fetch implementation) | | tracedAxios(instance?) | Wraps an axios instance to capture external_http_call events | | ChildEventWriter | Low-level JSONL event emitter used by adapters | | traceStorage / getContext / writeEvent / currentParentEventId | AsyncLocalStorage-based context used to correctly nest parent/child event IDs | | TRACEGRAPH_ENV | Helper to read TRACEGRAPH_ENABLED, TRACEGRAPH_RUN_DIR, TRACEGRAPH_TRACE_ID |

Register hooks (separate entry points)

| Entry point | Use case | |-------------|----------| | @tracegraph/trace-js/register | ESM --import hook — auto-instruments CJS modules loaded after registration | | @tracegraph/trace-js/register-cjs | CJS --require hook — same, for CommonJS contexts |

Installation

npm install -D @tracegraph/trace-js

Express is a peer dependency (optional — only needed if you use traceExpress):

npm install express

Usage

Express middleware

Add before your route handlers. The middleware captures the full request/response lifecycle as a pair of trace events.

import express from 'express';
import { traceExpress } from '@tracegraph/trace-js';

const app = express();
app.use(express.json());
app.use(traceExpress({
  sanitizerConfig: {
    redactKeys:      ['authorization', 'cardNumber'],
    maxStringLength: 500,
  },
}));

app.post('/invoices', invoiceHandler);

Events emitted per request:

  • http_request — on entry (method, path, sanitized headers + body)
  • http_response — on exit (status code, sanitized response body, duration)

Manual function wrappers (Level 2 capture)

import { traceFunction, traceMethod } from '@tracegraph/trace-js';

// Wrap any function
const tracedCreate = traceFunction('InvoiceService.create', originalCreate);
const result = await tracedCreate(invoiceData);

// Class method decorator
class InvoiceService {
  @traceMethod()
  async create(data: CreateInvoiceDto) {
    // ...
  }
}

Each call emits a function_call event with the function name, sanitized arguments, return value, timing, and parentEventId correctly set from the AsyncLocalStorage call context.

Outbound HTTP patching

import { patchGlobalFetch, tracedAxios } from '@tracegraph/trace-js';

// Patch global fetch (or undici)
patchGlobalFetch();

// Or wrap an axios instance
import axios from 'axios';
const client = tracedAxios(axios.create({ baseURL: 'https://api.example.com' }));

Outbound calls emit external_http_call events with the URL, method, status, and duration.

Auto-instrumentation via register hooks

For CJS modules, pass --require to Node.js:

node --require @tracegraph/trace-js/register-cjs src/app.js
# or via tracegraph run:
tracegraph run -- node --require @tracegraph/trace-js/register-cjs src/app.js

For ESM:

node --import @tracegraph/trace-js/register src/app.mjs

The register hooks patch require/import to automatically wrap exported functions from application modules (non-node_modules) with traceFunction.

Capture levels

This package contributes to the following capture levels:

| Level | How | |-------|-----| | 1 | traceExpress() middleware | | 2 | traceFunction() / traceMethod() / patchGlobalFetch() | | 3 | @tracegraph/trace-js/register-cjs register hook | | 4 | @tracegraph/trace-js/register ESM hook |

Level 5 (per-test isolation) is provided by @tracegraph/vitest and @tracegraph/jest.

How parent/child nesting works

All adapters use a shared AsyncLocalStorage context (traceStorage). When traceExpress receives a request, it creates a new context containing the current traceId and parentEventId. Every traceFunction call inside that request reads from the context to set parentEventId on its own event — even across await boundaries and Promise.all concurrency.