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

1. Chuẩn bị

  • Thực hiện cài đặt Alloy Agent trên máy chủ Linux bằng lệnh sau:
/bin/sh -c "$(curl -fsSL https://s3hn.smartcloud.vn/agent/alloy/linux/releases/v1.8.3/install-linux.sh)"
  • Tạo một User trong PostgreSQL muốn lấy metric:
CREATE USER postgre-exporter WITH LOGIN PASSWORD 'password_postgre' CONNECTION LIMIT 20;
  • Nếu Alloy và PostgreSQL không cùng host, thêm cấu hình vào /etc/postgresql/xx/main/pg_hba.conf (xx là phiên bản PostgreSQL):
host    all             all             0.0.0.0/0               md5
  • Sau khi thêm, restart PostgreSQL.

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

sudo vi /etc/alloy/config.alloy

2.1 Thu thập Metrics ứng dụng

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-postgre
postgre-exporterTên user PostgreSQLpostgre-exporter
password_postgreMật khẩu user PostgreSQLbXlzcWxwYXNzd29yZA==
IPIP VM cài PostgreSQL10.168.15.58

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

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

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

prometheus.exporter.postgres "metrics_integrations_postgreSQL" {
  data_source_names = ["postgresql://postgre-exporter:password_postgre@IP:5432/postgres"]
}

discovery.relabel "metrics_integrations_postgreSQL" {
  targets = prometheus.exporter.postgres.metrics_integrations_postgreSQL.targets

  rule {
    source_labels = ["instance"]
    regex = "postgresql://([^:]+):[0-9]+/.*"
    target_label = "instance"
    replacement = "$1"
  }

  rule {
    target_label = "job"
    replacement = "integrations/postgres_exporter"
  }
}

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

2.2 Thu thập Logs

TrườngÝ nghĩaGiá trị mẫu
urlEndpoint APMhttps://logs-0qcs3dsvvva.apm.vnptplatform.vn/api/v1/push
AuthorizationToken dịch vụ APM494f9904-4a97-49cc-b6df-b7930fe2a123
tenantTên dịch vụ APMapm-postgre
postgre-exporterTên user PostgreSQLpostgre-exporter
password_postgreMật khẩu user PostgreSQLbXlzcWxwYXNzd29yZA==
IPIP VM cài PostgreSQL10.168.15.58

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

logging {
  level  = "warn"
  format = "logfmt"
}
loki.write "logs_integrations_postgreSQL" {
  endpoint {
    url = "<endpoint dịch vụ APM>"
    headers = {
      Authorization = "<token dịch vụ APM>",
    }
  }
  external_labels = {
    tenant = "<tên dịch vụ APM>",
  }
}

prometheus.exporter.postgres "logs_integrations_postgreSQL" {
  data_source_names = ["postgresql://postgre-exporter:password_postgre@IP:5432/postgres"]
}

local.file_match "logs_integrations_postgreSQL" {
    path_targets = [{
        __address__ = "localhost",
        __path__    = "/var/log/postgresql/*.log",
        instance    = "IP",
        job         = "integrations/postgres_exporter",
    }]
}

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

2.3 Thu thập Metrics, Logs

TrườngÝ nghĩaGiá trị mẫu
urlEndpoint APMhttps://metrics-0qcs3dsvvva.apm.vnptplatform.vn/api/v1/push
AuthorizationToken dịch vụ APM494f9904-4a97-49cc-b6df-b7930fe2a123
tenantTên dịch vụ APMapm-postgre
postgre-exporterTên user PostgreSQLpostgre-exporter
password_postgreMật khẩu user PostgreSQLbXlzcWxwYXNzd29yZA==
IPIP VM cài PostgreSQL10.168.15.58

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

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

prometheus.remote_write "metrics_integrations_postgreSQL" {
  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_postgreSQL" {
  endpoint {
    url = "<endpoint dịch vụ APM>"
    headers = {
      Authorization = "<token dịch vụ APM>",
    }
  }
  external_labels = {
    tenant = "<tên dịch vụ APM>",
  }
}

prometheus.exporter.postgres "metrics_integrations_postgreSQL" {
  data_source_names = ["postgresql://postgre-exporter:password_postgre@IP:5432/postgres"]
}

discovery.relabel "metrics_integrations_postgreSQL" {
  targets = prometheus.exporter.postgres.metrics_integrations_postgreSQL.targets

  rule {
    source_labels = ["instance"]
    regex = "postgresql://([^:]+):[0-9]+/.*"
    target_label  = "instance"
    replacement   = "$1"
  }
  rule {                
    target_label = "job"
    replacement  = "integrations/postgres_exporter"
  }
}
prometheus.scrape "metrics_integrations_postgreSQL" {
  targets    = discovery.relabel.metrics_integrations_postgreSQL.output
  forward_to = [prometheus.remote_write.metrics_integrations_postgreSQL.receiver]
  job_name   = "integrations/postgres_exporter"
}

