---
{% set base_endpoint = (observability_endpoint | default('', true) | regex_replace('/+$', '') | regex_replace('/ingest/otlp.*$', '')) %}
{% set collector_is_local = observability_collector_is_local | default(false) | bool %}
{% set default_metrics_endpoint = 'http://127.0.0.1:' ~ (vmetrics_port | default(8428) | string) ~ '/api/v1/write' if collector_is_local else base_endpoint ~ '/ingest/metrics/api/v1/write' %}
{% set default_logs_endpoint = 'http://127.0.0.1:' ~ (vlogs_port | default(9428) | string) ~ '/insert' if collector_is_local else base_endpoint ~ '/ingest/logs/insert' %}
{% set metrics_endpoint = observability_metrics_endpoint | default(default_metrics_endpoint, true) %}
{% set logs_endpoint = observability_logs_endpoint | default(default_logs_endpoint, true) %}
data_dir: {{ vector_data }}

api:
  enabled: true

sources:
  internal_metrics:
    type: internal_metrics
    scrape_interval_secs: 15

  node_exporter:
    type: prometheus_scrape
    endpoints:
      - http://127.0.0.1:{{ node_exporter_port | default(9100) }}{{ exporter_metrics_path | default('/metrics') }}
    scrape_interval_secs: 15

  process_exporter:
    type: prometheus_scrape
    endpoints:
      - http://127.0.0.1:{{ process_exporter_port | default(9256) }}/metrics
    scrape_interval_secs: 15

  journald:
    type: journald
    current_boot_only: true

  syslog_files:
    type: file
    include:
      - /var/log/syslog
      - /var/log/messages
      - /var/log/auth.log
    read_from: end

transforms:
  agent_metrics:
    type: remap
    inputs: ["node_exporter", "process_exporter"]
    source: |
      .tags.host = "{{ ansible_hostname | default(nodename | default(inventory_hostname)) }}"
      .tags.ip = "{{ inventory_hostname }}"
      .tags.ins = "{{ nodename | default(inventory_hostname) }}"
      .tags.cls = "{{ node_cluster | default('nodes') }}"
      .tags.job = "node"
      .tags.origin = "vector-agent"

  agent_logs:
    type: remap
    inputs: ["journald", "syslog_files"]
    source: |
      .host = "{{ ansible_hostname | default(nodename | default(inventory_hostname)) }}"
      .ip = "{{ inventory_hostname }}"
      .ins = "{{ nodename | default(inventory_hostname) }}"
      .cls = "{{ node_cluster | default('nodes') }}"
      .job = "node"
      .origin = "vector-agent"
      .timestamp = now()

sinks:
  vector_metrics:
    type: prometheus_exporter
    inputs: ["internal_metrics"]
    address: 0.0.0.0:{{ vector_port }}
    default_namespace: vector

  observability_metrics:
    type: prometheus_remote_write
    inputs: ["agent_metrics"]
    endpoint: "{{ metrics_endpoint }}"
{% if observability_ingest_basic_auth_enabled | default(false) %}
    auth:
      strategy: basic
      user: "{{ observability_ingest_basic_auth_user }}"
      password: "{{ observability_ingest_basic_auth_password }}"
{% endif %}
    compression: snappy
    healthcheck: false

  observability_logs:
    type: loki
    inputs: ["agent_logs"]
    endpoint: "{{ logs_endpoint }}"
{% if observability_ingest_basic_auth_enabled | default(false) %}
    auth:
      strategy: basic
      user: "{{ observability_ingest_basic_auth_user }}"
      password: "{{ observability_ingest_basic_auth_password }}"
{% endif %}
    compression: gzip
    encoding:
      codec: json
    labels:
      host: "{{ '{{ host }}' }}"
      job: "{{ '{{ job }}' }}"
      origin: "{{ '{{ origin }}' }}"
