Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Prometheus summary.observe method do?

I'm trying to play with summary metrics and not quiet understand where do I put summary.observe call? Here is prom-client example (you can find on npm):

    const client = require('prom-client');
    const summary = new client.Summary({
      name: 'metric_name',
      help: 'metric_help'
    });
    summary.observe(10);

but there is no enough info how to use it.

  1. what does observe(10) mean?

  2. where do I put that summary.observe(10) call? Just right after summary metric declaration or at the end of function/endpoint call like:

    const client = require('prom-client');
    
    const summary = new client.Summary({
      name: 'metric_name',
      help: 'metric_help'
    });
    summary.observe(10); // do I put it here?
    
    async myFunc(){
        await this.serviceCall();
        summary.observe(10); // or here?
    } 
    

Does anybody have a good example/explanation of summary observe?

like image 954
John Glabb Avatar asked May 24 '19 18:05

John Glabb


People also ask

How does a Prometheus Summary work?

A summary is a metric type in Prometheus that can be used to monitor latencies (or other distributions like request sizes). For example, when you monitor a REST endpoint you can use a summary and configure it to provide the 95th percentile of the latency.

What are summary metrics?

A summary metric is essentially a shortcut to a subtotal. It allows you to select the function to use to calculate the subtotal (that is, a summary). If you use a metric from a dataset report rather than a summary metric, the default subtotal function is used to subtotal the metric.

What are the types of metrics in Prometheus?

Prometheus has four primary metric types to query for via PromQL: counters, gauges, histograms, and summaries. These metric types fulfill the needs or requirements for most use cases, and are found in Prometheus' official client libraries: Go, Java, Ruby, and Python.


Video Answer


1 Answers

First, let's clarify what an summary is: a summary metric captures individual observations from an event and summarizes them into a number of related metric:

  1. a sum of values (aka observations)
  2. observation count
  3. quantiles

Taking an example: if you are measuring the response time of a service:

  • first, you create a summary metrics providing

    • a name and labels (as usual to identify the metric) - ex: foobar_request_duration_seconds
    • usually some way to indicate the windows of measurements (number of observation or maximum age of events)
    • a list of quantiles to compute - ex: 0.1, 0.5 (median), 0.75, 0.9
  • for each request, you will compute the response time of a request - this is an observation

  • you then feed the computed request response time to the summary object
  • when Prometheus scrapes you end-point, metrics are computed from the response time measures observed

    • foobar_request_duration_seconds_sum: total number of seconds consumed by requests
    • foobar_request_duration_seconds_count: number of requests (note you can compute average with sum)
    • foobar_request_duration_seconds_seconds{quantile="0.1"}: response time 10% (... same for all configured quantiles)

I hope this should help you understand the prom-client documentation:

The first example shows how to indicate the quantiles to compute

new client.Summary({
  name: 'metric_name',
  help: 'metric_help',
  percentiles: [0.01, 0.1, 0.9, 0.99]
});

The second how to limit the window of measurements; here, measure are computed over the last 10 minutes (using 5 buckets - this impacts smoothing of values):

new client.Summary({
  name: 'metric_name',
  help: 'metric_help',
  maxAgeSeconds: 600,
  ageBuckets: 5
});

Coming back to your question, the observe() method should be called on what you are observing.

If you want to measure some data returned by serviceCall(), you feed it to the summary

number_bytes_exchanged = await this.serviceCall();
summary.observe(number_bytes_exchanged);

If you want to measure the time taken by service call

const observe_response_time = summary.startTimer();
await this.serviceCall();
observe_response_time();
like image 125
Michael Doubez Avatar answered Nov 09 '22 22:11

Michael Doubez