一、背景有些时候,咱们想看每天零碎的登录人数、或者零碎中订单的数据,比方:胜利的订单、异样的订单等等。这些数据都在咱们的数据库中,通过sql_exporter咱们能够将这些数据接入到prometheus中,进行监控告警。
二、sql-exporter的应用1、下载1、拜访下载地址:https://github.com/free/sql_exporter/releases
# 下载wget https://github.com/free/sql_exporter/releases/download/0.5/sql_exporter-0.5.darwin-amd64.tar.gz# 解压tar -zxvf sql_exporter-0.5.darwin-amd64.tar.gz# 重命名mv 2、配置文件1、sql_exporter.yml# 全局配置global: # 能够了解为执行sql语句的超时工夫,这个值须要比prometheus的 `scrape_timeout` 值要小。如果配置了下方的 scrape_timeout_offset 值,那么最终的超时工夫为, min(scrape_timeout, X-Prometheus-Scrape-Timeout-Seconds - scrape_timeout_offset) # X-Prometheus-Scrape-Timeout-Seconds 为 prometheus 的超时工夫,从申请头中获取的 scrape_timeout: 10s # 从 prometheus 的超时工夫中减去一个偏移量,避免 prometheus 先超时。 scrape_timeout_offset: 500ms # 各个sql收集器之间运行距离的秒数 min_interval: 0s # 容许获取到的数据库最大的连接数, <=0 示意不限度。 max_connections: 3 # 容许闲暇连接数的个数,<=0 不做限度 max_idle_connections: 3# 配置监控的数据库和抓取信息target: # 配置数据库链接信息 # mysql://root(用户名):root(明码)@tcp(localhost:3306)/temp_work(库名) data_source_name: 'mysql://root:root@tcp(localhost:3306)/temp_work' # 收集器的名字, 对应者下方 collector_files 中具体的文件的 collector_name 的值 collectors: [collector_user,collector_payment_orders]collector_files: - "collectors/*.collector.yml"2、collectors 目录中的配置文件1、collector_user的配置cat collectors/collector_user.collector.yml统计的是 某个字段 的值,该行只有一个单个的值,比方注册数等等,对应sql 相似 select count(*) from table# 收集器的名字collector_name: collector_usermetrics: - metric_name: every_day_register_users # 指标的名字 type: counter # 类型 help: '统计每天的注册人数.' # 形容 values: [register_users] # 值 query: | select count(*) register_users from t_users where create_date between concat(curdate(),' 00:00:00') and concat(curdate(),' 23:59:59')2、collector_payment_orders的配置cat collectors/collector_payment_orders.collector.yml1、统计的是 一行只有一个值。 比方 统计每个用户(须要有一个指标名称) 的订单数量(值) 对应sql 相似 select count(1), user_name from table group by user_name 。2、统计的是一行能够有多个值。比方 统计每个用户(须要有指标名称) 的订单数量(指标名称和值)或订单金额(指标名称和值) 对应sql 相似 select user_name,count(1),sum(1) from table group by user_name。# 收集器的名字collector_name: collector_payment_ordersmetrics: - metric_name: every_day_order_cnt type: gauge help: '每个人每天的订单数量和订单数量.' key_labels: - user_name # 会存在 {"user_name"="列user_name的值"}的标签 values: - order_cnt # 指标的值是 order_cnt 的值 query: | select user_name as user_name, count(*) as order_cnt, sum(amount) as order_amount from payment_order where create_date between concat(curdate(),' 00:00:00') and concat(curdate(),' 23:59:59') group by user_name - metric_name: user_order_cnt_or_amount type: counter help: '统计每个人(user_name)在每种订单状态(order_status)下的下单数量(order_cnt)或下单金额(order_amount)' key_labels: ["user_name","order_status"] # 每一行数据上会减少 {"user_name"="列user_name的值","order_status"="列order_status的值"} 标签 value_label: 'operation' # 轻易取一个名字,比方operation ,那么依据下方的 values 会存在 {"operation"="order_cnt"}或者{"operation"="order_amount"} 标签 values: ["order_cnt","order_amount"] query: | select user_name as user_name, status as order_status, count(*) as order_cnt, sum(amount) as order_amount from payment_order group by user_name,status3、集成到prometheus中scrape_configs: - job_name: 'sql-exporter' static_configs: - targets: ['localhost:9089'] labels: nodename: 'sql-exporter'4、启动#!/usr/binnohup /Users/huan/soft/prometheus/sql_exporter/sql_exporter \--config.file="/Users/huan/soft/prometheus/sql_exporter/sql_exporter.yml" \--web.listen-address="0.0.0.0:9089" \> logs/sql_exporter.out 2>&1 &5、拜访看是否有指标数据抓取我的一个简略的了解:1、every_day_register_users 5 就是查问进去一个值2、every_day_order_cnt {user_name="lisi"} 2 多了一个自定义的label,它的值是values指定的值。3、user_order_cnt_or_amount{operation="order_amount",order_status="2",user_name="lisi"} 3user_order_cnt_or_amount{operation="order_amount",order_status="2",user_name="wangwu"} 4user_order_cnt_or_amount{operation="order_cnt",order_status="1",user_name="lisi"} 1 能够看到每个多了 user_name和order_status和operation 这3个label,该工夫序列的是 operation标签对应的列的值。
...