DEV Community

Alex Spinov
Alex Spinov

Posted on

Thanos Has a Free API — Scale Prometheus to Multi-Cluster with Unlimited Retention

Thanos extends Prometheus with long-term storage, global querying, and high availability. It provides a single Prometheus-compatible API that queries across multiple Prometheus instances and object storage.

Free, open source, CNCF incubating project. Used by companies like Adobe, eBay, and Improbable.

Why Use Thanos?

  • Unlimited retention — store metrics in S3/GCS for years, not weeks
  • Global query — single API queries across all Prometheus instances
  • High availability — deduplicate metrics from HA Prometheus pairs
  • Downsampling — automatic 5m and 1h resolution for old data
  • 100% Prometheus-compatible — same PromQL, same API

Quick Setup

1. Install Thanos Sidecar

# Add sidecar to existing Prometheus
helm upgrade prometheus prometheus-community/prometheus \
  --set server.extraFlags[0]='--storage.tsdb.min-block-duration=2h' \
  --set server.extraFlags[1]='--storage.tsdb.max-block-duration=2h'

# Deploy Thanos sidecar
kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: thanos-sidecar
spec:
  template:
    spec:
      containers:
      - name: thanos-sidecar
        image: quay.io/thanos/thanos:v0.35.0
        args:
        - sidecar
        - --tsdb.path=/prometheus
        - --prometheus.url=http://localhost:9090
        - --objstore.config-file=/etc/thanos/objstore.yml
EOF
Enter fullscreen mode Exit fullscreen mode

2. Query via Thanos API

THANOS_URL="http://thanos-query:9090"

# Instant query (same as Prometheus API)
curl -s "$THANOS_URL/api/v1/query?query=up" | jq '.data.result[] | {metric: .metric.job, value: .value[1]}'

# Range query
curl -s "$THANOS_URL/api/v1/query_range" \
  --data-urlencode 'query=rate(http_requests_total[5m])' \
  --data-urlencode 'start=2026-03-28T00:00:00Z' \
  --data-urlencode 'end=2026-03-28T12:00:00Z' \
  --data-urlencode 'step=60s' | jq '.data.result | length'

# List available labels
curl -s "$THANOS_URL/api/v1/labels" | jq '.data'

# Stores status (which Prometheus instances are connected)
curl -s "$THANOS_URL/api/v1/stores" | jq '.data.store[] | {name: .name, type: .storeType, minTime: .minTime, maxTime: .maxTime}'
Enter fullscreen mode Exit fullscreen mode

3. Set Up Store Gateway

# Query historical data from object storage
kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: thanos-store
spec:
  template:
    spec:
      containers:
      - name: thanos-store
        image: quay.io/thanos/thanos:v0.35.0
        args:
        - store
        - --data-dir=/var/thanos/store
        - --objstore.config-file=/etc/thanos/objstore.yml
EOF
Enter fullscreen mode Exit fullscreen mode

4. Compactor (Downsampling)

# Auto-downsample old data
thanos compact \
  --data-dir=/var/thanos/compact \
  --objstore.config-file=objstore.yml \
  --retention.resolution-raw=30d \
  --retention.resolution-5m=180d \
  --retention.resolution-1h=365d
Enter fullscreen mode Exit fullscreen mode

Python Example

import requests
from datetime import datetime, timedelta

THANOS = "http://thanos-query:9090"

# Instant query
result = requests.get(f"{THANOS}/api/v1/query",
    params={"query": 'sum by (job) (up)'}).json()

for r in result["data"]["result"]:
    print(f"Job: {r['metric']['job']} | Up: {r['value'][1]}")

# Range query — last 24h
end = datetime.utcnow()
start = end - timedelta(hours=24)

range_result = requests.get(f"{THANOS}/api/v1/query_range", params={
    "query": 'rate(http_requests_total[5m])',
    "start": start.isoformat() + "Z",
    "end": end.isoformat() + "Z",
    "step": "300s"
}).json()

print(f"Series returned: {len(range_result['data']['result'])}")
Enter fullscreen mode Exit fullscreen mode

Architecture Components

Component Description
Sidecar Uploads Prometheus data to object storage
Query Unified PromQL endpoint across all stores
Store Gateway Serves historical data from object storage
Compactor Downsamples and compacts old data
Ruler Evaluates recording and alerting rules
Receiver Remote write endpoint for HA ingestion

Need custom data extraction or scraping solution? I build production-grade scrapers for any website. Email: Spinov001@gmail.com | My Apify Actors

Top comments (0)