Here is the plan: Showing my Spring boot cache metrics to Prometheus with the stack Hibernate 6, Spring boot 3, Ehcache 3, Micrometer and Prometheus.
I have two types of cache (they share the same ehcache.xml
file):
- Classic cache linked to a http service (users, I won't show the details)
- Hibernate 2nd level cache linked to the h2 db using
hibernate-jcache
(countries, I show the details)
First of all, both users and countries caches work as expected. And Prometheus shows metrics well.
However, I don't understand why the cache com.example.testcache.entity.Country
values are not correct with cache_gets_total
metrics but well evaluated for hibernate_second_level_cache_requests_total
.
Here is the Prometheus screenshot of the values
Can you explain me the results in the screenshot? Is it a bad configuration? Is there a selfish implementation not sharing his metrics?
- Thank You -
Except the application.yml
and ehcache.xml
, I don't have extra configuration, the dependencies by themselves are enough.
My ehcache.xml
works well:
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
<service>
<jsr107:defaults enable-management="true" enable-statistics="true"/>
</service>
<cache alias="com.example.testcache.entity.Country">
<expiry>
<ttl>5</ttl>
</expiry>
<heap>1000</heap>
</cache>
<cache alias="users">
<expiry>
<ttl>30</ttl>
</expiry>
<heap>100</heap>
</cache>
<cache-template name="heap-cache">
<resources>
<heap>50</heap>
<offheap unit="MB">10</offheap>
</resources>
</cache-template>
<cache alias="default-query-results-region">
<expiry><ttl unit="minutes">60</ttl></expiry>
<heap unit="entries">1000</heap>
</cache>
<cache alias="default-update-timestamps-region">
<expiry><none/></expiry>
<heap unit="entries">1000</heap>
</cache>
</config>
My application.yml
spring:
datasource:
url: jdbc:h2:mem:mydb
username: sa
password: password
driverClassName: org.h2.Driver
jpa:
database-platform: org.hibernate.dialect.H2Dialect
open-in-view: true
show-sql: true
hibernate:
ddl-auto: none
properties:
hibernate:
javax:
# ehcache configuration file for Hibernate
cache:
provider: org.ehcache.jsr107.EhcacheCachingProvider
uri: ehcache.xml
missing_cache_strategy: fail
cache:
use_second_level_cache: true
use_query_cache: true
region:
factory_class: jcache
generate_statistics: true
# ehcache configuration file for the other services "users" (not Hibernate)
cache:
jcache:
config: classpath:ehcache.xml
h2:
console:
enabled: true
management:
endpoint:
health:
show-details: always
enabled: true
info:
enabled: true
prometheus:
enabled: true
endpoints:
web:
exposure:
include: "*"
tracing:
sampling:
probability: 1.0
My pom.xml
with the required dependencies :
ehcache
,hibernate-jcache
micrometer-tracing-bridge-brave
,hibernate-micrometer
,datasource-micrometer-spring-boot
micrometer-registry-prometheus
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>test-cache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test-cache</name>
<description>test-cache</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-jcache</artifactId>
<version>6.1.7.Final</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-micrometer</artifactId>
<version>6.2.0.Final</version>
</dependency>
<dependency>
<groupId>net.ttddyy.observation</groupId>
<artifactId>datasource-micrometer-spring-boot</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
And finally FYI my com.example.testcache.entity.Country
entity using the Hibernate @Cache
annotation:
package com.example.testcache.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Data;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Data
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}