# srvariable/mongo-exporter architecture

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

- Interactive diagram: https://gitdiagram.com/srvariable/mongo-exporter
- Repository: https://github.com/srvariable/mongo-exporter

## Overview

##### Project Type and Purpose
- This is a Go-based “mongo-exporter” microservice whose sole responsibility is to collect various MongoDB metrics (CPU, RAM, connections, operation counters, collection stats) and expose them via HTTP endpoints. It’s designed for integration with Prometheus (scraping) and Grafana (visualization).

##### Core System Components
a. HTTP API Layer
- Built with Gin (router/router.go, router/*.go).
- Defines endpoints under `/v1/`.
- Controllers dispatch to the internal metric service.
b. Service Layer
- internal/metric/service: orchestrates fetching of data from repositories and mapping to DTOs.
c. Repository Layer
- internal/metric/repository: two implementations
- mongo driver (internal/metric/repository/mongo/*)
- in-memory mocks (for tests)
- Exposes a common interface (i_repository.go).
d. Domain Layer
- internal/metric/domain: value objects and entities representing CPU, RAM, connections, opcounters, collection metrics.
e. Database
- MongoDB container (docker/mongo/Dockerfile, docker-compose.yml).
f. Monitoring Stack
- Prometheus (prometheus/prometheus.yml) scrapes exposed metrics.
- Grafana (grafana/dashboards, grafana/datasources) visualizes time-series.
g. Infrastructure & Deployment
- Docker Compose orchestrates Go app, MongoDB, Prometheus, Grafana.
- Makefile for build and deployment.
- .env for runtime configuration (DB_HOST, ports, credentials).

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Clients"
        Browser["Browser / curl"]:::external
        PromClient["Prometheus Scraper"]:::external
    end

    subgraph "Docker Compose"
        direction TB
        subgraph "mongo-exporter Service"
            direction TB
            subgraph "HTTP/API Layer"
                Rtr["router/router.go<br/>healthcheck.go, hello.go, metric.go, prometheus.go"]:::service
            end
            subgraph "Service Layer"
                Svc["internal/metric/service/<br/>service.go, cpu.go, ram.go, connections.go,<br/>opcounters.go, collection.go"]:::service
            end
            subgraph "Repository Layer"
                RepoInt["i_repository.go"]:::service
                RepoMongo["mongo implementation<br/>repository.go, utils.go,<br/>cpu.go, ram.go, connections.go,<br/>opcounters.go, collection.go"]:::service
                RepoMock["mock implementation<br/>repository.go, cpu.go, ram.go,<br/>connections.go, opcounters.go, collection.go"]:::service
            end
            subgraph "Domain Layer"
                Domain["internal/metric/domain/metric.go<br/>value_object/*.go"]:::service
            end
            Entryp["cmd/mongo-exporter/main.go"]:::service
            Config["config/env.go<br/>.env.example"]:::infra
        end

        subgraph "External Services"
            Mongo["MongoDB<br/>(27017)"]:::db
            Prometheus["Prometheus<br/>(9090)"]:::monitor
            Grafana["Grafana<br/>(3000)"]:::monitor
        end

        Infra["Makefile<br/>docker/golang/Dockerfile<br/>docker/mongo/Dockerfile<br/>docker-compose.yml<br/>.github/workflows/ci.yml"]:::infra
    end

    Browser -->|"/v1/metrics/*"| Rtr
    PromClient -->|"scrape /v1/metrics/prometheus"| Rtr
    Rtr -->|"calls"| Svc
    Svc -->|"uses interface"| RepoInt
    RepoInt -->|"impl"| RepoMongo
    RepoInt -->|"impl"| RepoMock
    RepoMongo -->|"queries"| Mongo
    Svc -->|"uses entities"| Domain
    Infra --- Entryp
    Config --> Entryp
    Infra --- Mongo
    Infra --- Prometheus
    Infra --- Grafana

    click Rtr "https://github.com/srvariable/mongo-exporter/blob/main/router/router.go"
    click Svc "https://github.com/srvariable/mongo-exporter/blob/main/internal/metric/service/service.go"
    click RepoInt "https://github.com/srvariable/mongo-exporter/blob/main/internal/metric/repository/i_repository.go"
    click RepoMongo "https://github.com/srvariable/mongo-exporter/blob/main/internal/metric/repository/mongo/repository.go"
    click RepoMock "https://github.com/srvariable/mongo-exporter/blob/main/internal/metric/repository/mock/repository.go"
    click Domain "https://github.com/srvariable/mongo-exporter/blob/main/internal/metric/domain/metric.go"
    click Entryp "https://github.com/srvariable/mongo-exporter/blob/main/cmd/mongo-exporter/main.go"
    click Config "https://github.com/srvariable/mongo-exporter/blob/main/config/env.go"
    click Infra "https://github.com/srvariable/mongo-exporter/tree/main/docker/golang/Dockerfile"
    click Mongo "https://github.com/srvariable/mongo-exporter/tree/main/docker/mongo/Dockerfile"
    click Prometheus "https://github.com/srvariable/mongo-exporter/blob/main/prometheus/prometheus.yml"
    click Grafana "https://github.com/srvariable/mongo-exporter/blob/main/grafana/dashboards/dashboard.json"

    classDef service fill:#cce5ff,stroke:#3399ff,color:#000;
    classDef external fill:#d4edda,stroke:#28a745,color:#000;
    classDef db fill:#d4edda,stroke:#28a745,color:#000;
    classDef monitor fill:#d4edda,stroke:#28a745,color:#000;
    classDef infra fill:#f0f0f0,stroke:#6c757d,color:#000;
```
