Sobes.tech
Middle

What types of metrics in Prometheus are you familiar with?

sobes.tech AI

Answer from AI

Prometheus supports four main types of metrics:

  1. Counter: A monotonically increasing value that can only go up (or reset to zero upon process restart). Used for counting incremental events such as requests, errors, or completed tasks.
    // Example in Go
    var requestsTotal = prometheus.NewCounter(
        prometheus.CounterOpts{
            Name: "http_requests_total",
            Help: "Total number of HTTP requests.",
        },
    )
    
  2. Gauge: An arbitrary numeric value that can increase or decrease. Used to measure the current state of something, such as CPU load, memory usage, number of active users, or queue size.
    # Example in Python
    from prometheus_client import Gauge
    
    cpu_usage = Gauge('cpu_usage_percent', 'Current CPU usage percentage')
    cpu_usage.set(55.5)
    
  3. Histogram: Measures the distribution of samples (e.g., response times of requests) and groups them into configurable buckets. Provides data on the number of samples in each bucket and the total sum of values. Used for latency analysis.
    // Example in Java
    import io.prometheus.client.Histogram;
    
    static final Histogram requestLatencies = Histogram.build()
        .name("http_request_duration_seconds")
        .help("Request duration in seconds.")
        .buckets(0.1, 0.5, 1.0, 2.5, 5.0, 10.0) // Buckets
        .register();
    
  4. Summary: Similar to a histogram but calculates configurable quantiles of the sample distribution on the client side within a sliding time window. Also provides total count and sum of samples. Suitable for measuring latency when quantiles are important, but note that client-side calculation requires more computational power and may lead to less precise quantiles when aggregated.
    # Example in Ruby
    require 'prometheus/client'
    
    requests_summary = Prometheus::Client::Summary.new(:request_duration_seconds, 'Request duration in seconds.')
    

The choice of metric type depends on the nature of the data to be tracked.