Back to Monitoring & Logging
Q5Question 5 of 13

What are metrics, counters, gauges, and histograms

Answer: Metrics are numeric measurements collected over time, and Micrometer provides counters (cumulative totals), gauges (current values), and histograms (value distributions) to instrument Spring Boot banking applications for monitoring with Prometheus, Grafana, or Datadog.

Key Concepts
  • A metric is a numeric measurement collected over time that describes the behavior or health of a system.
  • A counter is a monotonically increasing value that tracks cumulative totals like total transfers processed.
  • A gauge is a value that can go up or down, representing a current state like active database connections.
  • A histogram tracks the distribution of values like request latency, enabling percentile calculations (p50, p95, p99).
  • Micrometer is the metrics facade for Spring Boot that works with Prometheus, Datadog, CloudWatch, and other backends.
  • The four golden signals (latency, traffic, errors, saturation) are the essential metrics for any banking service.
  • Custom business metrics like transfers per minute and failed authentication attempts provide domain-specific visibility.

Definition

Metrics are numeric measurements collected over time, and Micrometer provides counters (cumulative totals), gauges (current values), and histograms (value distributions) to instrument Spring Boot banking applications for monitoring with Prometheus, Grafana, or Datadog.
  • Metric is a named numeric value collected at regular intervals that describes system behavior (request count, latency, memory usage).
  • Counter is a metric that only increases, tracking cumulative totals: total HTTP requests, total failed transfers, total bytes sent.
  • Gauge is a metric that can increase or decrease, representing a snapshot: current active connections, current queue size, current heap usage.
  • Histogram records the distribution of observed values in configurable buckets, enabling percentile calculations: p50 latency, p95 latency, p99 latency.
  • Micrometer is a vendor-neutral metrics library for Java, similar to SLF4J for logging, that integrates with Prometheus, Datadog, CloudWatch, and others.

Think of the National Bank of India dashboard in the operations room. A counter is the total number of transactions processed today: it only goes up. A gauge is the number of customers currently in the branch: it goes up when someone walks in and down when they leave. A histogram is the distribution of transaction processing times: most take 200ms, some take 500ms, a few take 2 seconds. The operations manager watches these numbers to know if the branch is healthy or needs more tellers.

Understanding the Concepts

What are the different types of metrics?

Metrics fall into four categories based on what they measure and how their values behave.

TypeBehaviorExampleWhen to use
CounterOnly increases (or resets to 0)Total transfers processedCounting events
GaugeGoes up and downActive DB connectionsCurrent state
HistogramBuckets of value distributionTransfer latency in msLatency percentiles
TimerHistogram + counter for durationAPI response timeMeasuring how long things take
JAVA
package com.bank.metrics;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Map;
import java.util.TreeMap;

public class MetricTypesDemo {

    // Counter: only increases
    static AtomicLong totalTransfers = new AtomicLong(0);

    // Gauge: current value
    static AtomicInteger activeConnections = new AtomicInteger(0);

    // Histogram: distribution buckets
    static Map<String, Integer> latencyBuckets = new TreeMap<>();

    public static void main(String[] args) {
        // Counter: increment on each transfer
        totalTransfers.incrementAndGet();
        totalTransfers.incrementAndGet();
        totalTransfers.incrementAndGet();
        System.out.println("Counter (total_transfers): " + totalTransfers.get());

        // Gauge: connections go up and down
        activeConnections.incrementAndGet();
        activeConnections.incrementAndGet();
        activeConnections.decrementAndGet();
        System.out.println("Gauge (active_connections): " + activeConnections.get());

        // Histogram: record latencies
        recordLatency(150);
        recordLatency(200);
        recordLatency(350);
        recordLatency(1200);
        System.out.println("Histogram (latency buckets):");
        latencyBuckets.forEach((bucket, count) ->
            System.out.println("  " + bucket + ": " + count));
    }

