prometheus.go

Overview

The [prometheus.go](/projects/291/69100) file defines a metrics instrumentation layer for monitoring an application using Prometheus, a popular open-source monitoring and alerting toolkit. It encapsulates the creation, registration, and organization of Prometheus metrics, specifically tailored for HTTP request tracking and WebSocket client connection counts.

The primary purpose of this file is to provide a reusable and extensible Prometheus metrics registry with pre-defined metrics that track:

This file is foundational in enabling observability for the system by exposing meaningful metrics that can be scraped by Prometheus servers and visualized or alerted on via tools like Grafana.


Package and Imports


Types

Prometheus

type Prometheus struct {
    Registry *prometheus.Registry
    Metrics  Metrics
}

**Description:** The `Prometheus` struct is a container that holds a Prometheus registry (`prometheus.Registry`) and a collection of application-specific metrics (`Metrics`). This struct provides a centralized object to access all registered metrics and the registry itself.

**Fields:**


Metrics

type Metrics struct {
    HTTPRequestCounter         *prometheus.CounterVec
    HTTPRequestDurationSeconds *prometheus.HistogramVec
    WebsocketCount             prometheus.Gauge
}

**Description:** Defines specific Prometheus metric collectors used by the application.

**Fields:**


Labels

type Labels = prometheus.Labels

**Description:** Type alias for `prometheus.Labels`, which is a map of string keys to string values used to label metrics.


Functions

NewPrometheus

func NewPrometheus(coinstack string) *Prometheus

**Description:** Creates a new `Prometheus` instance initialized with a fresh registry and registers default Go runtime and process collectors along with custom application metrics.

**Parameters:**

**Returns:**

**Implementation Details:**

**Example Usage:**

p := NewPrometheus("bitcoin")
// Now p.Registry can be used to expose metrics endpoint,
// and p.Metrics.HTTPRequestCounter can be incremented in HTTP handlers.

Important Implementation Details


Interaction with the System


Mermaid Diagram

classDiagram
    class Prometheus {
        +Registry *prometheus.Registry
        +Metrics Metrics
    }

    class Metrics {
        +HTTPRequestCounter *prometheus.CounterVec
        +HTTPRequestDurationSeconds *prometheus.HistogramVec
        +WebsocketCount prometheus.Gauge
    }

    Prometheus "1" *-- "1" Metrics : contains

Summary

The [prometheus.go](/projects/291/69100) file provides a clean, extensible Prometheus instrumentation setup for HTTP and WebSocket metrics. It encapsulates metric creation, registration, and organization, enabling other parts of the system to easily integrate metrics collection with minimal boilerplate. The use of reflection and constant labels ensures maintainability and contextual richness for monitoring data. This file forms a critical part of the system's observability infrastructure.