Skip to main content
Version: 4.7.56

traced()

The traced() function wraps any function with OpenTelemetry tracing to automatically track its execution. It creates a span for each function call, records input arguments and return values, and captures any exceptions that occur during execution.

When used with the GentraceSpanProcessor (configured in your OpenTelemetry setup), Gentrace simplifies the process of converting baggage values to span attributes, ensuring the OpenTelemetry Collector can properly filter and route traces to Gentrace.

Basic usage

typescript
import { init, traced } from 'gentrace';
import OpenAI from 'openai';
init({
apiKey: process.env.GENTRACE_API_KEY,
});
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
async function generateResponse(prompt: string): Promise<string> {
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: prompt }],
});
return response.choices[0].message.content || '';
}
const tracedGenerateResponse = traced('generateResponse', generateResponse);
const aiResponse = await tracedGenerateResponse('What is the capital of France?');

Overview

The traced() function provides automatic instrumentation for any function in your codebase. It:

  1. Creates OpenTelemetry spans for each function execution with customizable names
  2. Records function arguments as span events for debugging and analysis
  3. Captures return values as span events to track function outputs
  4. Handles errors automatically by recording exceptions and setting appropriate span status
  5. Supports both sync and async functions with proper typing preservation
  6. Allows custom attributes to be attached to spans for enhanced observability

Parameters

Function signature

typescript
function traced<F extends (...args: any[]) => any>(
name: string,
fn: F,
config?: TracedConfig
): F

Parameters

  • name (string, required): The name for the OpenTelemetry span. This will appear in your tracing dashboard.
  • fn (function, required): The function to wrap with tracing. Can be synchronous or asynchronous.
  • config (TracedConfig, optional): Additional configuration options for the traced function.

TracedConfig

typescript
type TracedConfig = {
attributes?: Record<string, any>;
}
  • attributes (object, optional): Custom attributes to attach to the span for additional context.

Advanced usage

You can add custom attributes to the OpenTelemetry span using the attributes option.

typescript
import { traced } from 'gentrace';
// Add custom attributes for enhanced observability
function processData(data: any[]): any[] {
return data.map(item => ({ ...item, processed: true }));
}
const tracedProcessData = traced('processData', processData, {
attributes: {
'service.name': 'data-processor',
'service.version': '1.2.0',
'operation.type': 'batch_processing'
}
});
const result = tracedProcessData([{ id: 1 }, { id: 2 }]);

Tracing AI/LLM functions

typescript
import { traced } from 'gentrace';
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// Trace LLM calls with relevant attributes
async function generateResponse(prompt: string): Promise<string> {
const completion = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: prompt }],
temperature: 0.7,
});
return completion.choices[0].message.content || '';
}
const tracedGenerateResponse = traced('generateResponse', generateResponse, {
attributes: {
'llm.vendor': 'OpenAI',
'llm.model': 'gpt-4o',
'llm.temperature': 0.7,
'operation.type': 'text_generation'
}
});
const response = await tracedGenerateResponse('Explain quantum computing');

Tracing database operations

typescript
import { traced } from 'gentrace';
// Trace database operations
async function getUserById(userId: string): Promise<User | null> {
const query = 'SELECT * FROM users WHERE id = $1';
const result = await db.query(query, [userId]);
return result.rows[0] || null;
}
const tracedGetUserById = traced('getUserById', getUserById, {
attributes: {
'db.system': 'postgresql',
'db.operation': 'select',
'db.table': 'users'
}
});
const user = await tracedGetUserById('user-123');

Automatic data capture

The traced() function automatically captures:

Function arguments

All input arguments are serialized and recorded as span events, making it easy to debug function calls and understand what data was processed.

Return values

Function return values are captured and stored as span events, allowing you to track outputs and verify function behavior.

Error handling

When functions throw exceptions or reject promises, the traced() function automatically:

typescript
import { traced } from 'gentrace';
// Function that might throw an error
function riskyOperation(input: string): string {
if (input === 'error') {
throw new Error('Something went wrong!');
}
return `Processed: ${input}`;
}
const tracedRiskyOperation = traced('riskyOperation', riskyOperation);
try {
const result = tracedRiskyOperation('error');
} catch (error) {
// Error is automatically captured in the span with:
// - Exception details and stack trace
// - Span status set to ERROR
// - Error type attribute set
console.log('Operation failed:', error.message);
}

Span events and attributes

The traced() function creates the following span events and attributes:

Span events

  • Function Arguments: Captured at the start of function execution
  • Function Output: Captured when the function completes successfully
  • Exceptions: Captured when errors occur, including full stack traces

Span attributes

  • Custom Attributes: Any attributes provided in the configuration
  • Error Information: When exceptions occur:
    • error.type: The type/class name of the exception
    • Span status set to ERROR with error message

Requirements

  • OpenTelemetry Setup: The traced() function requires OpenTelemetry to be configured for tracing
  • Gentrace SDK Initialization: Must call init() with a valid API key before using traced functions
  • Function Compatibility: Works with both synchronous and asynchronous functions
  • init() - Initialize the Gentrace SDK
  • interaction() - Higher-level wrapper for AI/LLM function tracing with automatic baggage handling
  • experiment() - Create testing contexts that can contain traced functions
  • eval() / evalOnce() - Run evaluations that can include traced functions

See also