# thanos-io/thanos architecture

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

- Interactive diagram: https://gitdiagram.com/thanos-io/thanos
- Repository: https://github.com/thanos-io/thanos

## Overview

##### Main Components
- Frontend / Query Layer: Illustrate the Thanos Query and Query-Frontend components that provide a unified global query view. (Look at the “cmd/thanos/query.go”, “pkg/query”, “pkg/queryfrontend” directories.)
- Sidecars: Represent the “sidecar” component that sits beside Prometheus servers and exposes the Store API; it acts as an interface between Prometheus and the Thanos system.
- Store Gateway / Block Storage: Capture the Store component which reads data (blocks) from object storage. (Refer to “pkg/store”, “pkg/block”, and the block/indexheader components.)
- Compactor: Include the Compactor which is responsible for downsampling, deduplication, and retention. (See “cmd/thanos/compact.go” and “pkg/compact”.)
- Receive: Show the Receive component meant for scaling ingestion or integrating with remote-write compatible sources. (“cmd/thanos/receive.go” and “pkg/receive” directories give details.)
- Ruler and Rule: Incorporate the Rule component’s role in managing alerts and recording rules, with deduplication merging. (Found under “cmd/thanos/rule.go” and “pkg/rules”.)
- Additional Support Components: Optionally, include components for replication (from “pkg/replicate”), caching layers (from “pkg/cache” and “pkg/cacheutil”), service/discovery modules (see “pkg/discovery”), metrics logging/tracing (see “pkg/tracing”), and the UI component (package “pkg/ui” including the react-app).

##### Relationships and Interfaces
- Prometheus servers output metrics to their associated Sidecar.
- Sidecars expose the Store API that is consumed by Query and Query-Frontend.
- The Query component aggregates and deduplicates responses from multiple Sidecars and Store Gateways.
- The Store Gateway retrieves historical data blocks from external Object Storage.
- The Compactor reads from Object Storage, processes blocks (downsampling/deduplication), and writes back compacted/optimized blocks.
- The Receive component provides a scalable ingestion path that might also feed data into the long-term storage through block writing.
- Additional components such as Rule, Replicator, and caching layers interact with both querying and ingest paths.

##### Architectural Patterns and Design Principles
- Modularity and microservice-style separation. Each Thanos component (Query, Sidecar, Store, Compactor, Receive, etc.) runs as an independent process that can be scaled or deployed independently.
- Use of Prometheus’ native block storage format for inexpensive long-term storage coupled with on-the-fly deduplication for high availability.
- Caching and service discovery mechanisms which support performance and resiliency.
- If desired, indicate the use of gRPC (as found in “pkg/server/grpc”) for internal communication between components.

##### Technologies, Frameworks, and Domains
- Go (the project is written in Go, as indicated by the numerous go.mod files).
- Object storage services (such as S3, GCS, etc.) as an external dependency.
- gRPC for internal communication and HTTP for external endpoints.
- React-based UI for visualization and configuration (from the “pkg/ui/react-app”).
- Docker and CI/CD (as seen in Dockerfiles and GitHub/CircleCI pipelines) which can be shown as supporting “deployment scaffolding”.

