sql_exporter通过sql收集业务数据并通过prometheus+grafana展示

下载并解压安装sql_exporter

bash 复制代码
wget https://github.com/free/sql_exporter/releases/download/0.5/sql_exporter-0.5.linux-amd64.tar.gz
#解压
tar xvf sql_exporter-0.5.linux-amd64.tar.gz -C /usr/local/

修改主配置文件

bash 复制代码
cd /usr/local/
mv sql_exporter-0.5.linux-amd64 sql_exporter
cd /usr/local/sql_exporter
cat /usr/local/sql_exporter/config.yml
yml 复制代码
# Global defaults.
global:
  # Subtracted from Prometheus' scrape_timeout to give us some headroom and prevent Prometheus from timing out first.
  scrape_timeout_offset: 500ms
  # Minimum interval between collector runs: by default (0s) collectors are executed on every scrape.
  min_interval: 0s
  # Maximum number of open connections to any one target. Metric queries will run concurrently on multiple connections,
  # as will concurrent scrapes.
  max_connections: 3
  # Maximum number of idle connections to any one target. Unless you use very long collection intervals, this should
  # always be the same as max_connections.
  max_idle_connections: 3
  # Maximum number of maximum amount of time a connection may be reused. Expired connections may be closed lazily before reuse.
  # If 0, connections are not closed due to a connection's age.
  max_connection_lifetime: 5m

# The target to monitor and the collectors to execute on it.
target:
  # Data source name always has a URI schema that matches the driver name. In some cases (e.g. MySQL)
  # the schema gets dropped or replaced to match the driver expected DSN format.
  data_source_name: 'mysql://prometheus_user:prometheus_password@dbIP:3306/DBname'

  # Collectors (referenced by name) to execute on the target.
  # Glob patterns are supported (see <https://pkg.go.dev/path/filepath#Match> for syntax).
  collectors: [DBname*]

# Collector files specifies a list of globs. One collector definition is read from each matching file.
# Glob patterns are supported (see <https://pkg.go.dev/path/filepath#Match> for syntax).
collector_files: 
  - "/usr/local/sql_exporter/DBname_tables.yml"

编写sql查询配置文件

yml 复制代码
cat /usr/local/sql_exporter/DBname_tables.yml

collector_name: DBname_tables
metrics:
- metric_name: member_register_count_today
  type: gauge
  help: '当日会员注册数'
  values: [member_register_count_today]
  query: |
        SELECT COUNT(0) as 'member_register_count_today' FROM manage_member WHERE DATE(create_time)=CURDATE()

编写启动脚本并启动

bash 复制代码
cat > /usr/lib/systemd/system/sql-exporter-DBname.service <<EOF
[Unit]
Description=sql_exporter

[Service]
ExecStart=/usr/local/sql_exporter/sql_exporter -config.file /usr/local/sql_exporter/config.yml  -web.listen-address 0.0.0.0:9501
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable sql-exporter-DBname.service --now

接入prometheus

yaml 复制代码
  - job_name: 'sql'
    scrape_interval: 1m
    scrape_timeout: 50s
    static_configs:
      - targets:
        - 'localhost:9501'

prometheus热加载生效

curl -X POST http://localhost:9090/-/reload

prometheus查看监控指标是否收集

curl http://localhost:9501/metrics

grafana配置监控面板

配置会员注册趋势

复制代码
#当日
floor(delta(member_register_count_today[$__rate_interval]))>0 or (member_register_count_today*0)
昨日
floor(delta(member_register_count_today[$__rate_interval] offset 1d)) > 0 or (member_register_count_today*0)
相关推荐
Databend8 小时前
Databend 增量物化视图:基于 Change Tracking 的增量刷新与一致性读取
大数据·数据库·sql
疯狂打码的少年11 小时前
【数据库技术】SQL数据查询(SELECT基本语法)
数据库·笔记·sql·oracle
liuyicenysabel11 小时前
Grafana + Prometheus 分级告警配置设计(P0/P1/P2)
javascript·grafana·prometheus
随遇而安zx11 小时前
SpringCloud---可观测性与监控:Actuator / Micrometer / Prometheus / Grafana 深度解析
spring cloud·grafana·prometheus
qq_4523962313 小时前
第四篇:《Prometheus 深度实战:指标设计、Exporter 开发与服务发现》
服务发现·prometheus
刘某的Cloud15 小时前
k8s部署prometheus架构规则
linux·运维·kubernetes·prometheus·监控
小白说大模型15 小时前
AI 提示词专栏:Zero-Shot 与 One-Shot Prompt 的差别与适用场景
大数据·数据库·人工智能·sql·深度学习·prompt
Sagittarius_A*15 小时前
【好靶场】SQL 注入-布尔盲注-2
sql·web安全·网络安全·sql注入
疯狂打码的少年16 小时前
【数据库技术】SQL高级查询(分组/排序/聚合函数)
java·数据库·笔记·sql
LlmCraft|大模型工程实践16 小时前
12. Docker 日志管理与监控:ELK 日志收集 + Prometheus 性能监控
elk·docker·prometheus