    static void recordLatency(int ms) {
        if (ms <= 100) latencyBuckets.merge("le_100ms", 1, Integer::sum);
        else if (ms <= 250) latencyBuckets.merge("le_250ms", 1, Integer::sum);
        else if (ms <= 500) latencyBuckets.merge("le_500ms", 1, Integer::sum);
        else latencyBuckets.merge("le_inf", 1, Integer::sum);
    }
}
TEXT
Counter (total_transfers): 3
Gauge (active_connections): 1
Histogram (latency buckets):
  le_250ms: 2
  le_500ms: 1
  le_inf: 1

How do you use Micrometer in Spring Boot?

Micrometer is included in Spring Boot Actuator. Add the Prometheus registry dependency, and Spring Boot automatically exposes metrics at /actuator/prometheus.

XML
<!-- pom.xml dependencies --syn-kw">>
syn-kw"><dependencysyn-kw">>
    syn-kw"><groupIdsyn-kw">>org.springframework.bootsyn-kw"></groupIdsyn-kw">>
    syn-kw"><artifactIdsyn-kw">>spring-boot-starter-actuatorsyn-kw"></artifactIdsyn-kw">>
syn-kw"></dependencysyn-kw">>
syn-kw"><dependencysyn-kw">>
    syn-kw"><groupIdsyn-kw">>io.micrometersyn-kw"></groupIdsyn-kw">>
    syn-kw"><artifactIdsyn-kw">>micrometer-registry-prometheussyn-kw"></artifactIdsyn-kw">>
syn-kw"></dependencysyn-kw">>
YAML
# application.yml
management:
  endpoints:
    web:
      exposure:
        include: health,prometheus,metrics
  metrics:
    tags:
      application: transfer-service
      environment: production
JAVA
package com.bank.service;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import java.math.BigDecimal;
import java.time.Duration;

public class TransferServiceWithMetrics {

    private final Counter transferSuccess;
    private final Counter transferFailed;
    private final Timer transferLatency;

    public TransferServiceWithMetrics(MeterRegistry registry) {
        this.transferSuccess = Counter.builder("bank.transfers.total")
            .tag("status", "success")
            .description("Total successful transfers")
            .register(registry);

        this.transferFailed = Counter.builder("bank.transfers.total")
            .tag("status", "failed")
            .description("Total failed transfers")
            .register(registry);

        this.transferLatency = Timer.builder("bank.transfers.duration")
            .description("Transfer processing time")
            .publishPercentiles(0.5, 0.95, 0.99)
            .publishPercentileHistogram()
            .register(registry);
    }

    public void transfer(String from, String to, BigDecimal amount) {
        transferLatency.record(() -> {
            try {
                processTransfer(from, to, amount);
                transferSuccess.increment();
            } catch (Exception e) {
                transferFailed.increment();
                throw e;
            }
        });
    }

    private void processTransfer(String from, String to, BigDecimal amount) {
        // Business logic here
        System.out.println("Transfer: " + from + " -> " + to + " amount=" + amount);
    }
}
TEXT
Transfer: SAV2001 -> SAV3002 amount=25000.00

What are the four golden signals?

Google's Site Reliability Engineering book defines four golden signals that every service should measure:

SignalWhat it measuresMetric example
LatencyTime to serve a requestbank.transfers.duration (p50, p95, p99)
TrafficHow much demand is on the systembank.transfers.total (rate)
ErrorsRate of failed requestsbank.transfers.total{status=failed}
SaturationHow full the system ishikaricp.connections.active / max
JAVA
package com.bank.metrics;

