If you are using Spring boot 2.1.5.RELEASE then
- 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>
- add config to enable access to endpoint /actuator/prometheus
management: endpoints: web: exposure: include: '*'
- 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
Answer from Sulaymon Hursanov on Stack OverflowYou can scrap metrics from your actuator endpiont in the same way as Prometheus do it. I use OkHttpClient for that. Example of my client:
OkHttpClient client = new OkHttpClient.Builder()
.retryOnConnectionFailure(false)
.followRedirects(false)
.protocols(Collections.singletonList(Protocol.H2_PRIOR_KNOWLEDGE))
.build();
All settings are optional. But pay attention to the protocol - it should be the same as on your application's server.
After that you need to build url:
String url = "http://localhost:9090/actuator/prometheus?includedNames=<nameOfThePropertyThatYouNeed>";
You can include here more than 1 property:
String url = "http://localhost:9090/actuator/prometheus?includedNames=<propertyNameOne>,<propertyNameTwo>,<propertyNameThree>";
After that you make request:
Request request =new Request.Builder()
.url(url)
.get()
.build();
Response response = client.newCall(request).execute();
String responseBody = response.body().string();
Now you need to parse responseBody. Do it in the way as Prometheus does it and use classes of Prometheus:
InputStream inputStream = new ByteArrayInputStream(responseBody.getBytes())
CollectorPrometheusMetricsWalker walker = new CollectorPrometheusMetricsWalker();
PrometheusMetricsProcessor<MetricFamily> processor = new TextPrometheusMetricsProcessor(inputStream, walker);
processor.walk();
List<MetricFamily> metricList = walker.getAllMetricFamilies();
MetricFamily object stores all metrics with the same name but with different tags.
Use metricFamily.getMetrics() to get List<Metric>
Use metric.getValue() to get the value of metric.
You should use AlertManager that is part of Prometheus suit.