Tài liệu này hướng dẫn cấu hình Alloy để thu thập Metrics, Logs của dịch vụ Nginx

1. Chuẩn bị

  • Đã cài đặt Alloy trên VM
  • Đã cài đặt Nginx trên VM
  • Thêm đoạn cấu hình sau vào block http trong file /etc/nginx/nginx.conf
log_format json_analytics escape=json '{'
'"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
'"connection": "$connection", ' # connection serial number
'"connection_requests": "$connection_requests", ' # number of requests made in connection
'"pid": "$pid", ' # process pid
'"request_id": "$request_id", ' # the unique request id
'"request_length": "$request_length", ' # request length (including headers and body)
'"remote_addr": "$remote_addr", ' # client IP
'"remote_user": "$remote_user", ' # client HTTP username
'"remote_port": "$remote_port", ' # client port
'"time_local": "$time_local", '
'"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
'"request": "$request", ' # full path no arguments if the request
'"request_uri": "$request_uri", ' # full path and arguments if the request
'"args": "$args", ' # args
'"status": "$status", ' # response status code
'"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
'"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
'"http_referer": "$http_referer", ' # HTTP referer
'"http_user_agent": "$http_user_agent", ' # user agent
'"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
'"http_host": "$http_host", ' # the request Host: header
'"server_name": "$server_name", ' # the name of the vhost serving the request
'"request_time": "$request_time", ' # request processing time in seconds with msec resolution
'"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
'"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
'"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
'"upstream_response_time": "$upstream_response_time", ' # time spent receiving upstream body
'"upstream_response_length": "$upstream_response_length", ' # upstream response length
'"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
'"ssl_protocol": "$ssl_protocol", ' # TLS protocol
'"ssl_cipher": "$ssl_cipher", ' # TLS cipher
'"scheme": "$scheme", ' # http or https
'"request_method": "$request_method", ' # request method
'"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
'"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
'"gzip_ratio": "$gzip_ratio"'
'}';
  • Thêm đoạn cấu hình sau vào file /etc/nginx/sites-enabled/default
server {
  listen 127.0.0.1:8080;
  server_name localhost;

  location /stub_status {
    stub_status on;
    # Optionally restrict access to IP addresses if needed
    # allow 127.0.0.1;
    # deny all;
  }
}
  • Thực hiện lệnh sau để kiểm tra cấu hình Nginx
sudo nginx -t
  • Reload lại cấu hình Nginx
sudo systemctl reload nginx
  • Cài đặt Nginx-Prometheus-Exporter

Lưu ý: Nếu đã có exporter vui lòng bỏ qua bước này

Thay phiên bản và kiến trúc máy tương ứng tại đây.

wget  https://github.com/nginx/nginx-prometheus-exporter/releases/download/v1.4.2/nginx-prometheus-exporter_1.4.2_linux_amd64.tar.gz
  • Giải nén file vừa tải
tar -xzvf nginx-prometheus-exporter-1.4.2-linux-amd64.tar.gz
  • Di chuyển file nhị phân vào thư mục
sudo mv nginx-prometheus-exporter /usr/local/bin/
  • Cấp quyền thực thi
sudo chmod +x /usr/local/bin/nginx-prometheus-exporter
  • Chạy exporter của Nginx
nginx-prometheus-exporter --nginx.scrape-uri=http://IP_VM:8080/stub_status

Lưu ý: Đảm bảo server đã cấu hình metrics thành công

curl http://localhost:8080/stub\_status

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 Nginx:

sudo vi /etc/alloy/config.alloy

2.1 Thu thập Metrics ứng dụng

Thay các trường ở file 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
clusterTên dịch vụ APMCluster-IDC
__address__IP của VM10.168.17.30

Thay đổi các thông tin theo cấu hình:

logging {
    level  = "warn"
    format = "logfmt"
}

prometheus.remote_write "metrics_integrations_nginx" {
    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_nginx" {
    targets = [{
                __address__  = "<IP VM>:9113",
    }]

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

prometheus.scrape "metrics_integrations_nginx" {
    targets = discovery.relabel.metrics_integrations_nginx.output
    forward_to = [prometheus.remote_write.metrics_integrations_nginx.receiver]
    job_name   = "integrations/nginx"
}

2.2 Thu thập Logs

Thay các trường ở file cấu hình trên, 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ụ APMTenant-IDC
__address__IP của VM10.168.17.30

Thay đổi các thông tin theo cấu hình:

logging {
    level  = "warn"
    format = "logfmt"
}

loki.write "logs_integrations_nginx" {
    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 "logs_integrations_nginx" {
    targets = [{
        __address__  = "<IP VM>:9113",
    }]

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


local.file_match "logs_integrations_nginx" {
    path_targets = [{
        __address__ = "localhost",
        __path__    = "/var/log/nginx/*.log",
        host        = "localhost",
        instance    = "IP-VM",
        job         = "integrations/nginx",
    }]
}

loki.source.file "logs_integrations_nginx" {
    targets    = local.file_match.logs_integrations_nginx.targets
    forward_to = [loki.write.logs_integrations_nginx.receiver]
}

2.3 Thu thập Metrics, Logs

Thay các trường ở file cấu hình trên, 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ụ APMTenant-IDC
__address__IP của VM10.168.17.30

Thay đổi các thông tin theo cấu hình:

logging {
    level  = "warn"
    format = "logfmt"
}

prometheus.remote_write "metrics_integrations_nginx" {
    endpoint {
        url = "<endpoint dịch vụ APM>"
        headers = {
            Authorization = "<token dịch vụ APM>",
        }
    }
    external_labels = {
        tenant = "<tên dịch vụ APM>",
    }
}

loki.write "logs_integrations_nginx" {
    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_nginx" {
    targets = [{
        __address__  = "<IP VM>:9113",
    }]

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

prometheus.scrape "metrics_integrations_nginx" {
    targets = discovery.relabel.metrics_integrations_nginx.output
    forward_to = [prometheus.remote_write.metrics_integrations_nginx.receiver]
    job_name   = "integrations/nginx"
}
local.file_match "logs_integrations_nginx" {
    path_targets = [{
        __address__ = "localhost",
        __path__    = "/var/log/nginx/*.log",
        host        = "localhost",
        instance    = "IP-VM",
        job         = "integrations/nginx",
    }]
}

loki.source.file "logs_integrations_nginx" {
    targets    = local.file_match.logs_integrations_nginx.targets
    forward_to = [loki.write.logs_integrations_nginx.receiver]
}

3. Cài đặt

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

sudo systemctl restart alloy 

4. Kiểm tra

4.1 Kiểm tra dịch vụ

Thực hiện lệnh sau:

sudo systemctl status alloy 

Hệ thống trả về như sau 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 Thu 2025-04-24 08:56:32 +07; 2s ago
       Docs: https://grafana.com/docs/alloy
   Main PID: 3815363 (alloy)
      Tasks: 13 (limit: 9385)
     Memory: 54.9M
        CPU: 583ms
     CGroup: /system.slice/alloy.service
             └─3815363 /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 không thành công, xem log Alloy:

sudo journalctl -u alloy 

Sau đó chỉnh sửa cấu hình nếu cần và khởi động lại:

sudo systemctl restart alloy 

4.2 Kiểm tra tích hợp

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