java - 如何在 java 程序中收集 Prometheus 指标?
问题描述
我搜索了大部分文章,但没有找到我预期的解决方案。我是普罗米修斯的新手。我需要让我的 java 程序收集 Prometheus 指标。因此,需要帮助才能通过我的 java 程序读取或获取 Prometheus 指标。
任何帮助或建议都会有所帮助。
谢谢
解决方案
如果您使用的是 Spring boot 2.1.5.RELEASE 那么
- 添加依赖执行器和 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>
- 添加配置以启用对端点 /actuator/prometheus 的访问
management: endpoints: web: exposure: include: '*'
- 尝试请求
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
推荐阅读
- c# - 为什么我的光子双关语代码返回“null”?
- typescript - 这行在 VSCode 源代码中是什么意思?
- linked-list - 在连续的内存块中创建具有可变元素的链表
- php - 如何通过带有 PHP 后端的 Android 应用使用 Azure AD 对用户进行身份验证
- python - Python:当 C 字符串不是有效的 Python 字符串时,如何将其传递给操作系统?
- ruby-on-rails - 如何解决“您的 Ruby 版本是 2.6.3,但您的 Gemfile 指定了 2.7.4”我该怎么办?
- python - 如何解决:“y 应该是一维数组,取而代之的是一个形状为 (48786, 11) 的数组。” 用于序数编码器
- c++ - 我正在尝试编写一个程序,在该程序中,用户将一组数字输入到向量中,该程序会返回“统计信息”
- android - Firebase 存储:如何用新列表图像替换旧列表图像?
- c - 如何依次计算3个素数并将其中两个除以