Metrics Collection

Purpose

Metrics Collection focuses on capturing detailed runtime statistics from blockchain API servers and related services to enable observability and performance monitoring. Unlike the broader monitoring topic that includes alerting and dashboarding, this subtopic specifically addresses gathering, structuring, and exposing Prometheus metrics for HTTP request patterns, WebSocket client connections, and Kubernetes resource states within the blockchain coinstack infrastructure.

The goal is to provide real-time insight into API usage, latency, and connection health, which helps operators optimize performance, detect anomalies, and maintain system reliability.

Functionality

This subtopic implements a Prometheus metrics client integrated into the API server stack. Key workflows and features include:

Example: Prometheus Metrics Setup Class

The following snippet from [prometheus.ts](/projects/291/69099) illustrates the core class encapsulating Prometheus metric registration:

import client from 'prom-client'

export class Prometheus {
  register: client.Registry

  metrics = {
    httpRequestCounter: new client.Counter({
      name: 'unchained_http_request_count',
      help: 'Count of http requests',
      labelNames: ['method', 'route', 'statusCode'],
    }),
    httpRequestDurationSeconds: new client.Histogram({
      name: 'unchained_http_request_duration_seconds',
      help: 'Duration of HTTP requests in seconds',
      labelNames: ['method', 'route', 'statusCode'],
    }),
    websocketCount: new client.Gauge({
      name: 'unchained_ws_client_count',
      help: 'Count of websocket client connections',
    }),
  }

  constructor({ coinstack }: { coinstack: string }) {
    this.register = new client.Registry()
    this.register.setDefaultLabels({ coinstack })

    client.collectDefaultMetrics({ register: this.register })

    Object.values(this.metrics).forEach((value) => this.register.registerMetric(value))
  }
}

Integration

Metrics Collection integrates tightly with the Unified API Layer and Kubernetes infrastructure:

This subtopic introduces the standardized, code-level setup for metrics registration and collection, which is not covered by the broader monitoring topic or alerting configuration. It provides the foundation upon which monitoring dashboards and alerting rules are built.

Diagram

flowchart TD
  API[API Server] -->|HTTP Request| Middleware[Prometheus Middleware]
  Middleware -->|Increment Counters & Observe Latency| MetricsRegistry[Prometheus Registry]
  API -->|WebSocket Client Connect/Disconnect| WSHandler[WebSocket Connection Handler]
  WSHandler -->|Update Gauge| MetricsRegistry
  MetricsRegistry -->|Expose Metrics| /metrics[HTTP /metrics Endpoint]
  PrometheusServer[Prometheus Scraper] -->|Scrape /metrics| /metrics
  PrometheusServer -->|Store Metrics Data| TSDB[(Time-Series DB)]
  TSDB -->|Visualize| Grafana[Grafana Dashboard]

This Metrics Collection subtopic establishes a robust and extensible framework for gathering operational metrics, enabling effective observability and proactive system management across diverse blockchain coinstacks.