local.file_match "logs_integrations_postgreSQL" {
    path_targets = [{
        __address__ = "localhost",
        __path__    = "/var/log/postgresql/*.log",
        instance    = "IP",
        job         = "integrations/postgres_exporter",
    }]
}

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

2.4 Thu thập Metrics ứng dụng và VM

TrườngÝ nghĩaGiá trị mẫu
urlEndpoint APMhttps://metrics-0qcs3dsvvva.apm.vnptplatform.vn/api/v1/push
AuthorizationToken dịch vụ APM494f9904-4a97-49cc-b6df-b7930fe2a123
tenantTên dịch vụ APMapm-postgre
postgre-exporterTên user PostgreSQLpostgre-exporter
password_postgreMật khẩu user PostgreSQLbXlzcWxwYXNzd29yZA==
IPIP VM cài PostgreSQL10.168.15.58
replacementIP VM cài PostgreSQL10.168.15.58

Thay các trường ở file cấu hình trên, trong đó:

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

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

prometheus.exporter.postgres "metrics_integrations_postgreSQL" {
  data_source_names = ["postgresql://postgre-exporter:password_postgre@IP:5432/postgres"]
}

discovery.relabel "metrics_integrations_postgreSQL" {
  targets = prometheus.exporter.postgres.metrics_integrations_postgreSQL.targets

  rule {
    source_labels = ["instance"]
    regex = "postgresql://([^:]+):[0-9]+/.*"
    target_label = "instance"
    replacement = "$1"
  }

  rule {
    target_label = "job"
    replacement = "integrations/postgres_exporter"
  }
}

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

discovery.relabel "metrics_integrations_linux" {
  targets = prometheus.exporter.unix.metrics_integrations_linux.targets

  rule {
    target_label = "instance"
    replacement  = "<IP VM>"
  }

  rule {
    target_label = "job"
    replacement = "integrations/node_exporter"
  }
}

prometheus.exporter.unix "metrics_integrations_linux" {
  disable_collectors = ["ipvs", "btrfs", "infiniband", "xfs", "zfs"]

  filesystem {
    fs_types_exclude     = "^(autofs|binfmt_misc|bpf|cgroup2?|configfs|debugfs|devpts|devtmpfs|tmpfs|fusectl|hugetlbfs|iso9660|mqueue|nsfs|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|selinuxfs|squashfs|sysfs|tracefs)$"
    mount_points_exclude = "^/(dev|proc|run/credentials/.+|sys|var/lib/docker/.+)($|/)"
    mount_timeout        = "5s"
  }

  netclass {
    ignored_devices = "^(veth.*|cali.*|[a-f0-9]{15})$"
  }

  netdev {
    device_exclude = "^(veth.*|cali.*|[a-f0-9]{15})$"
  }
}

prometheus.scrape "metrics_integrations_linux" {
  targets    = discovery.relabel.metrics_integrations_linux.output
  forward_to = [prometheus.relabel.metrics_integrations_linux.receiver]
}

