prometheus:Prometheus是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的。
非常少的外部依赖+系统集成(docker Nginx JMX等),安装操作简单服务自动化发现,实现一种Profiling监控方式,实时分析系统运行的状态、执行时间、调用次数等,以找到系统的热点,为性能优化提供依据。
解压-由于普罗米修斯还将自己的数据公开为HTTP端点,因此可以对其自身的健康状况进行刮擦和监视。
http:// localhost:9090 进行监控,验证自身度量标准http:// localhost:9090 / metrics(其他端口后面加/metrics即可)。
下载地址https://prometheus.io/download/
tar xvfz prometheus-*.tar.gz
cd prometheus-*
./prometheus --help(查看帮助文档)
YAML 的数据组织主要依靠的是空白,缩进,分行等结构实现配置
在线YAML编辑器https://www.bejson.com/validators/yaml_editor/
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=` to any timeseries scraped from this config.
- job_name: 'tomcat'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:8080']
evaluation_interval选项控制普罗米修斯将多久评估规则(普罗米修斯使用规则来创建新的时间序列并生成警报)
rule_files块指定我们希望Prometheus服务器加载的任何规则的位置(无规则不填写)
scrape_configs控制着普罗米修斯监视的资源(由于普罗米修斯还将自己的数据公开为HTTP端点,因此可以对其自身的健康状况进行刮擦和监视)
./prometheus --config.file=prometheus.yml
输入 rate(http_requests_total[1m]) 使用graph可进行图形查看
普罗米修斯单独收集指标并不能很好地发挥普罗米修斯的能力。可使用Node Exporter来监视资源
下载地址https://prometheus.io/download/#node_exporter
tar xvfz node_exporter-*.tar.gz
cd node_exporter-*
启动:./node_exporter
使用实例
(1)下载Grafana
(2)下载DataSource—Prometheus
设置url为http://localhost:9090,监听prometheus公开的自己数据端口。eg:http://localhost:8080 job=tomcat监测tomcat数据
(3)创建DashBoard
rate(http_requests_total{job="node"}[5m]) 指标进行测试 。
class YourClass {
static final Gauge inprogressRequests = Gauge.build()
.name("inprogress_requests").help("Inprogress requests.").register();
void processRequest() {
inprogressRequest.inc();
// Your code here.
inprogressRequest.dec();
}
}
注册自定义counter,调用.inc()方法,void inc() -> void inc(double amt) -> 内部类Child中inc(double amt) -> DobleAdder().add(double x)存储到内存以供拉取。
class YourClass {
static final Summary receivedBytes = Summary.build()
.name("requests_size_bytes").help("Request size in bytes.").register();
static final Summary requestLatency = Summary.build()
.name("requests_latency_seconds").help("Request latency in seconds.").register();
void processRequest(Request req) {
Summary.Timer requestTimer = requestLatency.startTimer();
try {
// Your code here.
} finally {
receivedBytes.observe(req.size());
requestTimer.observeDuration();
}
}
}
class YourClass {
static final Gauge inprogressRequests = Gauge.build()
.name("inprogress_requests").help("Inprogress requests.").register();
void processRequest() {
inprogressRequest.inc();
// Your code here.
inprogressRequest.dec();
}
}
class YourClass {
static final Histogram requestLatency = Histogram.build()
.name("requests_latency_seconds").help("Request latency in seconds.").register();
void processRequest(Request req) {
Histogram.Timer requestTimer = requestLatency.startTimer();
try {
// Your code here.
} finally {
requestTimer.observeDuration();
}
}
}
HTTP API:
基础url+/api/v1 HTTP API网址:https://prometheus.io/docs/prometheus/latest/querying/api/#expression-queries
(1)Instant queries
GET /api/v1/query eg:curl 'http://localhost:9090/api/v1/query?query=up&time=2015-07-01T20:10:51.781Z'
up为查询条件,时间以GMT:格林尼治所在地的标准时间为准(比北京时间晚 8小时)
(2)Range queries
GET /api/v1/query_range eg:curl 'http://localhost:9090/api/v1/query_range?query=up&start=2018-01-18T20:10:30.781Z&end=2018-01-18T20:11:00.781Z&step=15s'
(3)Querying metadata
GET /api/v1/series
eg:curl -g 'http://localhost:9090/api/v1/seriesmatch[]=up&match[]=process_start_time_seconds{job="tomcat"}'
(4)Querying label values
GET /api/v1/label//values
eg:curl http://localhost:9090/api/v1/label/job/values
(5)Target
GET /api/v1/targets eg:curl http://localhost:9090/api/v1/targets
(6)Alertmanagers
GET /api/v1/alertmanagers eg:curl http://localhost:9090/api/v1/alertmanagers
io.prometheus
simpleclient
0.1.0
io.prometheus
simpleclient_hotspot
0.1.0
io.prometheus
simpleclient_httpserver
0.1.0
io.prometheus
simpleclient_pushgateway
0.1.0
网址:https://prometheus.io/docs/introduction/glossary/
Github:https://github.com/prometheus/client_java