# tony-spark/metrico architecture

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

- Interactive diagram: https://gitdiagram.com/tony-spark/metrico
- Repository: https://github.com/tony-spark/metrico

## Overview

##### Main components
- Agent: In the cmd/agent and internal/agent directories, you find the core logic for collecting metrics, configuration handling, and the abstraction over various transports (HTTP, gRPC, etc.).
- Server: Located in cmd/server and internal/server, this handles incoming metrics, provides APIs (both HTTP and gRPC, as evidenced in directories like internal/server/http and internal/server/grpc), and further processes and persists the data.
- Data Storage and Persistence: Within internal/server/storage you have multiple storage implementations (file-based, PostgreSQL, perhaps even single-value in-memory storage) along with a “db” directory that includes migration scripts for setting up database tables.
- Transport Layers: Both the agent and server modules have dedicated packages for handling different transport mechanisms (e.g., internal/agent/transports for the agent and internal/server/{http,grpc} for the server). This outlines a clear separation and abstraction between the communication protocols.
- Configuration & Security: Directories like internal/config, cmd/agent/README.md, and the various config files (agent-config.json, server-config.json) indicate a modular setup for configuration management. The crypto and keys directories are used for signing or encrypting data, adding a security layer.
- Static Lint Tool: The cmd/staticlint and internal/staticlint directories represent an auxiliary tool to improve code quality by analyzing for common pitfalls (like potential misuse of os.Exit, for example).

##### Relationships and interactions
- The Server receives the metrics and uses its “services” and “models” packages to process and perhaps persist them. Include the storage backends (file system, PostgreSQL, or in-memory) that the server writes to.
- External assets (like the web UI templates under assets/templates or static asset generation via assets/assets.go) are used to display metrics. You may show these as a separate component connected to the Server.
- The API interface is highlighted by the proto and generated pb directories. These represent interoperable definitions for both HTTP and gRPC services, so show them as part of the Server’s API layer.
- The static analyzer tool (staticlint) runs separately, likely during CI/CD (as seen in the GitHub workflows), so represent it as an auxiliary tool used during development rather than at runtime.

##### Architectural patterns and design principles
- Modular Separation: Clear separation between the Agent and Server, each with dedicated modules (configuration, transports, metrics, etc.).
- Abstraction and extensibility: The use of transport packages and the configuration modules indicate an architecture that encourages adding more protocols or storage solutions without altering core logic.
- Layered Architecture: Presentation (web/UI templates and API endpoints), Business/Services (processing and persistence) and Data Access (storage implementations) are distinct.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Runtime Components
    subgraph "Runtime Components"
        A["Agent (Metrics Collector)"]:::runtime
        S["Server (Metrics Processor)"]:::runtime
    end

    %% Transport Layers
    subgraph "Transport Layers"
        AT["Agent Transports"]:::transport
        SHT["Server HTTP"]:::transport
        SGRPC["Server gRPC"]:::transport
    end

    %% Supporting Components
    DS["Data Storage & Persistence"]:::storage
    CS["Configuration & Security"]:::config
    API["API/Protocol Definitions"]:::api
    SL["Static Lint Tool"]:::aux

    %% Connections between components
    A -->|"HTTP"| S
    A -->|"gRPC"| S
    AT -->|"supports"| A
    SHT -->|"supports"| S
    SGRPC -->|"supports"| S
    S -->|"stores metrics"| DS
    CS --> A
    CS --> S
    API --> S

    %% Styles
    classDef runtime fill:#e6f7ff,stroke:#1890ff,stroke-width:2px;
    classDef transport fill:#fff7e6,stroke:#faad14,stroke-width:2px;
    classDef storage fill:#f6ffed,stroke:#52c41a,stroke-width:2px;
    classDef config fill:#fff0f6,stroke:#eb2f96,stroke-width:2px;
    classDef api fill:#f9f0ff,stroke:#722ed1,stroke-width:2px;
    classDef aux fill:#f0fff0,stroke:#13a13d,stroke-width:2px;

    %% Click Events for Agent
    click A "https://github.com/tony-spark/metrico/tree/main/cmd/agent"
    click A "https://github.com/tony-spark/metrico/tree/main/internal/agent"

    %% Click Events for Server
    click S "https://github.com/tony-spark/metrico/tree/main/cmd/server"
    click S "https://github.com/tony-spark/metrico/tree/main/internal/server"

    %% Click Events for Data Storage
    click DS "https://github.com/tony-spark/metrico/tree/main/internal/server/storage"
    click DS "https://github.com/tony-spark/metrico/tree/main/db/migrations"

    %% Click Events for Transport Layers
    click AT "https://github.com/tony-spark/metrico/tree/main/internal/agent/transports"
    click SHT "https://github.com/tony-spark/metrico/tree/main/internal/server/http"
    click SGRPC "https://github.com/tony-spark/metrico/tree/main/internal/server/grpc"

    %% Click Events for Configuration & Security
    click CS "https://github.com/tony-spark/metrico/tree/main/internal/config"
    click CS "https://github.com/tony-spark/metrico/blob/main/agent-config.json"
    click CS "https://github.com/tony-spark/metrico/blob/main/server-config.json"
    click CS "https://github.com/tony-spark/metrico/tree/main/internal/crypto"

    %% Click Events for Static Lint Tool
    click SL "https://github.com/tony-spark/metrico/tree/main/cmd/staticlint"
    click SL "https://github.com/tony-spark/metrico/tree/main/internal/staticlint"

    %% Click Events for API / Protocol Definitions
    click API "https://github.com/tony-spark/metrico/tree/main/proto"
    click API "https://github.com/tony-spark/metrico/tree/main/gen/pb"
    click API "https://github.com/tony-spark/metrico/blob/main/gen/pb/api/metrico.pb.go"
    click API "https://github.com/tony-spark/metrico/blob/main/gen/pb/api/metrico_grpc.pb.go"
```
