Spring Boot default indicators come from?

Learn more about Spring Boot default indicators and their sources.

Did you notice all the default indicators Spring Boot and Micrometer generated for your application? If you do not - you can actuator add a dependency to the project, and then click / actuator / metrics endpoint, where you will find useful information about the JVM, processes, Tomcat, traffic and the like. Then, add some cache , data source or JPA dependencies, there will be even more indicators. If you want to know how they end, we can find explanations of the parameters they describe Where, then this article is for you to prepare.

Indicators show

To make it orderly, let us show how metrics in Spring Boot application start. If you already know, you can skip this section.

Spring Boot in the index by the micrometer.io process. However, if you use the actuator, you do not need to add micrometer dependencies to the project, because the actuator has been dependent on it. Even if the endpoint that you are not interested in providing it, and hope you use actuator, because it is by its AutoConfigurationsmodule many indicators of registration. We will discuss in detail later.

So, first of all, simply add the actuator dependency to the project (here build.gradle.kts)

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

And displays the name of the index in the actuator endpoints, click on 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 details, add the index name in 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 specific instrument registry, you can periodically send these indicators to the index system of your choice ( Prometheus , New Relic , CloudWatch , Graphite , etc.). Let us use the registry to do the most simple - LoggingMeterRegistry , it's just a regular record of all indicators.

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

Now, the indicators appear in the log:

2019-07-17 11:07:09.406  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.buffer.count{id=direct} value=0 buffers
2019-07-17 11:07:09.406  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.buffer.count{id=mapped} value=0 buffers
2019-07-17 11:07:09.406  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.buffer.memory.used{id=direct} value=0 B
2019-07-17 11:07:09.406  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.buffer.memory.used{id=mapped} value=0 B
2019-07-17 11:07:09.408  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.classes.loaded{} value=8530 classes
2019-07-17 11:07:09.408  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.gc.live.data.size{} value=0 B
2019-07-17 11:07:09.408  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.gc.max.data.size{} value=0 B
2019-07-17 11:07:09.410  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.memory.committed{area=nonheap,id=Compressed Class Space} value=6.25 MiB
2019-07-17 11:07:09.410  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.memory.committed{area=heap,id=G1 Eden Space} value=168 MiB
...

Supply Indicators

So, how to provide these indicators do? An example might be WebMvcMetricsFilter , add performance to all Spring Web MVC endpoint (http.server.requests Metric) .

But this example is very simple. When all requests processed by the Spring Framework, added indicator internally generated calls is not necessary (only check WebMvcMetricsFilter.record () method).

However, if you use pure ehcache or hibernate or other data source, and then generate indicators, the situation then?

So Cache. * Index it, even though I am @Autowiredpure net.sf.ehcache.Cachewill generate?

So Hibernate. * Index it, even though I am @Autowiredpure org.hibernate.SessionFactorywill generate?

jvm.*Then, process.*, , tomcat.*and so how to automatically generate?

It seems easier than people think, because these statistics are provided by the component monitored itself. Sometimes, it will directly provide, for example, cache.getStatistics()to EhCache provide StatisticsGateway , or sessionFactory.getStatistics()to Hibernate SessionFactory provide statistics and so on.

Sometimes, this can be achieved by other means, such as a managed bean. For example, the RuntimeMXBean for JVM process.*metrics and the (e.g. GlobalRequestProcessor, Servletetc.) Tomcat mbeansfor Tomcat. * Index

In order to access these statistics and convert it to a particular index, Micrometer introduces MeterBinder concepts.

Check the MeterBinderimplementation hierarchy, you will learn more about the available indicators group.

micrometer MeterBinders

You can also directly in the micrometer repo checking on.

Open, for example, EhCache2Metrics , you will find Ehcache statistical information is mapped to the content and methods specific Micrometer indicators.

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

Registration MeterBindersis very simple example can micrometer document was found in.

Remember, you can 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)
...

Alternatively, you can use Spring Boot, it will do it for you in the engine.

As I mentioned before, actuator will provide many AutoConfigurations and MetricsBinders, just add the given dependency, it will be registered MeterBinders.

For example, TomcatMetricsBinder registration TomcatMetrics (for your embedded container). MeterRegistryConfigurer will register JVM, running time and other system indicators.

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 cache (You can also ehcache.xml be achieved)

  @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;
    }

Now, CacheMetricsRegistrarConfiguration through Spring Cache Manager for each cache management registered EhCache2Metrics .

If you do not want to use Spring cache management, you can register yourself EhCache2Metrics.

Now, start the application, you will see other ehcache indicators.

2019-07-17 13:08:45.113  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.gets{cache=playCache,cacheManager=cacheManager,name=playCache,result=hit} throughput=12.95/s
2019-07-17 13:08: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/s
2019-07-17 13:08:45.124  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.gets{cache=playCache,cacheManager=cacheManager,name=playCache,result=miss} throughput=3.7/s
2019-07-17 13:08:48.840  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.puts{cache=playCache,cacheManager=cacheManager,name=playCache} throughput=16.65/s
2019-07-17 13:08:48.840  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.misses{cache=playCache,cacheManager=cacheManager,name=playCache,reason=notFound} throughput=3.7/s
2019-07-17 13:08:48.841  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.puts{cache=playCache,cacheManager=cacheManager,name=playCache} throughput=16.65/s
2019-07-17 13:08:48.841  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.puts.added{cache=playCache,cacheManager=cacheManager,name=playCache,result=updated} throughput=0.116667/s
2019-07-17 13:08:48.841  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.puts.added{cache=playCache,cacheManager=cacheManager,name=playCache,result=updated} throughput=0.116667/s
2019-07-17 13:08:48.841  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.puts.added{cache=playCache,cacheManager=cacheManager,name=playCache,result=added} throughput=0.116667/s
2019-07-17 13:08:48.842  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.puts.added{cache=playCache,cacheManager=cacheManager,name=playCache,result=added} throughput=0.116667/s
2019-07-17 13:08:48.847  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.local.disk.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=0 B
2019-07-17 13:08:48.847  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.local.disk.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=0 B
2019-07-17 13:08:48.908  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.local.heap.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=1.039062 KiB
2019-07-17 13:08: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 KiB
2019-07-17 13:08:48.909  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.local.offheap.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=0 B
2019-07-17 13:08:48.909  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.local.offheap.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=0 B
2019-07-17 13:08:48.909  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.remoteSize{} value=0
2019-07-17 13:08:48.909  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.remoteSize{} value=0
2019-07-17 13:08:48.909  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=7
2019-07-17 13:08: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 context indicators can be summarized as:

Ehcache index architecture

You can in here to see all these concepts sample applications provided.

Happy coding!

Original: https: //dzone.com/articles/spring-boot-where-do-the-default-metrics-come-from

Author: Dawid Kublik

Translator: Queena


August welfare struck on time, public concern number backstage Re: July 003 can receive a translation of highlights oh go on welfare Re: 001, 002, you can receive!



img

Guess you like

Origin www.cnblogs.com/liululee/p/11410493.html