±à¼ÍƼö: |
±¾ÎÄÖ÷Òª½éÉÜPrometheus
ÊÇÓÉ SoundCloud ¿ªÔ´¼à¿Ø¸æ¾¯½â¾ö·½°¸£¬ÒÔ¼°PromQLµÄÊý¾Ý²éѯ DSL
ÓïÑÔÓëÅäÖã¬Ï£Íû¶ÔÄúµÄѧϰÓÐËù°ïÖú¡£
±¾ÎÄÀ´×ÔÓÚ¼òÊ飬ÓÉ»ðÁú¹ûÈí¼þAlice±à¼¡¢ÍƼö¡£ |
|
Prometheus ÊÇÓÉ SoundCloud ¿ªÔ´¼à¿Ø¸æ¾¯½â¾ö·½°¸¡£
prometheus
prometheus´æ´¢µÄÊÇʱÐòÊý¾Ý£¬¼´°´ÏàͬʱÐò(ÏàͬÃû³ÆºÍ±êÇ©)£¬ÒÔʱ¼äά¶È´æ´¢Á¬ÐøµÄÊý¾ÝµÄ¼¯ºÏ¡£
ʱÐò(time series)ÊÇÓÉÃû×Ö(Metric)ÒÔ¼°Ò»×ékey/value±êÇ©¶¨ÒåµÄ£¬¾ßÓÐÏàͬµÄÃû×ÖÒÔ¼°±êÇ©ÊôÓÚÏàͬʱÐò¡£
metricÃû×Ö£º±íʾmetricµÄ¹¦ÄÜ£¬Èçhttp_request_total¡£Ê±ÐòµÄÃû×ÖÓÉ ASCII
×Ö·û£¬Êý×Ö£¬Ï»®Ïߣ¬ÒÔ¼°Ã°ºÅ×é³É£¬Ëü±ØÐëÂú×ãÕýÔò±í´ïʽ [a-zA-Z_:][a-zA-Z0-9_:]*,
ÆäÃû×ÖÓ¦¸Ã¾ßÓÐÓïÒ廯£¬Ò»°ã±íʾһ¸ö¿ÉÒÔ¶ÈÁ¿µÄÖ¸±ê£¬ÀýÈç http_requests_total, ¿ÉÒÔ±íʾ
http ÇëÇóµÄ×ÜÊý¡£
±êÇ©£º
Ñù±¾£º°´ÕÕij¸öʱÐòÒÔʱ¼äά¶È²É¼¯µÄÊý¾Ý£¬³ÆÖ®ÎªÑù±¾¡£Êµ¼ÊµÄʱ¼äÐòÁУ¬Ã¿¸öÐòÁаüÀ¨Ò»¸öfloat64µÄÖµºÍÒ»¸öºÁÃë¼¶µÄʱ¼ä´Á
Ò»¸ö float64 Öµ
Ò»¸öºÁÃë¼¶µÄ unix ʱ¼ä´Á
¸ñʽ£ºPrometheusʱÐò¸ñʽÓëOpenTSDBÏàËÆ£º
<metric name>{<label
name>=<label value>, ...} |
MetricÀàÐÍ£º
Counter: Ò»ÖÖÀÛ¼ÓµÄmetric£¬ÈçÇëÇóµÄ¸öÊý£¬½áÊøµÄÈÎÎñÊý£¬³öÏֵĴíÎóÊýµÈ
Gauge: ³£¹æµÄmetric,Èçζȣ¬¿ÉÈÎÒâ¼Ó¼õ¡£ÆäΪ˲ʱµÄ£¬Óëʱ¼äûÓйØÏµµÄ£¬¿ÉÒÔÈÎÒâ±ä»¯µÄÊý¾Ý¡£
Histogram: Öù״ͼ£¬ÓÃÓÚ¹Û²ì½á¹û²ÉÑù£¬·Ö×鼰ͳ¼Æ£¬È磺ÇëÇó³ÖÐøÊ±¼ä£¬ÏìÓ¦´óС¡£ÆäÖ÷ÒªÓÃÓÚ±íʾһ¶Îʱ¼äÄÚ¶ÔÊý¾ÝµÄ²ÉÑù£¬²¢Äܹ»¶ÔÆäÖ¸¶¨Çø¼ä¼°×ÜÊý½øÐÐͳ¼Æ¡£¸ù¾Ýͳ¼ÆÇø¼ä¼ÆËã
Summary: ÀàËÆHistogram£¬ÓÃÓÚ±íʾһ¶Îʱ¼äÄÚÊý¾Ý²ÉÑù½á¹û£¬ÆäÖ±½Ó´æ´¢quantileÊý¾Ý£¬¶ø²»ÊǸù¾Ýͳ¼ÆÇø¼ä¼ÆËã³öÀ´µÄ¡£²»ÐèÒª¼ÆË㣬ֱ½Ó´æ´¢½á¹û
PromQL
PromQL (Prometheus Query Language) ÊÇ Prometheus ×Ô¼º¿ª·¢µÄÊý¾Ý²éѯ
DSL ÓïÑÔ¡£
²éѯ½á¹ûÀàÐÍ£º
˲ʱÊý¾Ý (Instant vector): °üº¬Ò»×éʱÐò£¬Ã¿¸öʱÐòÖ»ÓÐÒ»¸öµã£¬ÀýÈ磺http_requests_total
Çø¼äÊý¾Ý (Range vector): °üº¬Ò»×éʱÐò£¬Ã¿¸öʱÐòÓжà¸öµã£¬ÀýÈ磺http_requests_total[5m]
´¿Á¿Êý¾Ý (Scalar): ´¿Á¿Ö»ÓÐÒ»¸öÊý×Ö£¬Ã»ÓÐʱÐò£¬ÀýÈ磺count(http_requests_total)
²éѯÌõ¼þ£ºÍ¨¹ýÃû³Æ¼°±êÇ©½øÐвéѯ£¬Èçhttp_requests_totalµÈ¼ÛÓÚ{name="http_requests_total"}
²éѯlevel="info"µÄevent: logback_events_total{level="info"}
²éѯÌõ¼þÖ§³ÖÕýÔòÆ¥Å䣺
http_requests_total{code!="200"}
// ±íʾ²éѯ code ²»Îª "200" µÄÊý¾Ý
http_requests_total{code=¡«"2.."}
// ±íʾ²éѯ code Ϊ "2xx" µÄÊý¾Ý
http_requests_total{code!¡«"2.."}
// ±íʾ²éѯ code ²»Îª "2xx" µÄÊý¾Ý |
ÄÚÖú¯Êý£º
È罫¸¡µãÊýת»»ÎªÕûÊý£º
floor(avg(http_requests_total{code="200"}))
ceil(avg(http_requests_total{code="200"})) |
²é¿´Ã¿ÃëÊý¾Ý £º
rate(http_requests_total[5m]) |
»ù±¾²éѯ£º
1.²éѯµ±Ç°ËùÓÐÊý¾Ý
2.Ä£ºý²éѯ£º
logback_events_total{level=~"in.."}
logback_events_total{level=~"in.*"} |
3.±È½Ï²éѯ£º
4.·¶Î§²éѯ£º ¹ýÈ¥5·ÖÖÓÊý¾Ý
ʱ¼ä·¶Î§µ¥Î»ÓÐÒÔÏ£º
s: Ãë
m: ·ÖÖÓ
h: Сʱ
d: Ìì
w: ÖÜ
y: Äê
ÔÚ˲ʱÏòÁ¿±í´ïʽ»òÕßÇø¼äÏòÁ¿±í´ïʽÖУ¬¶¼ÊÇÒÔµ±Ç°Ê±¼äΪ»ù×¼¡£
Èç¹ûÏë²éѯ5·ÖîËǰµÄ˲ʱÑù±¾Êý¾Ý£¬ÔòÐèҪʹÓÃÎ»ÒÆ²Ù×÷£¬¹Ø¼ü×Ö£ºoffset,
ÆäÒª½ô¸úÔÚÑ¡ÔñÆ÷{}ºóÃæ¡£È磺
sum(http_requests_total{method="GET"}
offset 5m)
rate(http_requests_total[5m] offset 1w) |
¾ÛºÏ¡¢Í³¼Æ¸ß¼¶²éѯ£º
1. count²éѯ£º count(logback_events_total)
2. sum²éѯ£º sum(logback_events_total)
3. svg²éѯ£º
4. topk: Èç²éѯ2µÄÖµ£ºtopk(2, logback_events_total)
5. irate: Èç²éѯ¹ýÈ¥5·ÖÖӵį½¾ùÖµ: irate( logback_events_total[5m])
ÅäÖÃ
Æô¶¯Ê±£¬¿ÉÒÔ¼ÓÔØÔËÐвÎÊý-config.fileÖ¸¶¨ÅäÖÃÎļþ£¬ ĬÈÏΪprometheus.yml:
ÔÚ¸ÃÅäÖÃÎļþÖпÉÒÔÖ¸¶¨¸÷ÖÖÊôÐÔ£¬Æä½á¹¹Ì嶨ÒåÈçÏ£º
type Config struct
{
GlobalConfig GlobalConfig `yaml:"global"`
AlertingConfig AlertingConfig `yaml:"alerting,omitempty"`
RuleFiles []string `yaml:"rule_files,omitempty"`
ScrapeConfigs []*ScrapeConfig `yaml:"scrape_configs,omitempty"`
RemoteWriteConfigs []*RemoteWriteConfig `yaml:"remote_write,omitempty"`
RemoteReadConfigs []*RemoteReadConfig `yaml:"remote_read,omitempty"`
// Catches all undefined fields and must be
empty after parsing.
XXX map[string]interface{} `yaml:",inline"`
// original is the input from which the config
was parsed.
original string
}
|
È«¾ÖÅäÖÃ
global: Ö÷ÒªÓÐËĸöÊôÐÔ
scrape_interval: ÀÈ¡ targets µÄĬÈÏʱ¼ä¼ä¸ô¡£
scrape_timeout: Àȡһ¸ö target µÄ³¬Ê±Ê±¼ä¡£
evaluation_interval: Ö´ÐÐ rules µÄʱ¼ä¼ä¸ô¡£
external_labels: ¶îÍâµÄÊôÐÔ£¬»áÌí¼Óµ½ÀÈ¡µÄÊý¾Ý²¢´æµ½Êý¾Ý¿âÖС£
Exporter
¸ºÔðÊý¾Ý»ã±¨µÄ³ÌÐòͳһ½ÐExporter£¬²»Í¬µÄExporter¸ºÔð²»Í¬µÄÒµÎñ¡£ÆäͳһÃüÃû¸ñʽ£ºxx_exporter
ÒÑÓÐexporter
clientlib
pullģʽ
prometheus.ymlÄÚÈÝÈçÏ£º
global:
scrape_interval: 15s
evaluation_interval: 15s
rule_files:
# - "first.rules"
# - "second.rules"
scrape_configs:
- job_name: 'spring'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['×Ô¼º±¾»úip:8080']
|
Æô¶¯prometheus docker£º
docker run
--name prom --hostname prom -p 9090:9090 -v
/Users/liukun/config/prometheus.yml:/etc/prometheus
/prometheus.yml prom/prometheus |
Æô¶¯ÒÔºó£¬ÎÒÃÇÔËÐÐhttp://localhost:9090 ¿ÉÒÔ·ÃÎÊPrometheus¡£
Pushgateway
ʹÓÃPushgatewayÔÒò£º
Prometheus²ÉÓÃpullģʽ£¬¿ÉÄÜÓÉÓÚ²»ÔÚÒ»¸ö×ÓÍø»ò·À»ðǽµ¼ÖÂÎÞ·¨Ö±½ÓÀÈ¡¸÷targetÊý¾Ý
ÐèÒª½«²»Í¬Êý¾Ý»ã×ܺó£¬ÔÙÓÉPrometheusͳһÊÕ¼¯
Æäȱµã£º
pushgatewayå´»úÓ°Ï췶Χ»á¸ü´ó¡£
prometheusÀȡ״̬upÖ»Õë¶Ôpushgateway£¬ÎÞ·¨×öµ½¶Ôÿ¸ö½ÚµãÓÐЧ¡£
pushgateway¿ÉÒԳ־û¯ÍÆË͸øËüµÄËùÓÐ¼à¿ØÊý¾Ý
docker run -d
\
--name=pg \
-p 9091:9091 \
prom/pushgateway |
ÔÚÆäÆô¶¯ºó£¬Í¨¹ý·ÃÎÊ£ºhttp://localhost:9091¾Í¿ÉÒԲ鿴µ½Æä½çÃæ
pushgatewayĬÈÏÊDz»³Ö¾Ã»¯Êý¾ÝµÄ£¬Èç¹ûÐèÒª£¬Ôò¿ÉÒÔͨ¹ýÆô¶¯Ê±¼ÓÈë²ÎÊý
docker run -d
-p9091:9091 prom/pushgateway "-persistence.file=push_file" |
ÏòpushgatewayÍÆËÍÊý¾Ý£º
1. ʹÓÃClient SDK
2. Ö±½ÓʹÓÃAPI
ʹÓÃAPIÏòPushgatewayÍÆÊý¾Ý
ÈçÏÂΪֱ½ÓʹÓÃAPI½øÐÐÊý¾ÝÍÆËÍ£º
echo "some_metric
3.14" | curl --data-binary @- http://localhost:9091/metrics/job/some_job |
·¢Ë͸ü¸´ÔÓµÄÊý¾Ý£¬¿ÉÒÔ»¹ÉÏinstance£¬±íʾÀ´Ô´Î»Öãº
cat <<EOF
| curl --data-binary @- http://localhost:9091/metrics/job/some_job/instance /some_instance
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example
another_metric 2398.283
EOF |
ɾ³ýÊý¾Ý£ºÈç¹ûij¸ö¼à¿ØÊý¾Ý²»ÔÙÐèÒª£¬ÔòÖ»ÓÐÊÖ¶¯É¾³ý²ÅÉúЧ£¬·ñÔòÈÔÈ»²É¼¯µÄΪ¾ÉÖµ
// ɾ³ýij¸ö×éÏÂij¸öʵÀýµÄËùÓÐÊý¾Ý
curl -X DELETE http://localhost:9091/metrics/job/some_job/instance/ some_instance
// ɾ³ýij¸ö×éϵÄËùÓÐÊý¾Ý
curl -X DELETE http://localhost:9091/metrics/job/some_job |
ͨ¹ýClient SDKÍÆËÍmetricÐÅÏ¢µ½Pushgateway:
1.Ìí¼ÓpomÒÀÀµ£º
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
<version>0.6.0</version>
</dependency> |
2.Ìí¼ÓÅäÖãºÔÚPrometheusµÄÅäÖÃÎļþÖÐÅäÖã¬ÈÃÆä´ÓPushgatewayÉϽøÐÐÊý¾Ý²É¼¯£¬ÕâÀï0.51.14.23:9091ΪÎÒPushgatewayµÄµØÖ·¶Ë¿Ú¡£ÅäÖÃÍêºóÐèÒªÖØÆôʹÆäÅäÖÃÉúЧ
scrape_configs:
- job_name: 'pushgateway'
static_configs:
- targets: ['10.51.14.23:9091']
labels:
instance: "pushgateway" |
3.´úÂ룺
@Test
public void pushToGateWay() throws Exception
{
CollectorRegistry registry = new CollectorRegistry();
Gauge duration = Gauge.build().name("my_batch_job_duration_seconds")
help("Duration of my batch job in second").register(registry);
Gauge.Timer durationTimer = duration.startTimer();
try {
Gauge lastSuccess = Gauge.build().name("my_batch_job_last_success")
help("Last time my batch job successed,
in unixtime")
register(registry);
lastSuccess.setToCurrentTime();
} finally {
durationTimer.setDuration();
PushGateway pg = new PushGateway("localhost:9091");
pg.pushAdd(registry, "my_batch_job");
}
} |
|