> ## 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.

# Span • Rust AI Agent SDK

> Span: A span representing a unit of work.

# Span

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

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

A span representing a unit of work.

## Fields

| Name            | Type                     | Description                                |
| --------------- | ------------------------ | ------------------------------------------ |
| `id`            | `String`                 | Span ID                                    |
| `parent_id`     | `Option&lt;String&gt;`   | Parent span ID                             |
| `trace_id`      | `String`                 | Trace ID                                   |
| `name`          | `String`                 | Span name                                  |
| `kind`          | `SpanKind`               | Span kind                                  |
| `status`        | `SpanStatus`             | Span status                                |
| `start_offset`  | `Duration`               | Start time (as duration since trace start) |
| `end_offset`    | `Option&lt;Duration&gt;` | End time (as duration since trace start)   |
| `duration`      | `Option&lt;Duration&gt;` | Duration                                   |
| `attributes`    | `HashMap&lt;String`      | Attributes                                 |
| `serde_json`    | `:Value&gt;`             | Attributes                                 |
| `events`        | `Vec&lt;SpanEvent&gt;`   | Events                                     |
| `error_message` | `Option&lt;String&gt;`   | Error message (if status is Error)         |

## Methods

### `new`

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

Create a new span.

**Parameters:**

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

### `with_parent`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn with_parent(mut self, parent_id: impl Into<String>) -> Self
```

Set parent span.

**Parameters:**

| Name        | Type                      |
| ----------- | ------------------------- |
| `parent_id` | `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) -> ()
```

Add an attribute.

**Parameters:**

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

### `add_event`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn add_event(&mut self, event: SpanEvent) -> ()
```

Add an event.

**Parameters:**

| Name    | Type        |
| ------- | ----------- |
| `event` | `SpanEvent` |

### `end`

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

End the span.

**Parameters:**

| Name         | Type       |
| ------------ | ---------- |
| `end_offset` | `Duration` |

### `set_error`

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

Mark as error.

**Parameters:**

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

### `is_ended`

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

Check if span is ended.

## Source

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