首页 > 解决方案 > 如何在 java 程序中收集 Prometheus 指标?

问题描述

我搜索了大部分文章,但没有找到我预期的解决方案。我是普罗米修斯的新手。我需要让我的 java 程序收集 Prometheus 指标。因此,需要帮助才能通过我的 java 程序读取或获取 Prometheus 指标。

任何帮助或建议都会有所帮助。

谢谢

标签: javaprometheusprometheus-node-exporterprometheus-java

解决方案


如果您使用的是 Spring boot 2.1.5.RELEASE 那么

  1. 添加依赖执行器和 micrometer-prometheus
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
 <dependency> 
   <groupId>io.micrometer</groupId>
   <artifactId>micrometer-registry-prometheus</artifactId>
 </dependency>
  1. 添加配置以启用对端点 /actuator/prometheus 的访问
management:
  endpoints:
    web:
      exposure:
       include: '*'
  1. 尝试请求http://domain:port/actuator/prometheus

编辑对于 kubernetes 即时使用 kind 部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myAppName
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myAppName
  template:
    metadata:
      labels:
        app: myAppName
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "8091"
        prometheus.io/path: "/actuator/prometheus"
    spec:
      containers:
        - name: myAppName
          image: images.com/app-service:master
          imagePullPolicy: Always
          ports:
            - containerPort: 8091
          env:
            - name: INSTANCE_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: SPRING_PROFILES_ACTIVE
              value: "prod"
            - name: CONFIG_SERVER_ADDRESS
              value: "http://config-server:8888"
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /actuator/health
              port: 8091
              scheme: HTTP
            initialDelaySeconds: 45
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5
          readinessProbe:
            failureThreshold: 5
            httpGet:
              path: /actuator/health
              port: 8091
              scheme: HTTP
            initialDelaySeconds: 30
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5
      nodeSelector:
        servicetype: mvp-cluster

推荐阅读