Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Where do Spring Boot default metrics come from?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Where does the default indicator of Spring Boot come from? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Have you noticed all the default metrics generated by Spring Boot and Micrometer for your application? If not-you can add actuator dependencies to the project, and then click the / actuator / metrics endpoint, where you will find useful information about JVM, processes, Tomcat, traffic, and so on. Then, add some caches, data sources, or JPA dependencies, and even more metrics. If you want to know how they end and where we can find explanations for the parameters they describe, then this article is for you.

Display indicator

To get it in order, let's start with how to display metrics in a Spring Boot application. If you already know, you can skip this section.

Metrics in Spring Boot are processed by micrometer.io. However, if you use actuator, you do not need to add a micrometer dependency to the project because actuator already depends on it. Even if you are not interested in the endpoints it provides, you want to use actuator, because this is a module that registers many metrics through its AutoConfigurations. We will discuss it in detail later.

So, first, simply add the executor dependency to the project (in this case, build.gradle.kts)

Dependencies {implementation ("org.springframework.boot:spring-boot-starter-actuator")}

And display the metric name in the endpoint of the actuator, click http://localhost:8080/actuator/metrics.

{"names": ["jvm.threads.states", "process.files.max", "jvm.memory.used", "jvm.gc.memory.promoted", "jvm.memory.max", "system.load.average.1m",...]}

Then, to view the details, add the metric name to the URL path, for example: http://localhost:8080/actuator/metrics/system.cpu.count.

{"name": "system.cpu.count", "description": "The number of processors available to the Java virtual machine", "baseUnit": null, "measurements": [{"statistic": "VALUE", "value": 8}], "availableTags": []}

By providing a specific instrument registry, you can periodically send these indicators to the indicator system of your choice (Prometheus,New Relic,CloudWatch,Graphite, etc.). Let's do it with the simplest registry-LoggingMeterRegistry, which simply records all metrics on a regular basis.

@ Configurationclass MetricsConfig {@ Bean LoggingMeterRegistry loggingMeterRegistry () {return new LoggingMeterRegistry ();}}

Now the metrics are also displayed in the log:

2019-07-17 11 i.m.c.i.logging.LoggingMeterRegistry 07 INFO 91283-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: jvm.buffer.count {id=direct} value=0 buffers2019-07-17 11 Ride 07v 09.406 INFO 91283-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: jvm.buffer.count {id=mapped} value=0 buffers2019-07-17 1111 provision07lv 09.406 INFO 91283-[trics-publisher] i.m.c .i.logging.LoggingMeterRegistry: jvm.buffer.memory.used {id=direct} value=0 B2019-07-17 11Participated 07Partition 09.406 INFO 91283-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: jvm.buffer.memory.used {id=mapped} value=0 B2019-07-17 11Rod 07Partition 09.408 INFO 91283-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: jvm.classes.loaded {} value=8530 classes2019-07-17 11: 07jvm.gc.live.data.size 09.408 INFO 91283-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: jvm.gc.live.data.size {} value=0 B2019-07-17 1111 value=0 07i.m.c.i.logging.LoggingMeterRegistry 09.408 INFO 91283-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: jvm.gc.max.data.size {} value=0 B2019-07-17 1111Vol 07RV 09.410 INFO 91283-- [trics-publisher] i.m.c. I.logging.LoggingMeterRegistry: jvm.memory.committed {area=nonheap Id=Compressed Class Space} value=6.25 MiB2019-07-17 11 INFO 0715 09.410 INFO 91283-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: jvm.memory.committed {area=heap,id=G1 Eden Space} value=168 MiB... Index supply

So how to provide these indicators? An example might be WebMvcMetricsFilter, which adds performance metrics (http.server.requests metric) to all Spring Web MVC endpoints.

But this example is simple. When all requests are processed by the Spring framework, it is not necessary to add internally the call generation metrics (just check the WebMvcMetricsFilter.record () method).

But what if you use pure ehcache or hibernate or other data sources and then generate metrics?

So cache. * indicators will be generated even if I @ Autowired pure net.sf.ehcache.Cache?

So hibernate. * indicators will be generated even if I @ Autowired pure org.hibernate.SessionFactory?

Then, how can jvm.*, process.*, tomcat.* and so on be generated automatically?

