id: vector-vrl-transform-unit-test-and-topology-gate
namespace: company.observability
inputs:
- id: git_repository_url
type: STRING
displayName: Git Repository URL
required: false
description: HTTPS URL of the Git repository that holds the Vector configuration
and its unit tests. Webhook runs take it from the push payload
(repository.clone_url), so set it for manual runs.
- id: git_branch
type: STRING
displayName: Git Branch
defaults: main
description: Branch to check out for manual runs. Webhook runs take it from the
push payload (ref).
- id: config_path
type: STRING
displayName: Vector Config Path
defaults: vector.yaml
description: Path of the root Vector configuration file, relative to the
repository root.
- id: tests_dir
type: STRING
displayName: Vector Tests Directory
defaults: tests
description: Directory with Vector unit test files (*.yaml), relative to the
repository root. Tests defined inside the config file are run too.
- id: slack_channel
type: STRING
displayName: Slack Channel
defaults: "#observability-alerts"
description: Slack channel for gate rejection alerts.
triggers:
- id: webhook_ci
type: io.kestra.plugin.core.trigger.Webhook
description: Receives a GitHub push webhook and checks out the pushed repository
and ref before running the gate.
key: "{{ secret('VECTOR_GATE_WEBHOOK_KEY') }}"
tasks:
- id: workspace
type: io.kestra.plugin.core.flow.WorkingDirectory
description: Shared working directory that holds the cloned repository, the
Vector reports and the parsed metrics.
tasks:
- id: clone_repo
type: io.kestra.plugin.git.Clone
description: Checks out the repository that contains the Vector configuration.
url: "{{ trigger.body.repository.clone_url is defined ?
trigger.body.repository.clone_url : inputs.git_repository_url }}"
branch: "{{ trigger.body.ref is defined ? trigger.body.ref : inputs.git_branch
}}"
- id: test_and_validate_vector
type: io.kestra.plugin.scripts.shell.Commands
description: Runs vector validate and vector test inside the official Vector
container and saves both reports.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
entryPoint: []
containerImage: timberio/vector:0.43.0-alpine
env:
CONFIG_PATH: "{{ inputs.config_path }}"
TESTS_DIR: "{{ inputs.tests_dir }}"
NO_COLOR: "1"
outputFiles:
- validation_report.txt
- test_report.txt
- summary.properties
commands:
- |
set +e
echo "=== Step 1: Validating Vector configuration ==="
vector validate --no-environment "$CONFIG_PATH" > validation_report.txt 2>&1
VAL_CODE=$?
echo "Validation exit code: $VAL_CODE"
echo "=== Step 2: Running VRL unit tests ==="
if [ $VAL_CODE -eq 0 ]; then
if ls "$TESTS_DIR"/*.yaml > /dev/null 2>&1; then
vector test "$CONFIG_PATH" "$TESTS_DIR"/*.yaml > test_report.txt 2>&1
else
vector test "$CONFIG_PATH" > test_report.txt 2>&1
fi
TEST_CODE=$?
else
echo "Vector validation failed; unit tests were skipped." > test_report.txt
TEST_CODE=1
fi
echo "Test exit code: $TEST_CODE"
echo "VALIDATE_EXIT_CODE=$VAL_CODE" > summary.properties
echo "TEST_EXIT_CODE=$TEST_CODE" >> summary.properties
echo "--- Validation report ---"
cat validation_report.txt
echo "--- Unit test report ---"
cat test_report.txt
exit 0
- id: parse_test_metrics
type: io.kestra.plugin.scripts.python.Script
description: Reads both Vector reports and classifies the run as PASSED,
VALIDATION_FAILED, TESTS_FAILED or NO_TESTS_FOUND.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
entryPoint: []
containerImage: python:3.12-slim
beforeCommands:
- pip install --quiet kestra
script: |
import os
import re
from kestra import Kestra
ANSI = re.compile(r"\x1b\[[0-9;]*m")
def read(path):
if not os.path.exists(path):
return ""
with open(path, "r", encoding="utf-8", errors="replace") as f:
return ANSI.sub("", f.read()).strip()
codes = {}
for line in read("summary.properties").splitlines():
key, _, value = line.partition("=")
codes[key.strip()] = int(value.strip())
val_code = codes.get("VALIDATE_EXIT_CODE", 1)
test_code = codes.get("TEST_EXIT_CODE", 1)
val_report = read("validation_report.txt")
test_report = read("test_report.txt")
# vector test prints one "test <name> ... passed|failed" line per test
passed_count = len(re.findall(r"^test .+ \.\.\. passed\s*$", test_report, re.MULTILINE))
failed_count = len(re.findall(r"^test .+ \.\.\. failed\s*$", test_report, re.MULTILINE))
total_count = passed_count + failed_count
if val_code != 0:
gate_status = "VALIDATION_FAILED"
failure_details = val_report[:500] if val_report else "Vector validation failed."
elif test_code != 0 or failed_count > 0:
gate_status = "TESTS_FAILED"
lines = [l.strip() for l in test_report.splitlines() if "failed" in l.lower() or "error" in l.lower()]
failure_details = " | ".join(lines[:5]) if lines else test_report[:500]
elif total_count == 0:
gate_status = "NO_TESTS_FOUND"
failure_details = "vector test ran no unit tests, so the VRL transforms are untested."
else:
gate_status = "PASSED"
failure_details = "Vector validation and all unit tests passed."
Kestra.outputs({
"gate_status": gate_status,
"tests_passed": passed_count,
"tests_failed": failed_count,
"total_tests": total_count,
"failure_details": failure_details
})
- id: evaluate_gate
type: io.kestra.plugin.core.flow.Switch
description: Logs a pass, or alerts Slack and fails the execution when
validation or tests failed or no tests exist.
value: "{{ outputs.parse_test_metrics.vars.gate_status }}"
cases:
PASSED:
- id: log_gate_passed
type: io.kestra.plugin.core.log.Log
message: "Vector gate PASSED: {{ outputs.parse_test_metrics.vars.tests_passed }}
of {{ outputs.parse_test_metrics.vars.total_tests }} unit test(s)
passed and the configuration validated."
TESTS_FAILED:
- id: alert_test_failure_slack
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Tells the observability team that VRL unit tests failed.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": {{ inputs.slack_channel | toJson }},
"text": {{ ("Vector VRL unit test gate REJECTED: " ~ (outputs.parse_test_metrics.vars.tests_failed | string) ~ " of " ~ (outputs.parse_test_metrics.vars.total_tests | string) ~ " test(s) failed. Details: " ~ outputs.parse_test_metrics.vars.failure_details ~ " Execution: " ~ execution.id) | toJson }}
}
- id: fail_on_test_regression
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Vector VRL unit tests failed ({{
outputs.parse_test_metrics.vars.tests_failed }} of {{
outputs.parse_test_metrics.vars.total_tests }}). Gate rejected."
VALIDATION_FAILED:
- id: alert_validation_failure_slack
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Tells the observability team that the Vector configuration is
invalid.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": {{ inputs.slack_channel | toJson }},
"text": {{ ("Vector configuration validation FAILED. Details: " ~ outputs.parse_test_metrics.vars.failure_details ~ " Execution: " ~ execution.id) | toJson }}
}
- id: fail_on_invalid_config
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Vector configuration validation failed. Gate rejected."
NO_TESTS_FOUND:
- id: alert_no_tests_slack
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Tells the observability team that no unit tests were found.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": {{ inputs.slack_channel | toJson }},
"text": {{ ("Vector gate REJECTED: no unit tests were found, so the VRL transforms are untested. Execution: " ~ execution.id) | toJson }}
}
- id: fail_on_no_tests
type: io.kestra.plugin.core.execution.Fail
errorMessage: "No Vector unit tests were found in '{{ inputs.tests_dir }}' or in
the config, so the gate cannot pass."
defaults:
- id: log_unexpected_status
type: io.kestra.plugin.core.log.Log
message: "Unexpected gate status: {{ outputs.parse_test_metrics.vars.gate_status
}}"
errors:
- id: alert_pipeline_error
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alerts the team when the flow stops with an error, whether the gate
rejected the config or a task failed.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"channel": {{ inputs.slack_channel | toJson }},
"text": {{ ("Vector gate flow " ~ flow.id ~ " stopped with an error in execution " ~ execution.id ~ ".") | toJson }}
}