上一篇文章讲了使用Pushgateway收集业务数据的方法,今天讲另外一种方式textfile collector
The textfile collector is similar to the Pushgateway, in that it allows exporting of statistics from batch jobs. The Pushgateway should be used for service-level metrics. The textfile module is for metrics that are tied to a machine.
textfile collector 和 Pushgateway类似,都可以收集监控数据,二者的不同是 Pushgateway是一个独立的组件、textfile collector是node_exporter的附加功能,Pushgateway适用于service-level的job,textfile collector适用于machine-level的job。
textfile collector的功能就是,让node_exporter在收集数据时,顺带把指定目录下文件的内容 也加入到metrics里面
To use it, set the --collector.textfile.directory flag on the node_exporter commandline. The collector will parse all files in that directory matching the glob *.prom using the text format.
要使用textfile collector,需要在node_exporter的启动命令中增加参数--collector.textfile.directory=dir_path
例:./node_exporter --collector.textfile.directory=/root/prometheus/prom
这样node_exporter会把/root/prometheus/prom
下所有 .prom 文件的内容一并收集到metrics
.prom 文件的内容格式是这样:
# HELP http_requests_total The total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="post",code="200"} 1027
http_requests_total{method="post",code="400"} 3
# A histogram, which has a pretty complex representation in the text format:
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.05"} 24054
http_request_duration_seconds_bucket{le="0.1"} 33444
http_request_duration_seconds_bucket{le="0.2"} 100392
http_request_duration_seconds_bucket{le="0.5"} 129389
http_request_duration_seconds_bucket{le="1"} 133988
http_request_duration_seconds_bucket{le="+Inf"} 144320
http_request_duration_seconds_sum 53423
http_request_duration_seconds_count 144320
# Finally a summary, which has a complex representation, too:
# HELP rpc_duration_seconds A summary of the RPC duration in seconds.
# TYPE rpc_duration_seconds summary
rpc_duration_seconds{quantile="0.01"} 3102
rpc_duration_seconds{quantile="0.05"} 3272
rpc_duration_seconds{quantile="0.5"} 4773
rpc_duration_seconds{quantile="0.9"} 9001
rpc_duration_seconds{quantile="0.99"} 76656
rpc_duration_seconds_sum 1.7560473e+07
rpc_duration_seconds_count 2693
到prometheus页面就能查到上面的指标:
需要注意的是,如果不需要收集指标时,应该将对应的文件删除,否则node_exporter在每次收集数据时,将该文件的内容加入到metrics。(如上图,只要.prom文件没被删除或更新,就会搜到一堆不同时间戳下的重复值)
textfile collector是最简单的业务数据收集方式,不挑语言,只要有文件就行。
更多内容见官方文档