updateCurrentSpan()

Update name, status, attributes, and Respan params on the current span

Overview

updateCurrentSpan() allows you to modify the currently active span’s metadata, status, attributes, and Respan-specific parameters.

Signature

1updateCurrentSpan(options: {
2 respan_params?: {
3 customer_identifier?: string;
4 trace_group_identifier?: string;
5 metadata?: Record<string, any>;
6 };
7 attributes?: Record<string, any>;
8 status?: 'OK' | 'ERROR';
9 name?: string;
10}): 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.withWorkflow(
11 { name: 'user_request' },
12 async () => {
13 const client = respanAi.getClient();
14
15 client.updateCurrentSpan({
16 respan_params: {
17 customer_identifier: 'user-123',
18 metadata: {
19 environment: 'production',
20 version: '2.0'
21 }
22 }
23 });
24
25 return await processRequest();
26 }
27);

Update Status

1await respanAi.withTask(
2 { name: 'validation' },
3 async () => {
4 const client = respanAi.getClient();
5
6 try {
7 const data = await validateInput();
8
9 client.updateCurrentSpan({
10 status: 'OK',
11 attributes: {
12 'validation.result': 'success'
13 }
14 });
15
16 return data;
17 } catch (error) {
18 client.updateCurrentSpan({
19 status: 'ERROR',
20 attributes: {
21 'validation.result': 'failed',
22 'error.message': error.message
23 }
24 });
25 throw error;
26 }
27 }
28);

Custom Attributes

1await respanAi.withWorkflow(
2 { name: 'data_pipeline' },
3 async () => {
4 const client = respanAi.getClient();
5
6 client.updateCurrentSpan({
7 attributes: {
8 'pipeline.stage': 'extraction',
9 'pipeline.source': 'database',
10 'pipeline.records': 1000
11 }
12 });
13
14 const data = await extractData();
15
16 client.updateCurrentSpan({
17 attributes: {
18 'pipeline.stage': 'transformation',
19 'pipeline.records_processed': data.length
20 }
21 });
22
23 return await transformData(data);
24 }
25);

Respan Parameters

1await respanAi.withWorkflow(
2 { name: 'customer_interaction' },
3 async () => {
4 const client = respanAi.getClient();
5
6 client.updateCurrentSpan({
7 respan_params: {
8 customer_identifier: 'customer-abc123',
9 trace_group_identifier: 'support-tickets',
10 metadata: {
11 ticket_id: 'TICKET-456',
12 priority: 'high',
13 category: 'billing'
14 }
15 }
16 });
17
18 return await handleTicket();
19 }
20);

Dynamic Span Naming

1await respanAi.withTask(
2 { name: 'api_call' },
3 async () => {
4 const client = respanAi.getClient();
5 const endpoint = '/api/users';
6
7 client.updateCurrentSpan({
8 name: `api_call:${endpoint}`,
9 attributes: {
10 'http.endpoint': endpoint,
11 'http.method': 'GET'
12 }
13 });
14
15 return await fetch(`https://api.example.com${endpoint}`);
16 }
17);

Comprehensive Update

1await respanAi.withWorkflow(
2 { name: 'order_processing' },
3 async () => {
4 const client = respanAi.getClient();
5
6 const orderId = 'ORDER-789';
7 const customerId = 'CUST-123';
8
9 client.updateCurrentSpan({
10 name: `order_processing:${orderId}`,
11 respan_params: {
12 customer_identifier: customerId,
13 trace_group_identifier: 'orders',
14 metadata: {
15 order_id: orderId,
16 payment_method: 'credit_card',
17 total_amount: 99.99
18 }
19 },
20 attributes: {
21 'order.id': orderId,
22 'order.status': 'processing',
23 'customer.id': customerId
24 },
25 status: 'OK'
26 });
27
28 return await processOrder(orderId);
29 }
30);

Parameters

respan_params
object

Respan-specific parameters for filtering and grouping

customer_identifier
string

Customer or user identifier for filtering traces by customer

trace_group_identifier
string

Group identifier for organizing related traces

metadata
Record<string, any>

Custom metadata for additional context

attributes
Record<string, any>

Custom OpenTelemetry attributes (key-value pairs)

status
string

Span status: "OK" or "ERROR"

name
string

New name for the span (useful for dynamic naming)

Best Practices

  • Update spans with customer identifiers for user-specific filtering
  • Set status to ERROR when operations fail
  • Use attributes for technical details (HTTP status, DB queries, etc.)
  • Use Respan metadata for business context (order IDs, ticket numbers, etc.)
  • Update spans early in the function with identifying information
  • Only call within an active span