isRecording()

Check if tracing is currently active

Overview

isRecording() returns whether the current span is active and recording. Useful for conditional tracing logic or debugging.

Signature

1isRecording(): boolean

Basic Usage

1import { RespanTelemetry } from '@respan/tracing';
2
3const respanAi = new RespanTelemetry({
4 apiKey: process.env.RESPAN_API_KEY,
5 appName: 'my-app'
6});
7
8await respanAi.initialize();
9
10await respanAi.withWorkflow(
11 { name: 'my_workflow' },
12 async () => {
13 const client = respanAi.getClient();
14
15 if (client.isRecording()) {
16 console.log('Tracing is active');
17 client.addSpanEvent('workflow_started');
18 }
19
20 return 'done';
21 }
22);

Conditional Event Adding

1await respanAi.withTask(
2 { name: 'data_processing' },
3 async () => {
4 const client = respanAi.getClient();
5
6 const data = await fetchData();
7
8 // Only add events if tracing is active
9 if (client.isRecording()) {
10 client.addSpanEvent('data_fetched', {
11 records: data.length,
12 size_bytes: JSON.stringify(data).length
13 });
14 }
15
16 const processed = await processData(data);
17
18 if (client.isRecording()) {
19 client.addSpanEvent('data_processed', {
20 records_processed: processed.length
21 });
22 }
23
24 return processed;
25 }
26);

Debug Logging

1function debugLog(message: string, data?: any) {
2 const client = respanAi.getClient();
3
4 if (client.isRecording()) {
5 client.addSpanEvent('debug_log', {
6 message,
7 data: JSON.stringify(data)
8 });
9 }
10
11 console.log(message, data);
12}
13
14await respanAi.withWorkflow(
15 { name: 'debug_workflow' },
16 async () => {
17 debugLog('Starting workflow');
18 const result = await process();
19 debugLog('Workflow complete', { result });
20 return result;
21 }
22);

Performance Metrics

1await respanAi.withTask(
2 { name: 'expensive_operation' },
3 async () => {
4 const client = respanAi.getClient();
5 const startTime = Date.now();
6
7 const result = await expensiveComputation();
8
9 const duration = Date.now() - startTime;
10
11 // Only record metrics if tracing is active
12 if (client.isRecording()) {
13 client.updateCurrentSpan({
14 attributes: {
15 'operation.duration_ms': duration,
16 'operation.result_size': result.length
17 }
18 });
19 }
20
21 return result;
22 }
23);

Outside of Traced Functions

1async function helperFunction() {
2 const client = respanAi.getClient();
3
4 if (client.isRecording()) {
5 // We're inside a traced context
6 client.addSpanEvent('helper_called');
7 } else {
8 // Not in a traced context, skip tracing
9 console.log('Running without tracing');
10 }
11
12 return await doWork();
13}
14
15await respanAi.withWorkflow(
16 { name: 'main_workflow' },
17 async () => {
18 // isRecording will be true here
19 await helperFunction();
20 }
21);
22
23// isRecording will be false here
24await helperFunction();

Sampling Logic

1await respanAi.withWorkflow(
2 { name: 'high_volume_workflow' },
3 async () => {
4 const client = respanAi.getClient();
5
6 // Only add detailed events if actively tracing
7 if (client.isRecording()) {
8 for (let i = 0; i < 1000; i++) {
9 await processItem(i);
10
11 // Add event every 100 items
12 if (i % 100 === 0) {
13 client.addSpanEvent('progress', {
14 items_processed: i
15 });
16 }
17 }
18 } else {
19 // Skip detailed tracking if not recording
20 for (let i = 0; i < 1000; i++) {
21 await processItem(i);
22 }
23 }
24
25 return 'complete';
26 }
27);

Return Value

Returns true if tracing is currently active and recording, false otherwise.

Best Practices

  • Use to conditionally add expensive tracing operations
  • Check before adding numerous events in loops
  • Useful for debug logging that should only happen during tracing
  • Safe to call from any context (traced or untraced)
  • Can be used to implement custom sampling logic
  • Helps avoid overhead when tracing is disabled