'Calculate the number of requests per second using Spring Boot Actuator

I want to calculate the number of requests per second for a particular URL from a Spring Boot 2 application, also the time taken for each request (latency) in milliseconds. We can see the following metrics from Actuator/Prometheus:

http_server_request_config_seconds_count  
http_server_request_config_seconds_sum  

I'm confused how to plot this in Prometheus to get my result. Do I need to add a histogram or quantiles?



Solution 1:[1]

If you only care about the request per seconds, you don't need anything related to the quantiles.

irate(http_server_requests_seconds_count{uri="/your-uri"}[5m])

And if your are interested in the response time overall:

irate(http_server_request_duration_seconds_sum{exception="None", uri = "/your-url"}[5m]) / irate(http_server_requests_duration_seconds_count{exception="None", uri = "/your-url"}[5m])

If you want more precised metrics (quantiles) you can refer to the Prometheus documentation.
e.g:

histogram_quantile(0.99, sum(rate(http_server_requests_seconds_bucket{exception="None", uri = "/your-uri"}[5m])) by (le))

Solution 2:[2]

Both http_server_request_config_seconds_count and http_server_request_config_seconds_sum metrics are counters. E.g. they increase over time and may reset to zero when the service, which exports these metrics, is restarted.

The http_server_request_config_seconds_count metric shows the total number of processed requests since the last service restart.

The http_server_request_config_seconds_sum metric shows the total sum of request durations for all the processed requests since the last service restart.

If you need to calculate the average RPS over the given lookbehind window, then you need to use rate() function:

rate(http_server_request_config_seconds_count[5m])

This query uses 5 minute lookbehind window in square brackets for calculating the average RPS. It is possible to use any desired lookbehind window according to time durations syntax.

The following query shows the average query duration over the last 5 minutes:

increase(http_server_request_config_seconds_sum[5m])
  /
increase(http_server_request_config_seconds_count[5m])

It works in the following way:

  1. It calculates the sum of request durations over the last 5 minutes with increase() function.
  2. It calculates the the number of requests over the last 5 minutes with the increase() function.
  3. It divides the sum of request durations by the number of requests and gets the average request duration over the last 5 minutes.

Queries above may return multiple results if http_server_request_config_seconds metric is scraped from multiple targets. If you want calculating the summary RPS and average response time across all the scrape targets, then sum aggregate function must be used.

The following query returns the summary average RPS across all the scrape targets during the last 5 minutes:

sum(rate(http_server_request_config_seconds_count[5m]))

The following query returns the average response duration across all the scrape targets during the last 5 minutes:

sum(increase(http_server_request_config_seconds_sum[5m]))
  /
sum(increase(http_server_request_config_seconds_count[5m]))

Note that the sum() must be used independently for the left-hand and right-hand sides of / operator.

Solution 3:[3]

To enable quantiles an additional configuration property needs to be added to your application.properties, substituting in whatever quantiles (not percentiles, despite the name) you’re interested in:

management.metrics.web.server.request.autotime.percentiles=<comma-separated list of quantiles>

If we configure the property as 0.95 this would result in the following metric at /actuator/prometheus:

http_server_requests_seconds{exception="None",method="GET",outcome="SUCCESS",status="200",uri="/doit",quantile="0.95",} 0.023068672 The tags used with this metric are the same as above, except now we also have a quantile tag, which can be used to query the metric.

The following graph is generated in Prometheus from the http_server_requests_seconds{quantile="0.95"} query:

ref? https://tomgregory.com/spring-boot-default-metrics/

Solution 4:[4]

I tried the code you provided, the reason why the image does not refresh is the cache of BitmapImage.

In the official document of BitmapImage.CreateOptions, it says:

The other possible value for CreateOptions is BitmapCreateOptions.IgnoreImageCache. You should only use BitmapCreateOptions.IgnoreImageCache in cases where you know that the source image file as retrieved by Uniform Resource Identifier (URI) has the potential to change over time.

After changing the CreateOptions, it works well.

public static System.Windows.Media.ImageSource BitmapFromUri(Uri source)
{
    var bitmap = new BitmapImage();
    bitmap.BeginInit();
    bitmap.UriSource = source;
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    // NOTE: ignore the image cache
    bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
    bitmap.EndInit();
    return bitmap;
}

As other friends' recommandation, use BitmapSource is a better implementation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 samabcde
Solution 2
Solution 3
Solution 4 cg-zhou