# Start Trace Span

> Initiates a new Distributed Tracing Span for deep observability.

Source: https://docs.sommatic.ai/en/nodes/ops.trace.start/

- Slug: `ops.trace.start`
- Family: Operations
- Version: 1.0.0
- Status: Available
- Execution: sync

Initiates a new Distributed Tracing Span for deep observability.

Spans allow you to visualize the duration and details of a specific block of work in a trace waterfall graph. This operator starts a custom span, which must typically be closed by a corresponding 'End Span' (or happens automatically at workflow end).

**Use Cases:**
- Explicitly measuring the duration of a specific group of steps.
- Adding business-specific attributes (tags) to a trace segment.
- Correlating with external systems via Parent Trace ID.

## Configuration

- `name` (string, required): Human-readable name for the span phase.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `success` (default): Span active.
- `error`: Failed to start tracer.

## Input contract

- `attributes` (object, optional): Custom tags/attributes to attach to the span.
- `parent_trace_id` (string, optional): Optional: Link to an external trace.

## Output contract

- `trace_id` (string, optional)
- `span_id` (string, optional)

## In expressions

What a later node can read from this node's output with `$("Start Trace Span").item.json.<path>` (write your node's label instead of `Start Trace Span`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `success`, from the output shown in Ports.

- `$("Start Trace Span").item.json.trace_id` (string) = `"94d2f2da-7886-4e23-8bbb-b8c841d99b21"`
- `$("Start Trace Span").item.json.span_id` (string) = `"50cb8ce0-8949-4f60-a5ff-7795654af4d7"`
- `$("Start Trace Span").item.json.port` (string) = `"success"` (Not suggested by the editor)


## How it works

Generates a trace id and a span id, writes a `[TRACE-START] <name>` line to the orchestrator log and returns both ids. No tracing backend is called.

1. Merges the input item with the resolved config (config wins).
2. Uses `parent_trace_id` as the trace id when set; otherwise generates a UUID v4.
3. Leaves through `error` with `Missing 'name'` when `name` is empty.
4. Generates the span id (UUID v4) and logs `[TRACE-START] <name>` at info level; the ids and `attributes` passed with it are not written.
5. Returns `{ port: "success", trace_id, span_id }`; the engine uses that whole object as the output.

## In a workflow

### Node

```json
{
  "node_id": "node-trace-start-checkout",
  "label": "Start checkout span",
  "operator_slug": "ops.trace.start",
  "operator_version": "1.0.0",
  "config": {
    "name": "checkout"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "success",
  "data": {
    "port": "success",
    "trace_id": "94d2f2da-7886-4e23-8bbb-b8c841d99b21",
    "span_id": "50cb8ce0-8949-4f60-a5ff-7795654af4d7"
  }
}
```

## Output ports (behaviour in the code)

### `success`

`name` is set.

```json
{
  "port": "success",
  "trace_id": "94d2f2da-7886-4e23-8bbb-b8c841d99b21",
  "span_id": "50cb8ce0-8949-4f60-a5ff-7795654af4d7"
}
```

### `error`

`name` is empty.

```json
{
  "port": "error",
  "error": "Missing 'name'"
}
```

## Behaviour in the code

- `parent_trace_id`: Not in the config schema (only in the input contract), but read from the node config or, when unset there, from the input item: `parent_trace_id`, `attributes`.
- `attributes`: Accepted but not written anywhere: the log line contains only the name.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Missing 'name'` | `error` | `name` is set neither on the node nor in the input item. |

## Notes and limits

- The ids are not propagated automatically and are not linked to the run ids. Pass them on explicitly, for example `{{ $json.trace_id }}` and `{{ $json.span_id }}` in `ops.trace.end`.
- The runtime logger prints only the message text: the structured object passed with it is not written to the log.
- The worker returns its fields at the top level instead of under `data`, so the engine uses the whole returned object, `port` included, as the node output.
- Leaving through `error` does not fail the run: the engine follows the edges wired to `error`, and if there are none the branch simply ends.
- The `execution` settings (`retryOnFail`, `maxTries`, `waitBetweenTries`, `continueOnFail`, `timeout_ms`) are accepted, but neither this worker nor the engine applies them.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "ops.trace.start",
  "name": "Start Trace Span",
  "category": "ops",
  "version": "1.0.0",
  "description": "Initiates a new Distributed Tracing Span for deep observability.\n\n  Spans allow you to visualize the duration and details of a specific block of work in a trace waterfall graph. This operator starts a custom span, which must typically be closed by a corresponding 'End Span' (or happens automatically at workflow end).\n\n  **Use Cases:**\n  - Explicitly measuring the duration of a specific group of steps.\n  - Adding business-specific attributes (tags) to a trace segment.\n  - Correlating with external systems via Parent Trace ID.",
  "capabilities": [
    "observability",
    "tracing",
    "apm"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "name"
    ],
    "properties": {
      "name": {
        "type": "string",
        "title": "Span Name",
        "description": "Human-readable name for the span phase."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "attributes": {
        "type": "object",
        "description": "Custom tags/attributes to attach to the span."
      },
      "parent_trace_id": {
        "type": "string",
        "description": "Optional: Link to an external trace."
      }
    }
  },
  "output_contract_schema": {
    "type": "object",
    "description": "The created trace context identifiers.",
    "properties": {
      "trace_id": {
        "type": "string"
      },
      "span_id": {
        "type": "string"
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Started",
      "is_default": true,
      "description": "Span active."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Failed to start tracer."
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "observability",
    "tracing",
    "telemetry"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_vectry",
    "profile": {
      "name": "Vectry Analytics",
      "verified": true,
      "url": "https://me.veripass.com/vectry",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "required": [
    "name"
  ],
  "properties": {
    "name": {
      "type": "string",
      "title": "Span Name",
      "description": "Human-readable name for the span phase."
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "attributes": {
      "type": "object",
      "description": "Custom tags/attributes to attach to the span."
    },
    "parent_trace_id": {
      "type": "string",
      "description": "Optional: Link to an external trace."
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "description": "The created trace context identifiers.",
  "properties": {
    "trace_id": {
      "type": "string"
    },
    "span_id": {
      "type": "string"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "success",
    "label": "Started",
    "is_default": true,
    "description": "Span active."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Failed to start tracer."
  }
]
```
