The ZeroEval TypeScript SDK provides tracing for Node.js and browser applications through wrapper functions and integration callbacks.

Installation

npm install zeroeval

Basic Setup

import * as ze from 'zeroeval';

// Option 1: ZEROEVAL_API_KEY in your environment variable file
ze.init();

// Option 2: API key
ze.init({ apiKey: 'YOUR_API_KEY' });

// Option 3: With additional configuration
ze.init({
  apiKey: 'YOUR_API_KEY',
  apiUrl: 'https://api.zeroeval.com', // optional
  flushInterval: 10, // seconds
  maxSpans: 100,
});

Patterns

The SDK offers two ways to add tracing to your TypeScript/JavaScript code:

Basic Usage

import * as ze from 'zeroeval';

// Wrap synchronous functions
const fetchData = (userId: string) =>
  ze.withSpan({ name: 'fetch_data' }, () => ({
    userId,
    name: 'John Doe'
  }));

// Wrap async functions
const processData = async (data: { name: string }) =>
  ze.withSpan(
    {
      name: 'process_data',
      attributes: { version: '1.0' }
    },
    async () => {
      const result = await transform(data);
      return `Welcome, ${result.name}!`;
    }
  );

// Complex workflows with nested spans
async function complexWorkflow() {
  return ze.withSpan({ name: 'data_pipeline' }, async () => {
    const data = await ze.withSpan(
      { name: 'fetch_stage' },
      fetchExternalData
    );

    const processed = await ze.withSpan(
      { name: 'process_stage' },
      () => transformData(data)
    );

    const result = await ze.withSpan(
      { name: 'save_stage' },
      () => saveToDatabase(processed)
    );

    return result;
  });
}
Decorators require TypeScript configuration: Enable experimentalDecorators and emitDecoratorMetadata in your tsconfig.json. When using runtime tools like tsx or ts-node, pass the --experimental-decorators flag.

Sessions

Group related spans into sessions:
import { v4 as uuidv4 } from 'uuid';

const sessionId = uuidv4();

async function userJourney(userId: string) {
  return ze.withSpan(
    {
      name: 'user_journey',
      sessionId: sessionId,
      sessionName: `User ${userId} Session`
    },
    async () => {
      await login(userId);
      await browseProducts();
      await checkout();
    }
  );
}

Context

Access current context information:
import { getCurrentSpan, getCurrentTrace, getCurrentSession } from 'zeroeval';

function myFunction() {
  // Get current span
  const span = getCurrentSpan();
  
  // Get current trace ID
  const traceId = getCurrentTrace();
  
  // Get current session ID
  const sessionId = getCurrentSession();
}