简体   繁体   中英

How to collect the Prometheus metrics in a java program?

I searched most of the articles, but not found my expected solutions. I am new to Prometheus. I need to get the Prometheus metrics to collect by my java program. So, need help to read or get the Prometheus metrics by my java program.

Any help or suggestion would be helpful.

Thanks

If you are using Spring boot 2.1.5.RELEASE then

  1. add dependencies actuator and 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. add config to enable access to endpoint /actuator/prometheus
management: endpoints: web: exposure: include: '*'
  1. try to request http://domain:port/actuator/prometheus

EDIT For kubernetes im using kind deployment:

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

There are bunch of ways you can do it Please refer this https://github.com/prometheus/client_java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM