HAProxy

Tài liệu hướng dẫn cấu hình Alloy để thu thập Metrics từ ứng dụng HAProxy

1. Chuẩn bị

/bin/sh -c "$(curl -fsSL https://s3hn.smartcloud.vn/agent/alloy/linux/releases/v1.8.3/install-linux.sh)"
  • Đã cài đặt HAProxy trên VM
  • Cấu hình HAProxy để mở /metrics endpoint theo đường dẫn /etc/haproxy/haproxy.cfg
defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend metrics
    bind *:8404
    mode http
    http-request use-service prometheus-exporter if { path /metrics }

2. Cấu hình tích hợp

Tại đường dẫn /etc/alloy/config.alloy, chỉnh sửa Alloy để tích hợp HAProxy:

sudo vi /etc/alloy/config.alloy

Thay các trường trong cấu hình sau, trong đó:

TrườngÝ nghĩaGiá trị mẫu
urlEndpoint của dịch vụ APM trên giao diện Consolehttps://metrics-0qcs3dsvvva.apm.vnptplatform.vn/api/v1/push
AuthorizationToken của dịch vụ APM trên giao diện Consolea5130b91-ff9b-41ef-8722-9d923a574123
tenantTên dịch vụ APMapm-cvnptcloud
IPIP VM đang chạy HAProxy10.168.17.30
logging {
    level  = "warn"
    format = "logfmt"
}
prometheus.remote_write "metrics_integrations_haproxy" {
    endpoint {
        url = "<endpoint dịch vụ APM>"
        headers = {
          "Authorization" = "<token dịch vụ APM>",
        }
    }
    external_labels = {
        tenant = "<tên dịch vụ APM>",
    }
}
discovery.relabel "metrics_integrations_haproxy" {
    targets = [{
        __address__ = "<IP>:8404",
    }]

    rule {
        source_labels = ["__address__"]
        regex         = "([^:]+):.*"
        target_label  = "instance"
        replacement   = "$1"
    }
}

prometheus.scrape "metrics_integrations_haproxy" {
    targets    = discovery.relabel.metrics_integrations_haproxy.output
    forward_to = [prometheus.relabel.metrics_integrations_haproxy.receiver]
    job_name   = "integrations/haproxy"
}

prometheus.relabel "metrics_integrations_haproxy" {
    forward_to = [prometheus.remote_write.metrics_integrations_haproxy.receiver]

    rule {
        source_labels = ["__name__"]
        regex         = "up|haproxy_backend_bytes_in_total|haproxy_backend_bytes_out_total|haproxy_backend_check_up_down_total|haproxy_backend_connect_time_average_seconds|haproxy_backend_connection_attempts_total|haproxy_backend_connection_errors_total|haproxy_backend_http_requests_total|haproxy_backend_internal_errors_total|haproxy_backend_max_connect_time_seconds|haproxy_backend_max_queue_time_seconds|haproxy_backend_max_response_time_seconds|haproxy_backend_max_total_time_seconds|haproxy_backend_queue_time_average_seconds|haproxy_backend_response_errors_total|haproxy_backend_response_time_average_seconds|haproxy_backend_status|haproxy_backend_total_time_average_seconds|haproxy_frontend_bytes_in_total|haproxy_frontend_bytes_out_total|haproxy_frontend_connections_total|haproxy_frontend_http_requests_total|haproxy_frontend_internal_errors_total|haproxy_frontend_request_errors_total|haproxy_frontend_status|haproxy_process_current_connections|haproxy_process_dropped_logs_total|haproxy_process_limit_connection_rate|haproxy_process_limit_session_rate|haproxy_process_limit_ssl_rate|haproxy_process_max_connections|haproxy_process_max_fds|haproxy_process_max_memory_bytes|haproxy_process_max_pipes|haproxy_process_max_sockets|haproxy_process_nbproc|haproxy_process_nbthread|haproxy_process_pool_allocated_bytes|haproxy_process_pool_used_bytes|haproxy_process_start_time_seconds|haproxy_server_bytes_in_total|haproxy_server_bytes_out_total|haproxy_server_check_up_down_total|haproxy_server_connect_time_average_seconds|haproxy_server_connection_attempts_total|haproxy_server_connection_errors_total|haproxy_server_http_responses_total|haproxy_server_internal_errors_total|haproxy_server_max_connect_time_seconds|haproxy_server_max_queue_time_seconds|haproxy_server_max_response_time_seconds|haproxy_server_max_total_time_seconds|haproxy_server_queue_time_average_seconds|haproxy_server_response_errors_total|haproxy_server_response_time_average_seconds|haproxy_server_status|haproxy_server_total_time_average_seconds"
        action        = "keep"
    }
}

2. Cài đặt

Thực hiện lệnh sau để khởi động Alloy:

sudo systemctl start alloy

3. Kiểm tra

3.1 Kiểm tra dịch vụ

Thực hiện lệnh sau:

sudo systemctl status alloy

Kết quả nếu thành công:

 alloy.service - Vendor-agnostic OpenTelemetry Collector distribution with programmable pipelines
    Loaded: loaded (/lib/systemd/system/alloy.service; enabled; vendor preset: enabled)
    Active: active (running) since Wed 2025-04-23 10:44:50 +07; 5s ago
      Docs: https://grafana.com/docs/alloy
  Main PID: 3119454 (alloy)
     Tasks: 13 (limit: 9385)
    Memory: 61.3M
       CPU: 848ms
    CGroup: /system.slice/alloy.service
            └─3119454 /usr/bin/alloy run --server.http.listen-addr=0.0.0.0:30000 --storage.path=/var/lib/alloy/data /etc/alloy/config.alloy

Nếu chưa thành công, thực hiện lệnh sau để xem log của Alloy:

sudo journalctl -u alloy

Sau đó, sửa lại giá trị của cấu hình và restart lại Alloy:

sudo systemctl restart alloy

3.2 Kiểm tra tích hợp

Tham khảo hướng dẫn tích hợp dashboard, kiểm tra metrics HAProxy trên giao diện Dashboard:

Một số dashboard mẫu khi tích hợp thành công: