addSpanEvent()

Add a timestamped event to the current span

Overview

addSpanEvent() adds a named event with optional attributes to the currently active span. Events are useful for marking significant milestones or state changes within a span.

Signature

1addSpanEvent(
2 name: string,
3 attributes?: Record<string, any>
4): void

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.withTask(
11 { name: 'data_processing' },
12 async () => {
13 const client = respanAi.getClient();
14
15 client.addSpanEvent('processing_started');
16
17 const data = await fetchData();
18
19 client.addSpanEvent('data_fetched', {
20 records: data.length
21 });
22
23 const processed = await processData(data);
24
25 client.addSpanEvent('processing_completed', {
26 records_processed: processed.length,
27 success_rate: 0.98
28 });
29
30 return processed;
31 }
32);

Batch Processing Events

1await respanAi.withWorkflow(
2 { name: 'batch_job' },
3 async () => {
4 const client = respanAi.getClient();
5
6 client.addSpanEvent('batch_started', {
7 total_batches: 10
8 });
9
10 for (let i = 0; i < 10; i++) {
11 await processBatch(i);
12
13 client.addSpanEvent('batch_completed', {
14 batch_number: i,
15 records: 100,
16 duration_ms: 150
17 });
18 }
19
20 client.addSpanEvent('all_batches_completed', {
21 total_records: 1000
22 });
23
24 return 'done';
25 }
26);

API Call Events

1await respanAi.withTask(
2 { name: 'external_api_call' },
3 async () => {
4 const client = respanAi.getClient();
5
6 client.addSpanEvent('request_prepared', {
7 endpoint: '/api/users',
8 method: 'GET'
9 });
10
11 const response = await fetch('https://api.example.com/users');
12
13 client.addSpanEvent('response_received', {
14 status_code: response.status,
15 content_type: response.headers.get('content-type')
16 });
17
18 const data = await response.json();
19
20 client.addSpanEvent('response_parsed', {
21 records: data.length
22 });
23
24 return data;
25 }
26);

State Transitions

1await respanAi.withAgent(
2 { name: 'workflow_agent' },
3 async () => {
4 const client = respanAi.getClient();
5
6 client.addSpanEvent('state_changed', {
7 from: 'idle',
8 to: 'planning'
9 });
10
11 const plan = await createPlan();
12
13 client.addSpanEvent('state_changed', {
14 from: 'planning',
15 to: 'executing'
16 });
17
18 const result = await executePlan(plan);
19
20 client.addSpanEvent('state_changed', {
21 from: 'executing',
22 to: 'completed'
23 });
24
25 return result;
26 }
27);

Error Context Events

1await respanAi.withTask(
2 { name: 'retry_operation' },
3 async () => {
4 const client = respanAi.getClient();
5 let attempt = 0;
6
7 while (attempt < 3) {
8 try {
9 client.addSpanEvent('attempt_started', {
10 attempt_number: attempt + 1
11 });
12
13 const result = await unstableApiCall();
14
15 client.addSpanEvent('attempt_succeeded', {
16 attempt_number: attempt + 1
17 });
18
19 return result;
20 } catch (error) {
21 client.addSpanEvent('attempt_failed', {
22 attempt_number: attempt + 1,
23 error_message: error.message
24 });
25
26 attempt++;
27 if (attempt >= 3) throw error;
28 }
29 }
30 }
31);

Performance Milestones

1await respanAi.withWorkflow(
2 { name: 'image_processing' },
3 async () => {
4 const client = respanAi.getClient();
5 const startTime = Date.now();
6
7 client.addSpanEvent('download_started');
8 const image = await downloadImage();
9 client.addSpanEvent('download_completed', {
10 duration_ms: Date.now() - startTime,
11 size_bytes: image.size
12 });
13
14 const resizeStart = Date.now();
15 client.addSpanEvent('resize_started');
16 const resized = await resizeImage(image);
17 client.addSpanEvent('resize_completed', {
18 duration_ms: Date.now() - resizeStart,
19 dimensions: resized.dimensions
20 });
21
22 const uploadStart = Date.now();
23 client.addSpanEvent('upload_started');
24 await uploadImage(resized);
25 client.addSpanEvent('upload_completed', {
26 duration_ms: Date.now() - uploadStart
27 });
28
29 return 'done';
30 }
31);

Parameters

name
stringRequired

Event name for identification

attributes
Record<string, any>

Optional key-value pairs with additional event context

Best Practices

  • Use events to mark significant milestones in span execution
  • Include relevant attributes for context (counts, durations, status codes)
  • Add events before and after critical operations
  • Use consistent naming conventions for events
  • Events are automatically timestamped
  • Only call within an active span
  • Events are visible in the Respan dashboard timeline