为什么说是变相呢。因为Prometheus获取数据的方式一直是拉取方式,官方并没有提供推送数据的功能。但是官方为了兼容推送这种方式,增加了一个PushGateway组件。
这个组件相当于一个代理服务,独立部署。它没有数据抓取功能,只能被动的等待数据推送。应用把数据推送到PushGateway后,Prometheus再从PushGateway抓取。
适用场景:
- Prometheus 采用定时拉取模式,可能由于子网络或者防火墙的原因,不能直接拉取各个Target的指标数据,此时可以采用各个Target往PushGateway上推送数据,然后Prometheus去PushGateway上定时拉取
- 在监控各个业务数据时,需要将各个不同的业务数据进行统一汇总,此时也可以采用PushGateway来统一收集,然后Prometheus来统一拉取
Pushgateway分docker安装和普通安装两种
GitHub:https://github.com/prometheus/pushgateway
比如:
docker pull prom/pushgateway docker run -d -p 9091:9091 prom/pushgateway
启动之后,游览器访问:http://localhost:9091/#
启动成功!
prometheus 配置文件加入:
- job_name: 'pushgateway' scrape_interval: 10s # 每过10秒拉取一次 honor_labels: true static_configs: - targets: ['pushgateway:9091'] labels: instance: pushgateway
最终的docker-compose.yaml:
version: '3.4' services: prometheus: image: prom/prometheus container_name: prometheus hostname: prometheus ports: - 9090:9090 volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml grafana: image: grafana/grafana container_name: grafana hostname: grafana ports: - 3000:3000 volumes: - ./grafana.ini:/etc/grafana/grafana.ini pushgateway: image: prom/pushgateway container_name: pushgateway hostname: pushgateway ports: - 9091:9091
prometheus.yml:
global: scrape_interval: 10s scrape_configs: - job_name: 'pushgateway' scrape_interval: 10s # 每过10秒拉取一次 honor_labels: true static_configs: - targets: ['pushgateway:9091'] labels: instance: pushgateway
3、java应用接入
加入依赖:
io.prometheus simpleclient_pushgateway0.12.0
测试代码:
public static void main(String[] args) throws Exception { String url = "localhost:9091"; PushGateway pg = new PushGateway(url); Counter counterDemo = Counter.build() .name("counterChanger2").labelNames("nullRequest") .help("Counter 实例").register(); Random random = new Random(); for (int i = 0; i < 10000; i++) { int num = 1; int i1 = random.nextInt(1000); System.out.println(i1); if (i1 < 100) { num = 0; } counterDemo.labels(String.valueOf(num)).inc(); pg.push(counterDemo, "my_job"); Thread.sleep(1000); } }
pushgateway:
出现这个说明数据已经推送到pushagteway
然后查看 prometheus,up即为正常:
然后可以在grafana中创建面板:
1、添加面板
2、编辑PromQL和标题
3、编辑Y坐标单位
4、点击apply,完成保存
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)