It seems simpler than people think, because these statistics are provided by the monitored components themselves. Sometimes it will be provided directly, for example, cache.getStatistics () provides StatisticsGateway * for EhCache, * or sessionFactory.getStatistics () provides statistics for Hibernate SessionFactory, and so on.

Sometimes this can be done in other ways, such as hosting bean. For example, using RuntimeMXBean for JVM process.* metrics and (such as GlobalRequestProcessor, Servlet, etc.) Tomcat mbeans for tomcat. * indicators

To access these statistics and convert them into specific metrics, Micrometer introduced the concept of MeterBinder.

Check the MeterBinder implementation hierarchy and you will learn more about the available metrics groups.

You can also check directly on micrometer repo.

Open, for example, EhCache2Metrics, and you will find out what and how Ehcache statistics map to specific Micrometer metrics.

Cache.size-> StatisticsGateway:getSize cache.gets {result=miss}-> StatisticsGateway:cacheMissCount cache.gets {result=hit}-> StatisticsGateway:cacheHitCount cache.puts-> StatisticsGateway:cachePutCount cache.evictions-> StatisticsGateway:cacheEvictedCount cache.remoteSize-> StatisticsGateway::getRemoteSize cache.removals-> StatisticsGateway::cacheRemoveCount cache.puts.added {result=added}-> StatisticsGateway::cachePutAddedCount cache.puts.added {result=updated}-> StatisticsGateway::cachePutAddedCount cache.misses {reason=expired}-> StatisticsGateway::cacheMissExpiredCount) cache.misses {reason=notFound}-> StatisticsGateway :: cacheMissNotFoundCount) cache.xa.commits {result=readOnly}-> StatisticsGateway::xaCommitReadOnlyCount cache.xa.commits {result=exception}-> StatisticsGateway::xaCommitExceptionCount cache.xa.commits {result=committed}-> StatisticsGateway::xaCommitCommittedCount cache.xa.rollbacks {result=exception}-> StatisticsGateway::xaRollbackExceptionCount cache.xa.rollbacks {result=success}-> StatisticsGateway::xaRollbackSuccessCount cache.xa.recoveries {result=nothing}-> StatisticsGateway::xaRecoveryNothingCount cache.xa.recoveries {result=success}-> StatisticsGateway::xaRecoveryRecoveredCount cache.local.offheap.size-> StatisticsGateway: : getLocalOffHeapSize) cache.local.heap.size-> StatisticsGateway::getLocalHeapSizeInBytes cache.local.disk.size-> StatisticsGateway::getLocalDiskSizeInBytes

Registering MeterBinders is very simple, and examples can be found in the micrometer documentation.

Remember, you can do this manually:

