# gaoyuan98/dameng_exporter architecture

> Architecture diagram of the GitHub repository gaoyuan98/dameng_exporter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gaoyuan98/dameng_exporter
- Repository: https://github.com/gaoyuan98/dameng_exporter

## Overview

##### Main Components
- Exporter Core: The central application (main file dameng_exporter.go) that initializes the service, loads configurations, sets up the HTTP endpoint, and serves the scraped metrics.
- Collector Module: Located in the “collector” directory. Each Go file (for example, db_monitor_info_collector.go, db_instance_running_info_collector.go, etc.) implements a specific metric collection logic by executing queries against the Dameng database.
- Configuration Module: Found in the “config” directory and further supported by external configuration files like dameng_exporter.config and custom_metrics.toml. This module manages database connection details, SQL queries, timeouts, logging and other runtime parameters.
- Database Connection Utility: Code in both the “db” directory and parts of “config” handles the interaction with the Dameng DB. It abstracts connection pooling, query execution, and error handling.
- Logging Utility: The “logger” module is in its separate directory to handle the logging needs with configurable log rotation and size parameters.
- Docker/Deployment Setup: The Dockerfile and associated scripts (build_all_versions.bat) indicate that the module is containerized for easy deployment across different platforms (Linux AMD64, ARM64 and Windows).

##### Relationships & Data Flow
- At start-up, the core executable loads configuration (from dameng_exporter.config and possibly custom_metrics.toml for custom SQL queries).
- The configuration module sets up database connection parameters using the utility functions.
- The Collector Module runs scheduled queries (from various collector_xxx.go files), each focusing on a specific area such as instance logs, sessions, slow SQL, tablespace information, and custom metrics.
- Once data is collected, the data is formatted into Prometheus metric standards, including metric names, help texts, types, and label annotations.
- The metrics are then exposed via an HTTP endpoint (default on port 9200), from which Prometheus scrapes the data.
- The external environment (Prometheus/Grafana) will interact with the exporter for monitoring, whereas internal modules interact using standard Go function calls.

##### Architectural Patterns & Key Technologies
- Modularity: Each metric category is encapsulated into its own Go file within the collector directory.
- Separation of Concerns: Distinct layers manage configuration, collection, database interaction, logging, and HTTP serving.
- Extensibility: Custom metrics can be added through an external TOML file. New collectors can be integrated by creating additional modules within the “collector” directory.
- Containerization: A Dockerfile is provided to enable a consistent deployment environment.
- Prometheus Integration: The design follows Prometheus exporter standards (exposing an HTTP metrics endpoint for scraping).

##### Elements
- Clearly labeled components:
- Exporter Core Application
- Collector Modules (individual metric collectors)
- Configuration Module (including external config files)
- Database Connection Module with connection pool and query engine
- Logging Service
- HTTP Server / Metrics Endpoint (e.g., “/metrics”)
- External Systems: Prometheus (scraping mechanism) and Grafana (dashboard visualization)
- Startup configuration loading → Database connection initialization
- Database connection → Query execution (by each collector)
- Collector modules → Metric aggregation and formatting
- Metric exposure endpoint → Prometheus scrape
- Prometheus → Grafana dashboard visualization (data flow)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Systems
    subgraph "External Systems"
        A1["Prometheus"]:::external
        A2["Grafana Dashboards"]:::external
    end

    %% Core Application Layer
    subgraph "Core Application"
        B1["Exporter Core Application"]:::core
        subgraph "Configuration"
            B2["Configuration Module"]:::config
            B2a["dameng_exporter.config"]:::configFile
            B2b["custom_metrics.toml"]:::configFile
        end
        B3["Collector Modules"]:::collector
        B4["Database Connection Utility"]:::db
        B5["Logging Service"]:::logger
        B6["HTTP Server (/metrics)"]:::endpoint
    end

    %% Infrastructure Layer
    subgraph "Infrastructure"
        C1["Dameng Database"]:::database
        subgraph "Deployment Environment"
            C2["Docker/Deployment Setup"]:::deploy
            C2a["Dockerfile"]:::deployFile
            C2b["build_all_versions.bat"]:::deployFile
        end
    end

    %% Relationships
    B1 -->|"loads_config"| B2
    B2 -->|"provides_DB_params"| B4
    B1 -->|"initiates_collectors"| B3
    B3 -->|"queries_database"| B4
    B3 -->|"aggregates_metrics"| B6
    B1 -->|"logs_to"| B5
    B4 -->|"executes_queries"| C1
    B6 -->|"scraped_by"| A1
    A1 -->|"exports_to"| A2

    %% Click Events
    click B1 "https://github.com/gaoyuan98/dameng_exporter/blob/master/dameng_exporter.go"
    click B2 "https://github.com/gaoyuan98/dameng_exporter/tree/master/config/"
    click B2a "https://github.com/gaoyuan98/dameng_exporter/blob/master/config/dameng_exporter.config"
    click B2b "https://github.com/gaoyuan98/dameng_exporter/blob/master/config/custom_metrics.toml"
    click B3 "https://github.com/gaoyuan98/dameng_exporter/tree/master/collector/"
    click B4 "https://github.com/gaoyuan98/dameng_exporter/blob/master/db/db.go"
    click B5 "https://github.com/gaoyuan98/dameng_exporter/blob/master/logger/logger.go"
    click C2a "https://github.com/gaoyuan98/dameng_exporter/tree/master/Dockerfile"
    click C2b "https://github.com/gaoyuan98/dameng_exporter/blob/master/build_all_versions.bat"
    click A2 "https://github.com/gaoyuan98/dameng_exporter/tree/master/doc/"

    %% Styles
    classDef external fill:#ffcccc,stroke:#aa0000,stroke-width:2px;
    classDef core fill:#ccf2ff,stroke:#0066cc,stroke-width:2px;
    classDef config fill:#e6ffe6,stroke:#33cc33,stroke-width:2px;
    classDef configFile fill:#f2fff2,stroke:#33cc33,stroke-width:1px,stroke-dasharray: 5 5;
    classDef collector fill:#fff2cc,stroke:#cc9900,stroke-width:2px;
    classDef db fill:#ffe6f2,stroke:#cc3399,stroke-width:2px;
    classDef logger fill:#f2e6ff,stroke:#9933cc,stroke-width:2px;
    classDef endpoint fill:#e6f7ff,stroke:#3399ff,stroke-width:2px;
    classDef database fill:#fff0e6,stroke:#ff9933,stroke-width:2px;
    classDef deploy fill:#e6e6e6,stroke:#666666,stroke-width:2px;
    classDef deployFile fill:#f2f2f2,stroke:#666666,stroke-width:1px,stroke-dasharray: 5 5;
```