public class GoldenSignalsDemo {
    public static void main(String[] args) {
        System.out.println("Four Golden Signals for Transfer Service:");
        System.out.println();
        System.out.println("1. Latency:");
        System.out.println("   bank_transfers_duration_seconds{quantile=0.5}  = 0.15");
        System.out.println("   bank_transfers_duration_seconds{quantile=0.95} = 0.45");
        System.out.println("   bank_transfers_duration_seconds{quantile=0.99} = 1.20");
        System.out.println();
        System.out.println("2. Traffic:");
        System.out.println("   rate(bank_transfers_total[5m]) = 120 transfers/min");
        System.out.println();
        System.out.println("3. Errors:");
        System.out.println("   rate(bank_transfers_total{status=failed}[5m]) = 3/min");
        System.out.println("   Error rate: 3/120 = 2.5%");
        System.out.println();
        System.out.println("4. Saturation:");
        System.out.println("   hikaricp_connections_active / hikaricp_connections_max = 8/10 (80%)");
    }
}
TEXT
Four Golden Signals for Transfer Service:

1. Latency:
   bank_transfers_duration_seconds{quantile=0.5}  = 0.15
   bank_transfers_duration_seconds{quantile=0.95} = 0.45
   bank_transfers_duration_seconds{quantile=0.99} = 1.20

2. Traffic:
   rate(bank_transfers_total[5m]) = 120 transfers/min

3. Errors:
   rate(bank_transfers_total{status=failed}[5m]) = 3/min
   Error rate: 3/120 = 2.5%

4. Saturation:
   hikaricp_connections_active / hikaricp_connections_max = 8/10 (80%)

How do you create custom business metrics?

Beyond technical metrics, banking applications need business metrics: transfers per account type, loan approval rate, average transaction amount.

JAVA
package com.bank.metrics;

import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import java.math.BigDecimal;
import java.util.concurrent.atomic.AtomicInteger;

public class BusinessMetrics {

    private final DistributionSummary transferAmountSummary;
    private final AtomicInteger pendingLoans = new AtomicInteger(0);

    public BusinessMetrics(MeterRegistry registry) {
        // Distribution summary: track transfer amounts
        this.transferAmountSummary = DistributionSummary
            .builder("bank.transfer.amount")
            .baseUnit("INR")
            .description("Distribution of transfer amounts")
            .publishPercentiles(0.5, 0.95)
            .register(registry);

        // Gauge: current pending loan applications
        Gauge.builder("bank.loans.pending", pendingLoans, AtomicInteger::get)
            .description("Number of pending loan applications")
            .register(registry);
    }

    public void recordTransfer(BigDecimal amount) {
        transferAmountSummary.record(amount.doubleValue());
    }

    public void loanSubmitted() { pendingLoans.incrementAndGet(); }
    public void loanProcessed() { pendingLoans.decrementAndGet(); }
}

What are common mistakes with metrics?

Mistake 1: High-cardinality labels

Wrong: using customer ID or transaction ID as a metric label. This creates millions of time series and crashes Prometheus.

JAVA
// WRONG: customer ID as label creates millions of unique time series
Counter.builder("bank.transfers.total")
    .tag("customer_id", "CUST-98765")  // Millions of unique values!
    .register(registry);

Correct: use low-cardinality labels like account type, status, or region.

JAVA
// CORRECT: bounded set of values
Counter.builder("bank.transfers.total")
    .tag("account_type", "savings")  // savings, current, nre
    .tag("status", "success")         // success, failed
    .register(registry);

What should a senior engineer know?

- Prometheus query language (PromQL). Calculate error rates: rate(bank_transfers_total{status="failed"}[5m]) / rate(bank_transfers_total[5m]). Calculate p99 latency: histogram_quantile(0.99, rate(bank_transfers_duration_seconds_bucket[5m])).

- Metric naming conventions. Prometheus recommends: ___. Example: bank_transfers_duration_seconds_count. Use snake_case, include the unit, and use standard suffixes (_total for counters, _seconds for durations).

- Recording rules. Pre-compute expensive PromQL expressions as new time series. This speeds up dashboard loading for high-traffic banking services.