New ClassLoaderMetrics (). BindTo (registry); new JvmMemoryMetrics (). BindTo (registry); new EhCache2Metrics (cache, Tags.of ("name", cache.getName ()). BindTo (registry) new TomcatMetrics (manager, tags) .bindTo (registry).

Or, you can use Spring Boot, which will do this for you under the engine.

As I mentioned earlier, actuator will provide a number of AutoConfiguration s and MetricsBinders, and it will register MeterBinders as soon as a given dependency is added.

For example, TomcatMetricsBinder will register TomcatMetrics (as your embedded container). MeterRegistryConfigurer registers JVM, uptime, and other system metrics.

Now, suppose you want to use Ehcache in your application. You can add two dependencies:

Implementation ("org.springframework.boot:spring-boot-starter-cache") implementation ("net.sf.ehcache:ehcache")

Then register the cache (you can also do this through ehcache.xml)

@ Bean Cache playCache (EhCacheCacheManager cacheManager) {CacheConfiguration cacheConfiguration = new CacheConfiguration () .name (CACHE_NAME) .maxEntriesLocalHeap (MAX_ELEMENTS_IN_MEMORY); Cache cache = new Cache (cacheConfiguration); cacheManager.getCacheManager () .addCache (cache); cacheManager.initializeCaches (); return cache;}

CacheMetricsRegistrarConfiguration will now register EhCache2Metrics for each cache management through the Spring cache manager.

If you don't want to use Spring cache management, you can also register EhCache2Metrics yourself.

Now, start the application and you will see other ehcache metrics.

2019-07-17 13 i.m.c.i.logging.LoggingMeterRegistry 08 INFO 93052-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: cache.gets {cache=playCache,cacheManager=cacheManager,name=playCache,result=hit} throughput=12.95/s2019-07-17 13 13 Ride 08purl 45.124 INFO 93052-[Thread-4] i.m.c.i.logging.LoggingMeterRegistry: cache.misses {cache=playCache,cacheManager=cacheManager,name=playCache Reason=notFound} throughput=3.7/s2019-07-17 13 trics-publisher 08 INFO 93052-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: cache.gets {cache=playCache,cacheManager=cacheManager,name=playCache,result=miss} throughput=3.7/s2019-07-17 1313 INFO 0840 INFO 93052-[Thread-4] i.m.c.i.logging.LoggingMeterRegistry: cache.puts {cache=playCache,cacheManager=cacheManager Name=playCache} throughput=16.65/s2019-07-17 13 INFO 0840 INFO 93052-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: cache.misses {cache=playCache,cacheManager=cacheManager,name=playCache,reason=notFound} throughput=3.7/s2019-07-17 1313 INFO 0841 INFO 93052-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: cache.puts {cache=playCache,cacheManager=cacheManager Name=playCache} throughput=16.65/s2019-07-17 13 INFO 0841 INFO 93052-[Thread-4] i.m.c.i.logging.LoggingMeterRegistry: cache.puts.added {cache=playCache,cacheManager=cacheManager,name=playCache,result=updated} throughput=0.116667/s2019-07-17 1313 Fraser 0841 INFO 93052-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: cache.puts.added {cache=playCache,cacheManager=cacheManager,name=playCache Result=updated} throughput=0.116667/s2019-07-17 13 INFO 0841 INFO 93052-[Thread-4] i.m.c.i.logging.LoggingMeterRegistry: cache.puts.added {cache=playCache,cacheManager=cacheManager,name=playCache,result=added} throughput=0.116667/s2019-07-17 1313 INFO 0842 INFO 93052-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: cache.puts.added {cache=playCache,cacheManager=cacheManager,name=playCache Result=added} throughput=0.116667/s2019-07-17 13 INFO 0847 INFO 93052-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: cache.local.disk.size {cache=playCache,cacheManager=cacheManager,name=playCache} value=0 B2019-07-17 1313 Fraser 0847 INFO 93052-[Thread-4] i.m.c.i.logging.LoggingMeterRegistry: cache.local.disk.size {cache=playCache,cacheManager=cacheManager Name=playCache} value=0 B2019-07-17 13 Thread-4 08 INFO 93052-[Thread-4] i.m.c.i.logging.LoggingMeterRegistry: cache.local.heap.size {cache=playCache,cacheManager=cacheManager,name=playCache} value=1.039062 KiB2019-07-17 138 Swiss 48.908 INFO 93052-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: cache.local.heap.size {cache=playCache,cacheManager=cacheManager Name=playCache} value=1.039062 KiB2019-07-17 13 Thread-4 08 INFO 48.909 INFO 93052-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: cache.local.offheap.size {cache=playCache,cacheManager=cacheManager,name=playCache} value=0 B2019-07-17 1313 Fraser 08Switzerland 48.909 INFO 93052-[Thread-4] i.m.c.i.logging.LoggingMeterRegistry: cache.local.offheap.size {cache=playCache,cacheManager=cacheManager Name=playCache} value=0 B2019-07-17 13 Thread-4 08 INFO 93052-[Thread-4] i.m.c.i.logging.LoggingMeterRegistry: cache.remoteSize {} value=02019-07-17 138 purge 48.909 INFO 93052-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: cache.remoteSize {} value=02019-07-17 13 purge 08hand48.909 INFO 93052-[Thread-4] i.m. C.i.logging.LoggingMeterRegistry: cache.size {cache=playCache CacheManager=cacheManager,name=playCache} value=72019-07-17 13 i.m.c.i.logging.LoggingMeterRegistry 08 INFO 48.909 INFO 93052-[trics-publisher] i.m.c.i.logging.LoggingMeterRegistry: cache.size {cache=playCache,cacheManager=cacheManager,name=playCache} value=7

In this case, the responsibilities of each component in the context of the metric can be summarized as follows:

You can view all of these concepts in the sample application provided here.

After reading the above, have you mastered the method of where the default indicators of Spring Boot come from? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 276

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report