Module prometheus

Module prometheus 

Source
Expand description

Copyright © 2025-2026 Wenze Wei. All Rights Reserved.

This file is part of DMSC. The DMSC project belongs to the Dunimd Team.

Licensed under the Apache License, Version 2.0 (the “License”); You may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

§Prometheus Exporter

This module provides a Prometheus exporter implementation for the DMSC framework. It allows registering and managing Prometheus metrics (counters, gauges, histograms) and generating Grafana dashboards from these metrics.

§Key Components

  • DMSCPrometheusExporter: Main exporter class for managing Prometheus metrics

§Design Principles

  1. Prometheus Integration: Uses the official prometheus crate for metric collection
  2. Thread Safety: Uses Arc and RwLock for safe concurrent access
  3. Multiple Metric Types: Supports Counter, Gauge, and Histogram metrics
  4. Grafana Integration: Provides methods to generate Grafana dashboards and panels
  5. Easy to Use: Simple API for registering and updating metrics
  6. Text Encoding: Exports metrics in Prometheus text format
  7. Registry Management: Maintains its own Prometheus registry
  8. Error Handling: Comprehensive error handling with DMSCResult

§Usage

use dmsc::prelude::*;
 
fn example() -> DMSCResult<()> {
    // Create a new Prometheus exporter
    let exporter = DMSCPrometheusExporter::new()?;
     
    // Register a counter metric
    exporter.register_counter("http_requests_total", "Total number of HTTP requests")?;
     
    // Register a gauge metric
    exporter.register_gauge("active_connections", "Number of active connections")?;
     
    // Register a histogram metric
    exporter.register_histogram(
        "response_time_seconds", 
        "Response time in seconds", 
        vec![0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0]
    )?;
     
    // Update metrics
    exporter.increment_counter("http_requests_total", 1.0)?;
    exporter.set_gauge("active_connections", 10.0)?;
    exporter.observe_histogram("response_time_seconds", 0.123)?;
     
    // Render metrics in Prometheus format
    let metrics_text = exporter.render()?;
    println!("Prometheus metrics:\n{}", metrics_text);
     
    // Generate a Grafana dashboard
    let dashboard = exporter.generate_default_dashboard()?;
    let dashboard_json = dashboard.to_json()?;
    println!("Grafana dashboard JSON:\n{}", dashboard_json);
     
    Ok(())
}

Structs§

DMSCPrometheusExporter
Prometheus exporter for managing metrics and generating Grafana dashboards.