• Aucun résultat trouvé

Remote shell plugins

Dans le document Spring Boot Reference Guide (Page 196-200)

In addition to new commands, it is also possible to extend other CRaSH shell features. All Spring Beans that extend org.crsh.plugin.CRaSHPlugin will be automatically registered with the shell.

For more information please refer to the CRaSH reference documentation.

51. Loggers

Spring Boot Actuator includes the ability to view and configure the log levels of your application at runtime. You can view either the entire list or an individual logger’s configuration which is made up of both the explicitly configured logging level as well as the effective logging level given to it by the logging framework. These levels can be:

• TRACE

• DEBUG

• INFO

• WARN

• ERROR

• FATAL

• OFF

• null

with null indicating that there is no explicit configuration.

51.1 Configure a Logger

In order to configure a given logger, you POST a partial entity to the resource’s URI:

{

"configuredLevel": "DEBUG"

}

52. Metrics

Spring Boot Actuator includes a metrics service with ‘gauge’ and ‘counter’ support. A ‘gauge’ records a single value; and a ‘counter’ records a delta (an increment or decrement). Spring Boot Actuator also provides a PublicMetrics interface that you can implement to expose metrics that you cannot record via one of those two mechanisms. Look at SystemPublicMetrics for an example.

Metrics for all HTTP requests are automatically recorded, so if you hit the metrics endpoint you should see a response similar to this:

{

"counter.status.200.root": 20, "counter.status.200.metrics": 3, "counter.status.200.star-star": 5, "counter.status.401.root": 4, "gauge.response.star-star": 6, "gauge.response.root": 2, "gauge.response.metrics": 3, "classes": 5808,

"classes.loaded": 5808, "classes.unloaded": 0, "heap": 3728384,

"heap.committed": 986624, "heap.init": 262144, "heap.used": 52765, "nonheap": 0,

"nonheap.committed": 77568, "nonheap.init": 2496, "nonheap.used": 75826, "mem": 986624, "mem.free": 933858, "processors": 8, "threads": 15, "threads.daemon": 11, "threads.peak": 15,

"threads.totalStarted": 42, "uptime": 494836,

"instance.uptime": 489782, "datasource.primary.active": 5, "datasource.primary.usage": 0.25 }

Here we can see basic memory, heap, class loading, processor and thread pool information along with some HTTP metrics. In this instance the root (‘/’) and /metrics URLs have returned HTTP 200 responses 20 and 3 times respectively. It also appears that the root URL returned HTTP 401 (unauthorized) 4 times. The double asterisks (star-star) comes from a request matched by Spring MVC as /** (normally a static resource).

The gauge shows the last response time for a request. So the last request to root took 2ms to respond and the last to /metrics took 3ms.

Note

• The total system memory in KB (mem)

• The amount of free memory in KB (mem.free)

• The number of processors (processors)

• The system uptime in milliseconds (uptime)

• The application context uptime in milliseconds (instance.uptime)

• The average system load (systemload.average)

• Heap information in KB (heap, heap.committed, heap.init, heap.used)

• Thread information (threads, thread.peak, thread.daemon)

• Class load information (classes, classes.loaded, classes.unloaded)

• Garbage collection information (gc.xxx.count, gc.xxx.time)

52.2 DataSource metrics

The following metrics are exposed for each supported DataSource defined in your application:

• The number of active connections (datasource.xxx.active)

• The current usage of the connection pool (datasource.xxx.usage).

All data source metrics share the datasource. prefix. The prefix is further qualified for each data source:

• If the data source is the primary data source (that is either the only available data source or the one flagged @Primary amongst the existing ones), the prefix is datasource.primary.

• If the data source bean name ends with DataSource, the prefix is the name of the bean without DataSource (i.e. datasource.batch for batchDataSource).

• In all other cases, the name of the bean is used.

It is possible to override part or all of those defaults by registering a bean with a customized version of DataSourcePublicMetrics. By default, Spring Boot provides metadata for all supported data sources; you can add additional DataSourcePoolMetadataProvider beans if your favorite data source isn’t supported out of the box. See DataSourcePoolMetadataProvidersConfiguration for examples.

52.3 Cache metrics

The following metrics are exposed for each supported cache defined in your application:

• The current size of the cache (cache.xxx.size)

• Hit ratio (cache.xxx.hit.ratio)

• Miss ratio (cache.xxx.miss.ratio) Note

Cache providers do not expose the hit/miss ratio in a consistent way. While some expose an aggregated value (i.e. the hit ratio since the last time the stats were cleared), others expose a

temporal value (i.e. the hit ratio of the last second). Check your caching provider documentation for more details.

If two different cache managers happen to define the same cache, the name of the cache is prefixed by the name of the CacheManager bean.

It is possible to override part or all of those defaults by registering a bean with a customized version of CachePublicMetrics. By default, Spring Boot provides cache statistics for EhCache, Hazelcast, Infinispan, JCache and Guava. You can add additional CacheStatisticsProvider beans if your favorite caching library isn’t supported out of the box. See CacheStatisticsAutoConfiguration for examples.

52.4 Tomcat session metrics

If you are using Tomcat as your embedded servlet container, session metrics will automatically be exposed. The httpsessions.active and httpsessions.max keys provide the number of active and maximum sessions.

52.5 Recording your own metrics

To record your own metrics inject a CounterService and/or GaugeService into your bean.

The CounterService exposes increment, decrement and reset methods; the GaugeService provides a submit method.

Here is a simple example that counts the number of times that a method is invoked:

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.actuate.metrics.CounterService;

import org.springframework.stereotype.Service;

@Service

public class MyService {

private final CounterService counterService;

@Autowired

public MyService(CounterService counterService) { this.counterService = counterService;

}

public void exampleMethod() {

this.counterService.increment("services.system.myservice.invoked");

} }

Tip

You can use any string as a metric name but you should follow guidelines of your chosen store/

graphing technology. Some good guidelines for Graphite are available on Matt Aimonetti’s Blog.

Dans le document Spring Boot Reference Guide (Page 196-200)