Working with Metrics and Monitoring

1. Understanding Metric Types

TypeBehaviorExample
CounterMonotonic uprequests_total
GaugeUp/down valuequeue_depth
HistogramBucketed distributionrequest_duration_seconds
SummaryPre-computed quantilesresponse_size
Meter (Dropwizard)Rate over timeevents/sec

2. Implementing Application Metrics

Example: Micrometer + Prometheus

MeterRegistry reg = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
Counter created = Counter.builder("orders.created")
    .tag("region", "us-east-1")
    .register(reg);
Timer timer = Timer.builder("orders.create.duration")
    .publishPercentileHistogram()
    .register(reg);

timer.record(() -> { createOrder(req); created.increment(); });

3. Implementing Infrastructure Metrics

SourceTool
Host (CPU, mem, disk)node_exporter
ContainercAdvisor, kubelet
K8s objectskube-state-metrics
Networkblackbox_exporter, eBPF
DBpostgres_exporter, mysqld_exporter

4. Implementing Business Metrics

MetricExample
Conversion ratesignups / visitors
Revenue / secSum of completed payments
Active usersDAU, MAU
Funnel stagesCart → Checkout → Paid

5. Understanding RED Method (rate, errors, duration)

MetricDefinition
RateRequests/sec
Errors% failures
DurationLatency distribution (p50, p99)
Best forRequest-driven services

6. Understanding USE Method (utilization, saturation, errors)

MetricDefinition
Utilization% time resource busy
SaturationQueue length / wait time
ErrorsCount of error events
Best forResources (CPU, disk, network)

7. Implementing Metric Aggregation

MechanismDetail
Pull (Prometheus)Scrape /metrics endpoint
Push (StatsD, OTLP push)Client emits to collector
FederationHierarchical Prom servers
Long-term storeThanos, Mimir, Cortex, VictoriaMetrics

8. Implementing Alerting Rules

Example: Prometheus alert rule

groups:
  - name: api-slo
    rules:
      - alert: HighErrorRate
        expr: |
          sum(rate(http_requests_total{status=~"5.."}[5m]))
            / sum(rate(http_requests_total[5m])) > 0.01
        for: 10m
        labels: { severity: page }
        annotations:
          summary: "5xx error rate > 1% for 10m"
          runbook: "https://wiki/runbooks/api-errors"
PatternDetail
Multi-window multi-burnSLO burn-rate alerts (Google SRE)
Symptom-basedAlert on user impact, not causes
Actionable onlyEach page must have a clear action

9. Implementing Dashboards and Visualization

ToolDetail
GrafanaMulti-source dashboards
Datadog / NRSaaS
KibanaES-native
Best practiceService overview + per-route + dependency views

10. Understanding Metric Cardinality

CauseMitigation
High-cardinality labels (user id, request id)Move to traces/logs
Per-endpoint labels with regex pathsNormalize: /users/:id not /users/42
CostLinear series storage; query slowdown
LimitProm: keep total series < few million per server

11. Implementing SLI and SLO Tracking

SLI TypeExample
Availabilitygood_requests / total_requests
LatencyP(latency < threshold)
Quality% complete responses (no degraded)
Error budget burn(1 − SLI) / (1 − SLO)