Overview
The initialize() method sets up the tracing client and establishes connection to Respan. This must be called before using any tracing methods.
Signature
async initialize(): Promise<void>
Basic Usage
import { RespanTelemetry } from '@respan/tracing';
const respanAi = new RespanTelemetry({
apiKey: process.env.RESPAN_API_KEY,
appName: 'my-app'
});
// Initialize before using any tracing methods
await respanAi.initialize();
// Now ready to use
await respanAi.withWorkflow(
{ name: 'my_workflow' },
async () => {
return 'ready';
}
);
Constructor Options
const respanAi = new RespanTelemetry({
apiKey: string; // Required: Your Respan API key
baseURL?: string; // Optional: API base URL (default: https://api.respan.ai)
appName?: string; // Optional: Application name for identification
instrumentModules?: { // Optional: Modules to auto-instrument
openAI?: typeof OpenAI;
anthropic?: typeof Anthropic;
};
disableBatch?: boolean; // Optional: Send spans immediately (default: false)
logLevel?: string; // Optional: 'debug' | 'info' | 'warn' | 'error' (default: 'warn')
});
Complete Example
import { RespanTelemetry } from '@respan/tracing';
import OpenAI from 'openai';
const respanAi = new RespanTelemetry({
apiKey: process.env.RESPAN_API_KEY,
baseURL: 'https://api.respan.ai',
appName: 'production-app',
instrumentModules: {
openAI: OpenAI, // Auto-instrument OpenAI
},
disableBatch: false,
logLevel: 'info'
});
// Initialize the client
await respanAi.initialize();
console.log('Respan Tracing initialized successfully');
// Use tracing
await respanAi.withWorkflow(
{ name: 'user_request' },
async () => {
// Your code here
}
);
// Shutdown gracefully when done
await respanAi.shutdown();
Environment Variables
You can also configure using environment variables:
RESPAN_API_KEY=your-api-key
RESPAN_BASE_URL=https://api.respan.ai
const respanAi = new RespanTelemetry({
apiKey: process.env.RESPAN_API_KEY,
baseURL: process.env.RESPAN_BASE_URL,
appName: 'my-app'
});
await respanAi.initialize();
Configuration Options
Your Respan API key from the dashboard
baseURL
string
default:"https://api.respan.ai"
Respan API base URL
Application name for identifying traces in the dashboard
Modules to automatically instrument:
openAI: OpenAI SDK class
anthropic: Anthropic SDK class
If true, sends spans immediately instead of batching them
Logging verbosity: "debug", "info", "warn", "error"
Best Practices
- Always call
initialize() before using any tracing methods
- Initialize once at application startup
- Use environment variables for sensitive configuration
- Call
shutdown() before application exit to flush pending spans
- Enable auto-instrumentation for supported SDKs