prometheus.relabel "metrics_integrations_linux" {
  forward_to = [prometheus.remote_write.metrics_integrations_postgreSQL.receiver]

  rule {
    source_labels = ["__name__"]
    regex         = "up|node_arp_entries|node_boot_time_seconds|node_context_switches_total|node_cpu_seconds_total|node_disk_io_time_seconds_total|node_disk_io_time_weighted_seconds_total|node_disk_read_bytes_total|node_disk_read_time_seconds_total|node_disk_reads_completed_total|node_disk_write_time_seconds_total|node_disk_writes_completed_total|node_disk_written_bytes_total|node_filefd_allocated|node_filefd_maximum|node_filesystem_avail_bytes|node_filesystem_device_error|node_filesystem_files|node_filesystem_files_free|node_filesystem_readonly|node_filesystem_size_bytes|node_intr_total|node_load1|node_load15|node_load5|node_md_disks|node_md_disks_required|node_memory_Active_anon_bytes|node_memory_Active_bytes|node_memory_Active_file_bytes|node_memory_AnonHugePages_bytes|node_memory_AnonPages_bytes|node_memory_Bounce_bytes|node_memory_Buffers_bytes|node_memory_Cached_bytes|node_memory_CommitLimit_bytes|node_memory_Committed_AS_bytes|node_memory_DirectMap1G_bytes|node_memory_DirectMap2M_bytes|node_memory_DirectMap4k_bytes|node_memory_Dirty_bytes|node_memory_HugePages_Free|node_memory_HugePages_Rsvd|node_memory_HugePages_Surp|node_memory_HugePages_Total|node_memory_Hugepagesize_bytes|node_memory_Inactive_anon_bytes|node_memory_Inactive_bytes|node_memory_Inactive_file_bytes|node_memory_Mapped_bytes|node_memory_MemAvailable_bytes|node_memory_MemFree_bytes|node_memory_MemTotal_bytes|node_memory_SReclaimable_bytes|node_memory_SUnreclaim_bytes|node_memory_ShmemHugePages_bytes|node_memory_ShmemPmdMapped_bytes|node_memory_Shmem_bytes|node_memory_Slab_bytes|node_memory_SwapTotal_bytes|node_memory_VmallocChunk_bytes|node_memory_VmallocTotal_bytes|node_memory_VmallocUsed_bytes|node_memory_WritebackTmp_bytes|node_memory_Writeback_bytes|node_netstat_Icmp6_InErrors|node_netstat_Icmp6_InMsgs|node_netstat_Icmp6_OutMsgs|node_netstat_Icmp_InErrors|node_netstat_Icmp_InMsgs|node_netstat_Icmp_OutMsgs|node_netstat_IpExt_InOctets|node_netstat_IpExt_OutOctets|node_netstat_TcpExt_ListenDrops|node_netstat_TcpExt_ListenOverflows|node_netstat_TcpExt_TCPSynRetrans|node_netstat_Tcp_InErrs|node_netstat_Tcp_InSegs|node_netstat_Tcp_OutRsts|node_netstat_Tcp_OutSegs|node_netstat_Tcp_RetransSegs|node_netstat_Udp6_InDatagrams|node_netstat_Udp6_InErrors|node_netstat_Udp6_NoPorts|node_netstat_Udp6_OutDatagrams|node_netstat_Udp6_RcvbufErrors|node_netstat_Udp6_SndbufErrors|node_netstat_UdpLite_InErrors|node_netstat_Udp_InDatagrams|node_netstat_Udp_InErrors|node_netstat_Udp_NoPorts|node_netstat_Udp_OutDatagrams|node_netstat_Udp_RcvbufErrors|node_netstat_Udp_SndbufErrors|node_network_carrier|node_network_info|node_network_mtu_bytes|node_network_receive_bytes_total|node_network_receive_compressed_total|node_network_receive_drop_total|node_network_receive_errs_total|node_network_receive_fifo_total|node_network_receive_multicast_total|node_network_receive_packets_total|node_network_speed_bytes|node_network_transmit_bytes_total|node_network_transmit_compressed_total|node_network_transmit_drop_total|node_network_transmit_errs_total|node_network_transmit_fifo_total|node_network_transmit_multicast_total|node_network_transmit_packets_total|node_network_transmit_queue_length|node_network_up|node_nf_conntrack_entries|node_nf_conntrack_entries_limit|node_os_info|node_sockstat_FRAG6_inuse|node_sockstat_FRAG_inuse|node_sockstat_RAW6_inuse|node_sockstat_RAW_inuse|node_sockstat_TCP6_inuse|node_sockstat_TCP_alloc|node_sockstat_TCP_inuse|node_sockstat_TCP_mem|node_sockstat_TCP_mem_bytes|node_sockstat_TCP_orphan|node_sockstat_TCP_tw|node_sockstat_UDP6_inuse|node_sockstat_UDPLITE6_inuse|node_sockstat_UDPLITE_inuse|node_sockstat_UDP_inuse|node_sockstat_UDP_mem|node_sockstat_UDP_mem_bytes|node_sockstat_sockets_used|node_softnet_dropped_total|node_softnet_processed_total|node_softnet_times_squeezed_total|node_systemd_unit_state|node_textfile_scrape_error|node_time_zone_offset_seconds|node_timex_estimated_error_seconds|node_timex_maxerror_seconds|node_timex_offset_seconds|node_timex_sync_status|node_uname_info|node_vmstat_oom_kill|node_vmstat_pgfault|node_vmstat_pgmajfault|node_vmstat_pgpgin|node_vmstat_pgpgout|node_vmstat_pswpin|node_vmstat_pswpout|process_max_fds|process_open_fds"
    action        = "keep"
  }
}

3. Cài đặt

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

sudo systemctl start 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 PostgreSQL trên giao diện Dashboard:

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

Image title