mn create-app example.micronaut.micronautguide \
--features=micrometer-annotation,data-jdbc,flyway,validation,http-client \
--build=gradle \
--lang=groovy \
--test=spock
Collect Metrics with Micronaut
Learn how to collect standard and custom metrics with the Micronaut framework.
Authors: Burt Beckwith
Micronaut Version: 4.6.3
1. Getting Started
In this guide, we will create a Micronaut application written in Groovy.
We’ll use Micronaut Micrometer to expose application metric data with Micrometer.
2. What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 17 or greater installed with
JAVA_HOME
configured appropriately
3. Solution
We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.
-
Download and unzip the source
4. Writing the Application
Create an application using the Micronaut Command Line Interface or with Micronaut Launch.
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool. If you don’t specify the --lang argument, Java is used as the language.If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.
|
The previous command creates a Micronaut application with the default package example.micronaut
in a directory named micronautguide
.
If you use Micronaut Launch, select Micronaut Application as application type and add micrometer-annotation
, data-jdbc
, flyway
, validation
, and http-client
features.
If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application. |
4.1. Metrics Configuration
Several groups of metrics are enabled by default; these include system metrics (JVM info, uptime, etc.), as well as metrics tracking web requests, DataSource activity, and others. Overall metrics can be enabled or disabled, and groups can be individually enabled or disabled in configuration.
Add the following to application.properties
:
(1)
micronaut.metrics.enabled=true
(2)
micronaut.metrics.binders.files.enabled=true
micronaut.metrics.binders.jdbc.enabled=true
micronaut.metrics.binders.jvm.enabled=true
micronaut.metrics.binders.logback.enabled=true
micronaut.metrics.binders.processor.enabled=true
micronaut.metrics.binders.uptime.enabled=true
micronaut.metrics.binders.web.enabled=true
1 | Change to false to disable all metrics, e.g., per-environment |
2 | These default to true and are only here for convenience, to be able to disable a subset of metrics |
4.2. Custom Metrics
If the built-in metrics aren’t enough, you can easily add custom metrics with the following annotations.
- @Timed
-
Creates a
Timer
metric that contains the total time, max time and count. - @Counted
-
Creates a
Counter
metric that only contains a count.
If you want to create your own gauge, you can inject io.micrometer.core.instrument.MeterRegistry
to your bean.
4.3. Data Source Metrics
DataSource
metrics are enabled by default if you use the HikariCP (the default), Tomcat JDBC, or Commons DBCP connection pool.
4.4. Database Migration with Flyway
We need a way to create the database schema. For that, we use Micronaut integration with Flyway.
Flyway automates schema changes, significantly simplifying schema management tasks, such as migrating, rolling back, and reproducing in multiple environments.
Add the following snippet to include the necessary dependencies:
implementation("io.micronaut.flyway:micronaut-flyway")
We will enable Flyway in the Micronaut configuration file and configure it to perform migrations on one of the defined data sources.
(1)
flyway.datasources.default.enabled=true
1 | Enable Flyway for the default datasource. |
Configuring multiple data sources is as simple as enabling Flyway for each one. You can also specify directories that will be used for migrating each data source. Review the Micronaut Flyway documentation for additional details. |
Flyway migration will be automatically triggered before your Micronaut application starts. Flyway will read migration commands in the resources/db/migration/
directory, execute them if necessary, and verify that the configured data source is consistent with them.
Create the following migration files with the database schema creation:
DROP TABLE IF EXISTS book;
CREATE TABLE book (
id BIGINT GENERATED BY DEFAULT AS IDENTITY,
name VARCHAR(255) NOT NULL,
isbn VARCHAR(255) NOT NULL UNIQUE
);
During application startup, Flyway will execute the SQL file and create the schema needed for the application.
4.5. Domain
Create a Book
domain class that uses Micronaut Data JDBC:
package example.micronaut
import groovy.transform.CompileStatic
import io.micronaut.data.annotation.GeneratedValue
import io.micronaut.data.annotation.Id
import io.micronaut.data.annotation.MappedEntity
import io.micronaut.serde.annotation.Serdeable
import static io.micronaut.data.annotation.GeneratedValue.Type.AUTO
@CompileStatic
@MappedEntity (1)
@Serdeable
class Book {
@Id (2)
@GeneratedValue(AUTO) (3)
Long id
String name
String isbn
Book(String isbn, String name) {
this.isbn = isbn
this.name = name
}
}
1 | Annotate the class with @MappedEntity to map the class to the table defined in the schema. |
2 | Specifies the ID of an entity |
3 | Specifies that the property value is generated by the database and not included in inserts |
4.6. BookRepository
Next, create the BookRepository
interface to define database operations. Micronaut Data will implement the interface at compilation time:
package example.micronaut
import io.micronaut.core.annotation.NonNull
import io.micronaut.data.jdbc.annotation.JdbcRepository
import io.micronaut.data.repository.CrudRepository
import jakarta.validation.constraints.NotBlank
import static io.micronaut.data.model.query.builder.sql.Dialect.H2
@JdbcRepository(dialect = H2) (1)
interface BookRepository extends CrudRepository<Book, Long> { (2)
@NonNull
Optional<Book> findByIsbn(@NotBlank String isbn)
}
1 | @JdbcRepository with a specific dialect. |
2 | Specifies that Book is the root entity, and the primary key type is Long . |
4.7. Data populator class
Create a DataPopulator
class to create some example database entries when the application starts:
package example.micronaut
import groovy.transform.CompileStatic
import io.micronaut.context.annotation.Requires
import io.micronaut.context.event.StartupEvent
import io.micronaut.runtime.event.annotation.EventListener
import jakarta.inject.Singleton
import jakarta.transaction.Transactional
import static io.micronaut.context.env.Environment.TEST
@CompileStatic
@Singleton (1)
class DataPopulator {
private final BookRepository bookRepository
DataPopulator(BookRepository bookRepository) { (2)
this.bookRepository = bookRepository
}
@EventListener (3)
@Transactional (4)
void init(StartupEvent event) {
if (bookRepository.count() == 0) {
bookRepository.save(new Book('1491950358', 'Building Microservices'))
bookRepository.save(new Book('1680502395', 'Release It!'))
bookRepository.save(new Book('0321601912', 'Continuous Delivery'))
}
}
}
1 | Use jakarta.inject.Singleton to designate a class as a singleton. |
2 | Use constructor injection to inject a bean of type BookRepository . |
3 | Annotate a method with @EventListener to subscribe to an event you define as the method parameter. |
4 | You can declare a method or class as transactional with the jakarta.transaction.Transactional annotation. |
4.8. BookController
Create BookController
to access Book
instances (and trigger JDBC metric data):
package example.micronaut
import groovy.transform.CompileStatic
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.scheduling.TaskExecutors
import io.micronaut.scheduling.annotation.ExecuteOn
import io.micrometer.core.annotation.Timed
import io.micrometer.core.annotation.Counted
@CompileStatic
@Controller('/books') (1)
@ExecuteOn(TaskExecutors.BLOCKING) (2)
class BookController {
private final BookRepository bookRepository
BookController(BookRepository bookRepository) { (3)
this.bookRepository = bookRepository
}
@Get(4)
@Timed("books.index") (5)
Iterable<Book> index() {
return bookRepository.findAll()
}
@Get('/{isbn}') (6)
@Counted("books.find") (7)
Optional<Book> findBook(String isbn) {
return bookRepository.findByIsbn(isbn)
}
}
1 | The class is defined as a controller with the @Controller annotation mapped to the path /books . |
2 | It is critical that any blocking I/O operations (such as fetching the data from the database) are offloaded to a separate thread pool that does not block the Event loop. |
3 | Use constructor injection to inject a bean of type BookRepository . |
4 | Maps a GET request to /books , which returns a list of Book |
5 | Creates a Timer metric with the given name. If the name is omitted, defaults to method.timed . |
6 | Maps a GET request to /books/{isbn} , which attempts to show a Book . This illustrates the use of a URL path variable. |
7 | Creates a Counter metric with the given name. If the name is omitted, defaults to the method.counted . |
4.9. Custom Metrics
To see custom metrics in action, create a service that periodically retrieves the current Bitcoin price in USD using REST.
4.9.1. Model
We’ll need a data class to represent the REST response. Create the BitcoinPrice
class:
package example.micronaut.crypto
import groovy.transform.CompileStatic
import io.micronaut.serde.annotation.Serdeable
@CompileStatic
@Serdeable (1)
class BitcoinPrice {
private final Data data
BitcoinPrice(Data data) {
this.data = data
}
float getPrice() {
return data.price
}
@CompileStatic
@Serdeable (1)
static class Data {
final float price
Data(float price) {
this.price = price
}
}
}
1 | Annotate the class with @Introspected to generate BeanIntrospection metadata at compilation time. This information can be used, for example, to render the POJO as JSON using Jackson without using reflection. |
4.9.2. Kucoin Declarative HTTP Client
Create declarative Micronaut HTTP Client interface that will be implemented at compile time:
package example.micronaut.crypto
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.QueryValue
import io.micronaut.http.client.annotation.Client
@Client(id = 'kucoin') (1)
abstract class PriceClient {
@Get('/api/v1/market/orderbook/level1')
abstract BitcoinPrice latest(@QueryValue String symbol)
BitcoinPrice latestInUSD() {
latest('BTC-USDT')
}
}
1 | Use @Client to use declarative HTTP Clients. You can annotate interfaces or abstract classes. You can use the id member to provide a service identifier or specify the URL directly as the annotation’s value. |
4.9.3. HTTP Service Configuration
Modify application.properties
to configure the URL for the kucoin
Service ID:
(1)
(2)
micronaut.http.services.kucoin.url=https://api.kucoin.com
1 | The ID we used with @Client and the URL for the kucoin service |
4.9.4. Service
Create a CryptoService
class that uses PriceClient
and updates three custom meters:
package example.micronaut.crypto
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.Timer
import io.micronaut.scheduling.annotation.Scheduled
import jakarta.inject.Singleton
import java.util.concurrent.atomic.AtomicInteger
@CompileStatic
@Slf4j
@Singleton (1)
class CryptoService {
private final PriceClient priceClient
private final Counter checks
private final Timer time
private final AtomicInteger latestPriceUsd = new AtomicInteger(0)
CryptoService(PriceClient priceClient, (2)
MeterRegistry meterRegistry) {
this.priceClient = priceClient
checks = meterRegistry.counter('bitcoin.price.checks') (3)
time = meterRegistry.timer('bitcoin.price.time') (4)
meterRegistry.gauge('bitcoin.price.latest', latestPriceUsd) (5)
}
@Scheduled(fixedRate = '${crypto.update-frequency:1h}',
initialDelay = '${crypto.initial-delay:0s}') (6)
void updatePrice() {
time.record(() -> { (7)
try {
checks.increment() (8)
latestPriceUsd.set((int) priceClient.latestInUSD().price) (9)
} catch (Exception e) {
log.error('Problem checking price', e)
}
})
}
}
1 | Use jakarta.inject.Singleton to designate a class as a singleton. |
2 | Use constructor injection to inject PriceClient , MeterRegistry , and ObjectMapper beans |
3 | Create a Counter to track the total number of updates |
4 | Create a Timer to track the total time performing updates |
5 | Create a Gauge to track the most recent update |
6 | Use the Scheduled annotation to configure regular updates |
7 | Update the Timer |
8 | Increment the Counter |
9 | Update the Gauge with the latest price |
5. Testing the Application
Create the src/test/resources/application-test.properties
configuration file for tests.
(1)
crypto.initial-delay=10h
1 | Disable crypto price lookups with a long initial delay |
Create a test class to verify metrics functionality:
package example.micronaut
import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.Tags
import io.micrometer.core.instrument.Timer
import io.micronaut.core.type.Argument
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.logging.LoggingSystem
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import spock.lang.Specification
import java.util.concurrent.TimeUnit
import static io.micronaut.logging.LogLevel.ALL
@MicronautTest (1)
class MetricsSpec extends Specification {
@Inject
MeterRegistry meterRegistry (2)
@Inject
LoggingSystem loggingSystem (3)
@Inject
@Client('/')
HttpClient httpClient (4)
void testExpectedMeters() {
when:
Set<String> names = meterRegistry.meters.id.name
then:
// check that a subset of expected meters exist
names.contains 'jvm.memory.max'
names.contains 'process.uptime'
names.contains 'system.cpu.usage'
names.contains 'process.files.open'
names.contains 'logback.events'
names.contains 'hikaricp.connections.max'
// these will be lazily created
!names.contains('http.client.requests')
!names.contains('http.server.requests')
}
void testHttp() {
when:
Timer timer = meterRegistry.timer('http.server.requests', Tags.of(
'exception', 'none',
'method', 'GET',
'status', '200',
'uri', '/books'))
Timer bookIndexTimer = meterRegistry.timer('books.index',
Tags.of('exception', 'none'))
then:
0 == timer.count()
0 == bookIndexTimer.count()
when:
httpClient.toBlocking().retrieve(
HttpRequest.GET('/books'),
Argument.listOf(Book))
then:
1 == timer.count()
1 == bookIndexTimer.count()
0.0 < bookIndexTimer.totalTime(TimeUnit.MILLISECONDS)
0.0 < bookIndexTimer.max(TimeUnit.MILLISECONDS)
when:
Counter bookFindCounter = meterRegistry.counter('books.find',
Tags.of('result', 'success',
'exception', 'none'))
then:
0 == bookFindCounter.count()
when:
httpClient.toBlocking().retrieve(
HttpRequest.GET("/books/1491950358"),
Argument.of(Book.class))
then:
1 == bookFindCounter.count()
}
void testLogback() {
when:
Counter counter = meterRegistry.counter('logback.events', Tags.of('level', 'info'))
double initial = counter.count()
Logger logger = LoggerFactory.getLogger('testing.testing')
loggingSystem.setLogLevel('testing.testing', ALL)
logger.trace('trace')
logger.debug('debug')
logger.info('info')
logger.warn('warn')
logger.error('error')
then:
initial + 1 == counter.count()
}
void testMetricsEndpoint() {
when:
Map<String, Object> response = httpClient.toBlocking().retrieve(
HttpRequest.GET('/metrics'),
Argument.mapOf(String, Object))
then:
response.containsKey 'names'
response.get('names') in List
when:
List<String> names = (List<String>) response.get('names')
then:
// check that a subset of expected meters exist
names.contains 'jvm.memory.max'
names.contains 'process.uptime'
names.contains 'system.cpu.usage'
names.contains 'process.files.open'
names.contains 'logback.events'
names.contains 'hikaricp.connections.max'
}
void testOneMetricEndpoint() {
when:
Map<String, Object> response = httpClient.toBlocking().retrieve(
HttpRequest.GET('/metrics/jvm.memory.used'),
Argument.mapOf(String, Object))
String name = (String) response.get('name')
then:
'jvm.memory.used' == name
when:
List<Map<String, Object>> measurements = (List<Map<String, Object>>) response.get('measurements')
then:
1 == measurements.size()
when:
double value = (double) measurements.get(0).get('value')
then:
value > 0
}
}
1 | Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info. |
2 | Inject the MeterRegistry bean |
3 | Inject the LoggingSystem bean |
4 | Inject the HttpClient bean and point it to the embedded server. |
Create an additional test class to verify the custom metrics:
package example.micronaut
import example.micronaut.crypto.CryptoService
import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.Timer
import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Requires
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.QueryValue
import io.micronaut.runtime.server.EmbeddedServer
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification
import static java.util.concurrent.TimeUnit.MILLISECONDS
class CryptoUpdatesSpec extends Specification {
@Shared
@AutoCleanup
EmbeddedServer kucoinEmbeddedServer = ApplicationContext.run(EmbeddedServer,
['spec.name': 'MetricsTestKucoin'])
@Shared
@AutoCleanup
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer,
['micronaut.http.services.kucoin.url': 'http://localhost:' + kucoinEmbeddedServer.port])
void 'test crypto updates'() {
given:
CryptoService cryptoService = embeddedServer.applicationContext.getBean(CryptoService)
MeterRegistry meterRegistry = embeddedServer.applicationContext.getBean(MeterRegistry)
when:
Counter counter = meterRegistry.counter('bitcoin.price.checks')
Timer timer = meterRegistry.timer('bitcoin.price.time')
then:
counter.count() == 0
timer.totalTime(MILLISECONDS) == 0
when:
int checks = 3
checks.times {
cryptoService.updatePrice()
}
then:
counter.count() == checks
timer.totalTime(MILLISECONDS) > 0
}
@Requires(property = 'spec.name', value = 'MetricsTestKucoin')
@Controller
static class MockKucoinController {
private static final String RESPONSE = '''{
| "code":"200000",
| "data":{
| "time":1654865889872,
| "sequence":"1630823934334",
| "price":"29670.4",
| "size":"0.00008436",
| "bestBid":"29666.4",
| "bestBidSize":"0.16848947",
| "bestAsk":"29666.5",
| "bestAskSize":"2.37840044"
| }
|}'''.stripMargin()
@Get('/api/v1/market/orderbook/level1')
String latest(@QueryValue String symbol) {
RESPONSE
}
}
}
The previous test creates two Micronaut embedded servers. One mocks the kucoin
API, the other is our application. The test uses @Requires
to conditionally load a controller only for the kucoin
bean context.
We used an identifier with @Client
, which makes it easy to point our HTTP client to the mock server.
5.1. Running the tests
To run the tests:
./gradlew test
Then open build/reports/tests/test/index.html
in a browser to see the results.
6. Running the Application
To run the application, use the ./gradlew run
command, which starts the application on port 8080.
Alternately, to make the Bitcoin price update occur more frequently to see the effects on metrics, start the application with a config override to update every five seconds:
./gradlew run --args="-crypto.updateFrequency=5s"
You can retrieve a list of known metrics using cURL:
curl localhost:8080/metrics
The response should look like this:
{
"names": [
"bitcoin.price.checks",
"bitcoin.price.latest",
"bitcoin.price.time",
"executor",
"executor.active",
"executor.completed",
"executor.pool.core",
"executor.pool.max",
"executor.pool.size",
"executor.queue.remaining",
"executor.queued",
"hikaricp.connections",
"hikaricp.connections.acquire",
"hikaricp.connections.active",
...
"jvm.threads.peak",
"jvm.threads.states",
"logback.events",
"process.cpu.usage",
"process.files.max",
"process.files.open",
"process.start.time",
"process.uptime",
"system.cpu.count",
"system.cpu.usage",
"system.load.average.1m"
]
}
After the application has been running for a bit and has made a few Bitcoin price checks, we can view the related metric values:
curl localhost:8080/metrics/bitcoin.price.latest
{
"measurements": [{ "statistic": "VALUE", "value": 29659.0 } ],
"name": "bitcoin.price.latest"
}
curl localhost:8080/metrics/bitcoin.price.checks
{
"measurements": [{ "statistic": "COUNT", "value": 5.0 } ],
"name": "bitcoin.price.checks"
}
curl localhost:8080/metrics/bitcoin.price.time
{
"baseUnit": "seconds",
"measurements": [
{ "statistic": "COUNT", "value": 5.0 },
{ "statistic": "TOTAL_TIME", "value": 2.525546791 },
{ "statistic": "MAX", "value": 0.851958216 }
],
"name": "bitcoin.price.time"
}
7. Next steps
In combination with the /metrics
endpoint, you often want to register a specific type of reporter. See the Micronaut Micrometer documentation to learn about the supported libraries for reporting metrics.
8. License
All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…). |