# Stream transforms

A stream transform runs a [Vector Remap Language](https://vector.dev/docs/reference/vrl/) (VRL) program on each record before delivery. Use it to redact or hash personal data, drop records, parse JSON strings or rename fields.

A stream has at most one transform, of up to 16 KiB. Set it on the stream's dashboard page, where you can test it against sample records first, or through the [Management API](#set-a-transform).

## How it works

```text
request ──▶ aggregation rules ──▶ transform ──▶ buffer ──▶ bucket or table
              (read records        (each record kept
               as they arrived)     for raw delivery)
```

- The transform runs when a request is accepted, before Send2Bucket answers `2xx`. A saved transform applies to records accepted from then on, not to records already accepted.
- [Aggregation rules](./aggregation.md) read records as they arrived. The transform runs on the records they leave for raw delivery.
- Usage is counted on each record as the transform leaves it: a record it drops is not counted, and a rewritten record counts at its new size. See [How bytes are counted](./plans-and-billing.md#how-bytes-are-counted).

## What the program sees

| Path | Holds | Writable |
|---|---|---|
| `.` | The record's `data`: the event as sent, or the OTLP JSON mapping of the log record, span or metric, as in the [delivery format](./delivery-format.md). | yes |
| `%signal` | `event`, `log`, `span` or `metric`. | no effect |
| `%resource` | An OTLP record's resource, such as `{"attributes": [...]}`. Absent for events and log drains. | yes |
| `%scope` | An OTLP record's instrumentation scope. Absent for events and log drains. | yes |

`batch_id`, `received_at` and `signal` are not visible and stay as they were. After the program runs, `.` must still be an object.

| The program | Send2Bucket delivers |
|---|---|
| Ends normally | The record as the program left it. |
| Calls `abort` | Nothing: the record is dropped. |
| Fails, such as `to_int!` on `"x"` | The record as it arrived. |
| Runs longer than 25 ms on one record | The record as it arrived. |

The stream's stats count the records its transform dropped and failed on, as `transform_dropped` and `transform_failed`: on the stream's page and from the [Management API](./management-api.md)'s `GET /streams/{stream_id}/stats`.

VRL rejects a program that can fail without handling it: write `to_int!(.n)` to fail the record on bad input, or `to_int(.n) ?? 0` to use a default.

## Examples

Drop debug events, remove a password, redact an email address and hash a user ID:

```text
if .level == "debug" { abort }
del(.password)
.email = "redacted"
.user_id = sha2(string!(.user_id))
```

Parse a field that holds a JSON string, and turn a string amount into a number with a default:

```text
parsed, err = parse_json(.payload)
if err == null { .payload = parsed }
.cents = to_int(.cents) ?? 0
```

On a stream of OpenTelemetry logs, drop records below `INFO` (severity number 9) and remove the `host.name` resource attribute:

```text
if (to_int(.severityNumber) ?? 0) < 9 { abort }
%resource.attributes = filter(array!(%resource.attributes)) -> |_index, a| { a.key != "host.name" }
```

On a stream of spans, drop health checks:

```text
if .name == "GET /healthz" { abort }
```

OTLP attributes are lists of `{key, value}` and 64-bit integers are strings, as in the [delivery format](./delivery-format.md#opentelemetry-records).

## Test a transform

On the stream's page, editors write the program under **transform**, and pick a sample signal. The sample records start as up to five records recently ingested on the stream, as they arrived, refreshed at most once a minute; edit or paste your own: events as you send them, or OTLP records as `{"resource", "scope", "data"}`, one per line or as a JSON array. **test** shows what each sample becomes: kept with its new value, dropped, or failed with the error. **save** compiles the program first and shows the compiler's message if it does not compile. Viewers see the saved program.

## Set a transform

The [Management API](./management-api.md) sets `transform` on a stream, and returns it when you read one:

```bash
curl -X PATCH https://api.send2bucket.com/v1/manage/streams/<stream_id> \
  -H "Authorization: Bearer <account_key>" \
  -H "Content-Type: application/json" \
  -d '{"transform": "del(.password)"}'
```

A program that does not compile is rejected with `400`, `invalid_request` and the compiler's message in `detail`. `null` or a blank string removes the transform.

## Limits

- One transform per stream, up to 16 KiB of source.
- 25 ms of run time per record, not counting decoding and encoding it. A slower record is delivered unchanged.
- VRL stores numbers as 64-bit integers or floats, so in a transformed record a number outside those ranges, such as a 30-digit integer, is rounded.
- No network, environment or host functions (`http_request`, `dns_lookup`, `get_env_var`, `get_hostname`), and no `parse_grok`, `parse_groks` or `log`.
- Aggregate rows (`signal` `aggregate`) are not transformed.