- RED method vs USE method. RED (Rate, Errors, Duration) is for request-driven services (APIs). USE (Utilization, Saturation, Errors) is for resource-oriented systems (databases, queues). Banking APIs use RED. Database monitoring uses USE.

- Alerting on symptoms, not causes. Alert on "transfer API error rate > 5%" (symptom), not "database CPU > 80%" (cause). Symptoms directly impact customers.

How do you set up alerting based on metrics?

Alerting rules define conditions that trigger notifications when metrics cross thresholds. For a banking API, you might alert when error rate exceeds 1% or p99 latency exceeds 500ms.

JAVA
package com.bank.monitoring;

public class AlertingRules {
    public static void main(String[] args) {
        System.out.println("Banking API alerting rules:");
        System.out.println("  1. Error rate > 1% for 5 minutes -> PagerDuty");
        System.out.println("  2. p99 latency > 500ms for 10 minutes -> Slack");
        System.out.println("  3. Connection pool usage > 80% -> Slack");
        System.out.println("  4. Transaction failure rate > 0.1% -> PagerDuty");
    }
}
TEXT
Expected output:
Banking API alerting rules:
  1. Error rate > 1% for 5 minutes -> PagerDuty
  2. p99 latency > 500ms for 10 minutes -> Slack
  3. Connection pool usage > 80% -> Slack
  4. Transaction failure rate > 0.1% -> PagerDuty

What are common interview follow-up questions?

  • What is the difference between a counter and a gauge?
  • How do you calculate p99 latency from a histogram?
  • What is the RED method for monitoring microservices?
  • How do you avoid high-cardinality metric labels?
  • What is the difference between push-based and pull-based metrics collection?

Visual Overview

DIAGRAM
  Metric Types
  =============

  Counter (total_transfers)      Gauge (active_connections)
  ─────────────────────────      ─────────────────────────
       ╱                              ╱╲
      ╱                              ╱  ╲
     ╱                              ╱    ╲
    ╱                              ╱      ╲
  ──────────────── time          ──────────── time
  Only goes up                   Goes up and down


  Histogram (latency buckets)
  ============================
  Count
   │
   │  ████
   │  ████ ██
   │  ████ ██ ██
   │  ████ ████ ██
   └──────────────── latency (ms)
     100  250  500  1000


  Micrometer -> Prometheus -> Grafana
  ====================================

  Spring Boot App           Prometheus         Grafana
  ┌───────────────┐        ┌───────────┐      ┌──────────┐
  │ Micrometer    │ <─pull─│ Scrapes   │ ──>  │Dashboard │
  │ /actuator/    │  /15s  │ /metrics  │      │ Graphs   │
  │ prometheus    │        │ Stores    │      │ Alerts   │
  └───────────────┘        └───────────┘      └──────────┘

Key Takeaway

Metrics are the vital signs of your banking application. Just as a doctor monitors heart rate, blood pressure, and temperature, an operations team monitors latency, error rate, traffic, and saturation. Without metrics, you discover problems when customers call. With metrics, you see the problem on a dashboard before customers notice.

Micrometer is the metrics library for Spring Boot. It provides counters for cumulative totals (total transfers processed), gauges for current state (active database connections), and histograms for distributions (transfer latency percentiles). Micrometer is vendor-neutral: you write metrics once and export to Prometheus, Datadog, or CloudWatch by changing a dependency.

The four golden signals, latency, traffic, errors, and saturation, are the minimum metrics for any banking service. Custom business metrics like transfers per account type, average transaction amount, and failed authentication attempts add domain-specific visibility that technical metrics alone cannot provide.

The most dangerous mistake is high-cardinality labels. Using a customer ID or transaction ID as a metric tag creates millions of unique time series and can crash your monitoring system. Always use bounded label values: account type (savings, current), status (success, failed), region (mumbai, delhi).

One sentence to remember: counters count events, gauges measure current state, histograms show distributions, and the four golden signals tell you if your banking service is healthy.

---

Share
Use ← → arrow keys to navigate