getCurrentTraceId()

Get the current trace ID

Overview

getCurrentTraceId() returns the trace ID of the currently active trace. Useful for correlating logs, passing to external systems, or debugging.

Signature

getCurrentTraceId(): string | undefined

Basic Usage

import { RespanTelemetry } from '@respan/tracing';
const respanAi = new RespanTelemetry({
apiKey: process.env.RESPAN_API_KEY,
appName: 'my-app'
});
await respanAi.initialize();
await respanAi.withWorkflow(
{ name: 'user_request' },
async () => {
const client = respanAi.getClient();
const traceId = client.getCurrentTraceId();
console.log(`Processing trace: ${traceId}`);
return traceId;
}
);

Log Correlation

await respanAi.withWorkflow(
{ name: 'api_request' },
async () => {
const client = respanAi.getClient();
const traceId = client.getCurrentTraceId();
// Add trace ID to logs for correlation
console.log(`[${traceId}] Starting API request`);
const result = await fetch('https://api.example.com/data');
console.log(`[${traceId}] API request completed`);
return result;
}
);

Pass to External Systems

await respanAi.withWorkflow(
{ name: 'payment_processing' },
async () => {
const client = respanAi.getClient();
const traceId = client.getCurrentTraceId();
// Pass trace ID to payment processor for correlation
const payment = await processPayment({
amount: 100,
currency: 'USD',
trace_id: traceId // Include for debugging
});
return payment;
}
);

Return to User

import express from 'express';
app.post('/api/process', async (req, res) => {
await respanAi.withWorkflow(
{ name: 'api_endpoint' },
async () => {
const client = respanAi.getClient();
const traceId = client.getCurrentTraceId();
const result = await processRequest(req.body);
// Return trace ID to user for support requests
res.json({
success: true,
result: result,
trace_id: traceId
});
}
);
});

Return Value

Returns the current trace ID as a string, or undefined if no trace is active.

Best Practices

  • Use trace IDs for log correlation across services
  • Include trace IDs in API responses for debugging
  • Pass trace IDs to external systems for end-to-end tracing
  • Store trace IDs with error reports for troubleshooting
  • Only call within an active trace (workflow, task, agent, or tool)