# Vector Agent

Replace `<REGION>` in the hosts and samples on this page with your workspace's region code: `us`, `ca`, `eu`, `uk` or `au`.

## Overview

[Vector](https://vector.dev/) is a cross-platform high-performance log aggregation, transformation, and forwarding agent written in rust.
Vector receives data from various sources, optionally performs data transformations, and then ships data to destinations (like SparkLogs).

[Over 40 log sources](https://vector.dev/docs/reference/configuration/sources/) are supported, including
[files](https://vector.dev/docs/reference/configuration/sources/file/), [journald](https://vector.dev/docs/reference/configuration/sources/journald/),
[syslog](https://vector.dev/docs/reference/configuration/sources/syslog/), [Kubernetes](https://vector.dev/docs/reference/configuration/sources/kubernetes_logs/),
and [Docker](https://vector.dev/docs/reference/configuration/sources/docker_logs/).
Vector can ship logs to SparkLogs using the [https sink](https://vector.dev/docs/reference/configuration/sinks/http/).

Although [AutoExtract](https://sparklogs.com/docs/ingest/autoextract.md) will automatically extract structured field data from your raw logs and is recommended in the
typical case, you can also manually parse log data within the Vector agent into structured fields using [Vector transformations](https://vector.dev/docs/reference/configuration/transforms/),
including the powerful [VRL](https://vector.dev/docs/reference/vrl/) language. Examples of common transformations can
be found [here](https://vector.dev/docs/reference/vrl/examples/).

## How to Use

Follow these steps for each [Ingest Key](https://sparklogs.com/docs/ingest.md) that will receive data from Vector:

### 1. Consider Vector deployment design and topology

In its simplest form, Vector is deployed as an agent on each machine and will send data directly to SparkLogs.
SparkLogs is highly scalable and can receive data from distributed Vector agents without bottlenecks.

Vector can also be deployed to aggregate log data locally, and then ship this aggregated log data to SparkLogs.
If you have complex requirements, review the [Vector deployment guide](https://vector.dev/docs/setup/deployment/),
including the available [roles](https://vector.dev/docs/setup/deployment/roles/) and [topologies](https://vector.dev/docs/setup/deployment/topologies/).

### 2. Create an Ingest Key and get config template

In the [app](https://sparklogs.app), click the Configure sidebar button:

![Configure button in the sidebar](https://sparklogs.com/img/ux/sidebar-configure.png)

and then click the `Ingest Keys` tab.

As appropriate, create a new Ingest Key, or highlight an existing Ingest Key and click `View Ingest Key`.
In the dialog that shows the configuration template, click the `Vector` tab
and copy the configuration template.

### 3. Customize configuration

Copy the configuration template and customize it based on your needs. At a minimum,
add additional sources in the YAML config, and modify the `add_timezone` transformation
to take as input any new sources you added.

**Example Vector configuration template**

Make sure to get your configuration template from the app, as your ingestion endpoint
can vary based on your provisioned region. This is an example of what it will look like:

```yaml
log_schema:
  # Use AutoExtract timestamp if found, otherwise use ingested time.
  timestamp_key: "observed_timestamp"

sources:
  # Add your desired log sources here...
  source1:
    type: "..."
    ...

transforms:
  add_timezone:
    type: remap
    # Use the agent local timezone for timestamps without an explicit TZ
    source: |-
      .__agent_timezone = get_timezone_name!()
    # Reference all of your sources here...
    inputs:
      - source1

sinks:
  itlightning:
    type: http
    inputs:
      - add_timezone
    uri: "https://ingest-<REGION>.engine.sparklogs.app/ingest/v1"
    request:
      headers:
        # Customize headers as desired, e.g., set to "true" to disable AutoExtract
        X-No-AutoExtract: "false"
    method: post
    compression: zstd
    encoding:
      codec: json
    auth:
      strategy: basic
      user: "<INGEST-KEY-ID>"
      password: "<INGEST-KEY-ACCESS-TOKEN>"
    batch:
      # Ingestion is optimized for 1 MB batches
      max_bytes: 1048576
```

> **Note**
>
> Vector 0.57+ no longer expands `${ENV_VAR}` in config files by default. If you substitute values from environment variables, start Vector with `--dangerously-allow-env-var-interpolation`, or use Vector's secrets backend.

### 4. Deploy Vector agents

On each system that will ship data to SparkLogs for this Ingest Key,
[install the Vector agent](https://vector.dev/docs/setup/installation/)
with the appropriate configuration, and make sure it starts on system boot.

While it's recommended to install vector in daemon mode on the same server
that is producing your log data, it also supports being deployed as a sidecar.

If you're using Kubernetes, consider deploying using [Helm](https://vector.dev/docs/setup/installation/package-managers/helm/).

## Performance

Vector is lightweight yet optimized for high throughput, low latency, and CPU efficiency.

Vector's [adaptive request concurrency](https://vector.dev/docs/about/under-the-hood/networking/arc/) architecture works
well with SparkLogs, delivering very high throughput even from a single Vector agent (e.g., 2 TB/day or more) and dynamically
adapting to your network conditions.

> **Tip**
>
> SparkLogs is optimized to receive log data in batches roughly 1MiB in size, and to accept as many
> parallel requests as needed to ingest data quickly. Note that log batches larger than 40MiB will fail.
> Our Vector configuration template is fully optimized has been tested to support high throughput.

## Advanced Use Cases

### Vector log namespace (beta)

Vector supports two log "namespaces" controlled by the `schema.log_namespace` configuration option:

- **Legacy log namespace** (default, recommended). Emitted source fields appear as top-level root keys on each
  event (`host`, `message`, `timestamp`, `channel`, etc.). SparkLogs field mapping is built for
  this shape.
- **Vector log namespace** (`schema.log_namespace = true`, currently labeled _beta_ in Vector's
  docs). Source-emitted fields move into event metadata, addressable via VRL paths like `%vector.*`
  and `%<source_name>.*`. When such events are serialized for HTTP sinks, the metadata is **not**
  automatically included in the JSON body; you must copy it explicitly via your sink encoding or a
  VRL `remap` transform.

If you've opted in to the Vector log namespace, copy the source-specific metadata into root-level
keys (legacy-style) before shipping to SparkLogs. The simplest pattern is a `remap` transform
that copies known metadata to root, or to a known nested key that SparkLogs will recurse into
(`meta`, `log`, or `lognamespace`):

```toml
[transforms.flatten_for_sparklogs]
type = "remap"
inputs = ["my_kubernetes_logs"]
source = '''
.kubernetes = %kubernetes_logs.kubernetes
.file = %kubernetes_logs.file
.stream = %kubernetes_logs.stream
.host = %kubernetes_logs.host
'''
```

Reference: [Vector "Log Namespace" guide](https://vector.dev/guides/level-up/log_namespace/) and
[schema configuration](https://vector.dev/docs/reference/configuration/schema/).

### Multiline aggregation

You may wish to have multiple lines of log text joined together into one log event. For example,
if your application logs a long stack trace after an error message. While this isn't required,
you may find it easier to explore and analyze your log data with multi-line log messages properly
merged into a single log event.

vector.dev has a number of options to do this. The simplest is to use the
[multiline configuration options](https://vector.dev/docs/reference/configuration/sources/file/#multiline)
of your vector.dev source (e.g., file source). The options support the strategy where
all lines that do not begin with a certain pattern are assumed to be part of the prior event,
as well as the opposite strategy where all lines are assumed to be independent unless they
begin with a pattern that is known to be a line continuation.

The multiline configuration options should be sufficient for all except the most unusual cases.
A more advanced technique is to use Lua scripting in a vector transformation to merge lines
together based on dynamic/custom conditions. See the [guide](https://vector.dev/guides/advanced/merge-multiline-logs-with-lua/).

### Ingesting into child organizations

As a very advanced use case, an Ingest Key can send data to any child organization of the organization that the Ingest Key is
associated with on a per-request basis during ingestion. This allows a single Ingest Key to be setup at a higher
level of the hierarchy and then it can send data to child organizations to group the data as needed.

For example, you could configure a different vector.dev http sink for each child organization you
want the Ingest Key to ingest into, and then use VRL to filter the source events so that events are
only picked up by the SparkLogs http sink associated with the child organization relevant to that log event.

To do this, the ingestion API request (e.g., in the vector http sink) should specify the `org_id` query
parameter with the ID of the organization the event data should be ingested into. The authorization check
will ensure that the Ingest Key is authorized to write data to the specified organization.

---

Source: <https://sparklogs.com/docs/ingest/tools/vector>
All SparkLogs documentation in Markdown: <https://sparklogs.com/llms.txt>
