> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Trace Context • Rust AI Agent SDK

> TraceContext: Context for a trace.

# TraceContext

> Defined in the [**trace**](../modules/trace) module.

<Badge color="orange">Rust AI Agent SDK</Badge>

Context for a trace.

## Fields

| Name         | Type                | Description        |
| ------------ | ------------------- | ------------------ |
| `id`         | `String`            | Trace ID           |
| `name`       | `String`            | Trace name         |
| `start_time` | `Instant`           | Start time         |
| `spans`      | `Vec&lt;Span&gt;`   | All spans          |
| `span_stack` | `Vec&lt;String&gt;` | Current span stack |
| `attributes` | `HashMap&lt;String` | Attributes         |
| `serde_json` | `:Value&gt;`        | Attributes         |

## Methods

### `new`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn new(name: impl Into<String>) -> Self
```

Create a new trace context.

**Parameters:**

| Name   | Type                      |
| ------ | ------------------------- |
| `name` | `impl Into&lt;String&gt;` |

### `elapsed`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn elapsed(&self) -> Duration
```

Get elapsed time since trace start.

### `start_span`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn start_span(&mut self, name: impl Into<String>, kind: SpanKind) -> String
```

Start a new span.

**Parameters:**

| Name   | Type                      |
| ------ | ------------------------- |
| `name` | `impl Into&lt;String&gt;` |
| `kind` | `SpanKind`                |

### `end_span`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn end_span(&mut self, span_id: &str) -> ()
```

End a span.

**Parameters:**

| Name      | Type   |
| --------- | ------ |
| `span_id` | `&str` |

### `current_span_id`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn current_span_id(&self) -> Option<&str>
```

Get current span ID.

### `get_span`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn get_span(&self, span_id: &str) -> Option<&Span>
```

Get a span by ID.

**Parameters:**

| Name      | Type   |
| --------- | ------ |
| `span_id` | `&str` |

### `get_span_mut`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn get_span_mut(&mut self, span_id: &str) -> Option<&mut Span>
```

Get mutable span by ID.

**Parameters:**

| Name      | Type   |
| --------- | ------ |
| `span_id` | `&str` |

### `add_event`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn add_event(&mut self, name: impl Into<String>) -> ()
```

Add event to current span.

**Parameters:**

| Name   | Type                      |
| ------ | ------------------------- |
| `name` | `impl Into&lt;String&gt;` |

### `set_attribute`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn set_attribute(&mut self, key: impl Into<String>, value: impl Serialize) -> ()
```

Set attribute on current span.

**Parameters:**

| Name    | Type                      |
| ------- | ------------------------- |
| `key`   | `impl Into&lt;String&gt;` |
| `value` | `impl Serialize`          |

### `spans`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn spans(&self) -> &[Span]
```

Get all spans.

### `span_count`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn span_count(&self) -> usize
```

Get span count.

## Source

<Card title="View on GitHub" icon="github" href="https://github.com/MervinPraison/PraisonAI/blob/main/src/praisonai-rust/praisonai/src/trace/mod.rs#L207">
  `praisonai/src/trace/mod.rs` at line 207
</Card>

***

## Related Documentation

<CardGroup cols={2}>
  <Card title="Rust Context Management" icon="layer-group" href="/docs/rust/context-management" />

  <Card title="Rust Token Management" icon="coins" href="/docs/rust/token-management" />

  <Card title="Rust Tracing" icon="chart-line" href="/docs/rust/tracing" />

  <Card title="Rust Telemetry" icon="signal" href="/docs/rust/telemetry" />
</CardGroup>
