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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@uselemma/tracing

v0.5.0

Published

OpenTelemetry-based tracing module for Lemma

Readme

@uselemma/tracing

Utilities for OpenTelemetry-based tracing and prompt management.

Installation

npm install @uselemma/tracing

Components

MemorySpanExporter

A custom OpenTelemetry span exporter that stores spans in memory for programmatic access. Useful for testing, debugging, or capturing trace data for custom processing.

Usage

import { NodeSDK } from "@opentelemetry/sdk-node";
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { MemorySpanExporter } from "@uselemma/tracing";

// Create and configure the exporter
const memoryExporter = new MemorySpanExporter();

const sdk = new NodeSDK({
  spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});

sdk.start();

// Later, retrieve spans
const allSpans = memoryExporter.getSpans();
const spansAsDicts = memoryExporter.getSpansAsDicts();
const traceSpans = memoryExporter.getSpansByTraceId("your-trace-id");

// Clear memory when needed
memoryExporter.clear();

Methods

  • getSpans(): ReadableSpan[] - Get all stored spans as OpenTelemetry ReadableSpan objects
  • getSpansAsDicts(): SpanDict[] - Get all stored spans as formatted dictionaries
  • getSpansByTraceId(traceId: string): SpanDict[] - Get all spans for a specific trace ID
  • clear(): void - Clear all stored spans from memory
  • export(spans: ReadableSpan[]): Promise<{ code: ExportResultCode }> - Export spans (called automatically by OpenTelemetry)
  • shutdown(): Promise<void> - Shutdown the exporter
  • forceFlush(): Promise<void> - Force flush pending spans

CandidatePromptManager

Manages prompt template overrides using AsyncLocalStorage for context-local state. Useful for A/B testing or evaluating different prompt variations.

Usage

import { CandidatePromptManager } from "@uselemma/tracing";

const promptManager = new CandidatePromptManager();

// Run code with prompt overrides
await promptManager.run(
  {
    greeting: "Hello {{ name }}, welcome!",
    farewell: "Goodbye {{ name }}!",
  },
  async () => {
    // Within this context, candidate prompts will be used
    const [template, wasOverridden] = promptManager.getEffectiveTemplate(
      "greeting",
      "Hi {{ name }}" // default template
    );

    console.log(template); // "Hello {{ name }}, welcome!"
    console.log(wasOverridden); // true
  }
);

Methods

  • run<T>(candidatePrompts: Record<string, string> | null, callback: () => Promise<T> | T): Promise<T>
    Run a callback with candidate prompts set in the async context
  • getEffectiveTemplate(promptName: string, defaultTemplate: string): [string, boolean]
    Get the effective template, applying candidate override if present. Returns [template, wasOverridden]
  • annotateSpan(span: { setAttribute: (key: string, value: unknown) => void }): void
    Annotate an OpenTelemetry span with candidate prompt metadata

Example: Dual Processor Setup

Use MemorySpanExporter alongside other exporters to both send traces to your backend and capture them locally:

import { NodeSDK } from "@opentelemetry/sdk-node";
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { MemorySpanExporter } from "@uselemma/tracing";
import { OtherSpanProcessor } from "your-backend";

export const memoryExporter = new MemorySpanExporter();

const sdk = new NodeSDK({
  spanProcessors: [
    new OtherSpanProcessor(), // Send to your backend
    new SimpleSpanProcessor(memoryExporter), // Store in memory for local access
  ],
});

sdk.start();

// In your application code
import { memoryExporter } from "./instrumentation";

function myTracedFunction() {
  // ... your code ...

  // Access spans programmatically
  const allSpans = memoryExporter.getSpansAsDicts();
  const myTrace = memoryExporter.getSpansByTraceId(currentTraceId);
}

License

MIT