# End Trace Span

> Closes an active distributed tracing span.

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

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

Closes an active distributed tracing span.

This operator marks the completion of a specific unit of work (Span). It calculates the duration and finalizing the span status (success/error). It is essential for accurate APM (Application Performance Monitoring) and constructing the full trace waterfall.

**Use Cases:**
- Measuring the latency of an external API call block.
- Grouping a set of complex logic steps into a single readable span.
- Recording the final status of a custom transaction.

## Configuration

- `name` (string, required): Name of the span to close (must match Start Span).

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 closed successfully.
- `error`: Failed to find or close span.

## Input contract

- `trace_id` (string, optional): The active Trace ID.
- `span_id` (string, required): The Span ID to close.
- `attributes` (object, optional): Final tags/attributes (e.g. status code).

## Output contract

- `success` (boolean, optional)
- `duration_ms` (number, optional)

## In expressions

What a later node can read from this node's output with `$("End Trace Span").item.json.<path>` (write your node's label instead of `End 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.

- `$("End Trace Span").item.json.success` (boolean) = `true`
- `$("End Trace Span").item.json.duration_ms` (number) **The worker does not return it: reads as undefined.**
- `$("End Trace Span").item.json.port` (string) = `"success"` (Not suggested by the editor)


## How it works

Writes a `[TRACE-END] <name>` line to the orchestrator log and returns `{ success: true }` on `success`. It does not look up or close a span and computes no duration.

1. Merges the input item with the resolved config (config wins).
2. Leaves through `error` with `Missing 'name'` when `name` is empty. `span_id` is not checked, although the input contract marks it as required.
3. Logs `[TRACE-END] <name>` at info level; `trace_id`, `span_id` and `attributes` passed with it are not written.
4. Returns `{ port: "success", success: true }`; the engine uses that whole object as the output.

## In a workflow

### Node

```json
{
  "node_id": "node-trace-end-checkout",
  "label": "End checkout span",
  "operator_slug": "ops.trace.end",
  "operator_version": "1.0.0",
  "config": {
    "name": "checkout",
    "trace_id": "{{ $json.trace_id }}",
    "span_id": "{{ $json.span_id }}"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "success",
  "data": {
    "port": "success",
    "success": true
  }
}
```

## Output ports (behaviour in the code)

### `success`

`name` is set.

```json
{
  "port": "success",
  "success": true
}
```

### `error`

`name` is empty.

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

## Behaviour in the code

- `span_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: `trace_id`, `span_id`, `attributes`. None of them is written to the log.

## Errors

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

## Notes and limits

- `duration_ms` appears in the output contract but is never returned.
- 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.end",
  "name": "End Trace Span",
  "category": "ops",
  "version": "1.0.0",
  "description": "Closes an active distributed tracing span.\n\n  This operator marks the completion of a specific unit of work (Span). It calculates the duration and finalizing the span status (success/error). It is essential for accurate APM (Application Performance Monitoring) and constructing the full trace waterfall.\n\n  **Use Cases:**\n  - Measuring the latency of an external API call block.\n  - Grouping a set of complex logic steps into a single readable span.\n  - Recording the final status of a custom transaction.",
  "capabilities": [
    "observability",
    "tracing",
    "apm"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "name"
    ],
    "properties": {
      "name": {
        "type": "string",
        "title": "Span Name",
        "description": "Name of the span to close (must match Start Span)."
      },
      "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": {
      "trace_id": {
        "type": "string",
        "description": "The active Trace ID."
      },
      "span_id": {
        "type": "string",
        "description": "The Span ID to close."
      },
      "attributes": {
        "type": "object",
        "description": "Final tags/attributes (e.g. status code)."
      }
    },
    "required": [
      "span_id"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "description": "Confirmation.",
    "properties": {
      "success": {
        "type": "boolean"
      },
      "duration_ms": {
        "type": "number"
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Ended",
      "is_default": true,
      "description": "Span closed successfully."
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Failed to find or close span."
    }
  ],
  "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": "Name of the span to close (must match Start Span)."
    },
    "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": {
    "trace_id": {
      "type": "string",
      "description": "The active Trace ID."
    },
    "span_id": {
      "type": "string",
      "description": "The Span ID to close."
    },
    "attributes": {
      "type": "object",
      "description": "Final tags/attributes (e.g. status code)."
    }
  },
  "required": [
    "span_id"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "description": "Confirmation.",
  "properties": {
    "success": {
      "type": "boolean"
    },
    "duration_ms": {
      "type": "number"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "success",
    "label": "Ended",
    "is_default": true,
    "description": "Span closed successfully."
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Failed to find or close span."
  }
]
```
