1

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

JDGuide
  • 6,239
  • 12
  • 46
  • 64
  • Are you using a framework? Java EE, Spring, ...? Without any details, all I can suggest is to look at some example projects, or use JHipster to set up a project with metrics and take a look there. – stuXnet Jul 10 '20 at 10:12
  • I am using spring-boot application , running in a pod. – JDGuide Jul 10 '20 at 10:19

2 Answers2

1

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
  • Getting errro error: error validating "src/main/resources/xxx-autoscaler-deployment.yaml": error validating data: [ValidationError(Deployment.spec.template): unknown field "livenessProbe" in io.k8s.api.core.v1.PodTemplateSpec, ValidationError(Deployment.spec.template): unknown field "nodeSelector" in io.k8s.api.core.v1.PodTemplateSpec, ValidationError(Deployment.spec.template): unknown field "readinessProbe" in io.k8s.api.core.v1.PodTemplateSpec]; if you choose to ignore these errors, turn validation off with --validate=false – JDGuide Jul 10 '20 at 12:50
  • 1
    @JDGuide what apiVersion and kind (Pod ? ) using ? livenessProbe and readinessProbe they are optional, you can remove them for testing your app – Sulaymon Hursanov Jul 11 '20 at 13:20
  • I have removed and testing. Will update what is happening. – JDGuide Jul 11 '20 at 20:21
  • what if class is a pojo, not a spring bean? – Mandroid Dec 24 '20 at 07:25
0

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

Mohsin Khan
  • 90
  • 2
  • 11