##### Additional Detailing
- Make mention of testing components (from “test/e2e” directories) to show the maturity of system validation—but keep this separate from the primary architectural depiction.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Ingestion Layer
    subgraph "Ingestion Layer"
        P("Prometheus"):::external
        SC("Thanos Sidecar"):::sidecar
        RC("Thanos Receive"):::receive
        P -->|"metrics_export"| SC
        RC -->|"data_ingestion"| SC
    end

    %% Query Layer
    subgraph "Query Layer"
        Q("Thanos Query"):::query
        QF("Thanos Query-Frontend"):::query
        SC -->|"storeAPI_call"| Q
        SC -->|"storeAPI_call"| QF
    end

    %% Storage Layer
    subgraph "Storage Layer"
        SG("Thanos Store Gateway"):::store
        OS[(Object Storage)]:::externalStorage
        CP("Thanos Compactor"):::compactor
        SG -->|"historical_data"| OS
        OS -->|"block_data"| SG
        CP -->|"downsample_write"| OS
    end

    %% Rules & Alerting
    subgraph "Alerting & Rules"
        RL("Thanos Rule"):::rule
        Q -->|"alert_queries"| RL
    end

    %% Additional Support
    subgraph "Additional Services"
        REP("Replication"):::additional
        CACHE("Caching Layers"):::additional
        DISC("Service Discovery"):::additional
        MET("Metrics/Tracing"):::additional
        UI("Thanos UI"):::ui
        Q -->|"replication_data"| REP
        Q -->|"cached_queries"| CACHE
        REP -->|"data_sync"| SG
        DISC -->|"supports_all"| Q
        DISC -->|"supports_all"| SC
        MET -->|"observability"| Q
        UI -->|"visualization"| Q
    end

    %% Deployment & DevOps
    subgraph "Deployment & DevOps"
        DEP("Docker & CI/CD"):::deployment
        DEP -->|"deploys"| Q
        DEP --> SC
        DEP --> SG
    end

    %% Click Events
    click Q "https://github.com/thanos-io/thanos/blob/main/cmd/thanos/query.go"
    click Q "https://github.com/thanos-io/thanos/tree/main/pkg/query"
    click QF "https://github.com/thanos-io/thanos/tree/main/pkg/queryfrontend"
    click SC "https://github.com/thanos-io/thanos/blob/main/cmd/thanos/sidecar.go"
    click SG "https://github.com/thanos-io/thanos/tree/main/pkg/store"
    click SG "https://github.com/thanos-io/thanos/tree/main/pkg/block"
    click SG "https://github.com/thanos-io/thanos/tree/main/pkg/block/indexheader"
    click CP "https://github.com/thanos-io/thanos/blob/main/cmd/thanos/compact.go"
    click CP "https://github.com/thanos-io/thanos/tree/main/pkg/compact"
    click RC "https://github.com/thanos-io/thanos/blob/main/cmd/thanos/receive.go"
    click RC "https://github.com/thanos-io/thanos/tree/main/pkg/receive"
    click RL "https://github.com/thanos-io/thanos/blob/main/cmd/thanos/rule.go"
    click RL "https://github.com/thanos-io/thanos/tree/main/pkg/rules"
    click REP "https://github.com/thanos-io/thanos/tree/main/pkg/replicate"
    click CACHE "https://github.com/thanos-io/thanos/tree/main/pkg/cache"
    click CACHE "https://github.com/thanos-io/thanos/tree/main/pkg/cacheutil"
    click DISC "https://github.com/thanos-io/thanos/tree/main/pkg/discovery"
    click MET "https://github.com/thanos-io/thanos/tree/main/pkg/tracing"
    click UI "https://github.com/thanos-io/thanos/tree/main/pkg/ui"
    click UI "https://github.com/thanos-io/thanos/tree/main/pkg/ui/react-app"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:2px;
    classDef externalStorage fill:#ff9,stroke:#333,stroke-width:2px;
    classDef sidecar fill:#b3ffb3,stroke:#333,stroke-width:2px;
    classDef query fill:#ccf,stroke:#333,stroke-width:2px;
    classDef store fill:#ffc,stroke:#333,stroke-width:2px;
    classDef compactor fill:#fbb,stroke:#333,stroke-width:2px;
    classDef receive fill:#ffdead,stroke:#333,stroke-width:2px;
    classDef rule fill:#e0b3ff,stroke:#333,stroke-width:2px;
    classDef additional fill:#d9edf7,stroke:#31708f,stroke-width:2px;
    classDef ui fill:#e2e2e2,stroke:#666,stroke-width:2px;
    classDef deployment fill:#dff0d8,stroke:#3c763d,stroke-width:2px;
```
