x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 50m
      max-file: '3'

x-clickhouse-defaults: &clickhouse-defaults
  <<: *defaults
  image: ${GLOBAL_REGISTRY:-}clickhouse/clickhouse-server:${SIGNOZ_CLICKHOUSE_VERSION:-25.5.6}
  depends_on:
    init-clickhouse:
      condition: service_completed_successfully
    zookeeper-1:
      condition: service_healthy
  healthcheck:
    test: [CMD, wget, --spider, -q, '0.0.0.0:8123/ping']
    interval: 30s
    timeout: 5s
    retries: 3
  ulimits:
    nproc: 65535
    nofile:
      soft: 262144
      hard: 262144

x-db-depend: &db-depend
  <<: *defaults
  depends_on:
    clickhouse:
      condition: service_healthy

services:
  # One-shot init: downloads histogramQuantile UDF binary into the shared volume
  init-clickhouse:
    image: ${GLOBAL_REGISTRY:-}clickhouse/clickhouse-server:${SIGNOZ_CLICKHOUSE_VERSION:-25.5.6}
    restart: on-failure
    logging:
      driver: json-file
      options:
        max-size: 10m
        max-file: '1'
    command:
      - bash
      - -c
      - |
        version="v0.0.1"
        node_os=$$(uname -s | tr '[:upper:]' '[:lower:]')
        node_arch=$$(uname -m | sed s/aarch64/arm64/ | sed s/x86_64/amd64/)
        echo "Fetching histogram-binary for $${node_os}/$${node_arch}"
        cd /tmp
        wget -O histogram-quantile.tar.gz "https://github.com/SigNoz/signoz/releases/download/histogram-quantile%2F$${version}/histogram-quantile_$${node_os}_$${node_arch}.tar.gz"
        tar -xvzf histogram-quantile.tar.gz
        mv histogram-quantile /var/lib/clickhouse/user_scripts/histogramQuantile
        echo "Done."
    volumes:
      - clickhouse_user_scripts:/var/lib/clickhouse/user_scripts/
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 256M

  # ZooKeeper for ClickHouse replication metadata
  zookeeper-1:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}signoz/zookeeper:${SIGNOZ_ZOOKEEPER_VERSION:-3.7.1}
    user: root
    environment:
      - TZ=${TZ:-UTC}
      - ZOO_SERVER_ID=1
      - ALLOW_ANONYMOUS_LOGIN=yes
      - ZOO_AUTOPURGE_INTERVAL=1
      - ZOO_ENABLE_PROMETHEUS_METRICS=yes
      - ZOO_PROMETHEUS_METRICS_PORT_NUMBER=9141
    volumes:
      - zookeeper_data:/bitnami/zookeeper
    healthcheck:
      test: [CMD-SHELL, 'curl -s -m 2 http://localhost:8080/commands/ruok | grep error | grep null']
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 30s
    deploy:
      resources:
        limits:
          cpus: ${SIGNOZ_ZOOKEEPER_CPU_LIMIT:-0.5}
          memory: ${SIGNOZ_ZOOKEEPER_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${SIGNOZ_ZOOKEEPER_CPU_RESERVATION:-0.1}
          memory: ${SIGNOZ_ZOOKEEPER_MEMORY_RESERVATION:-128M}

  # ClickHouse for storing traces, metrics and logs
  clickhouse:
    <<: *clickhouse-defaults
    environment:
      - TZ=${TZ:-UTC}
      - CLICKHOUSE_SKIP_USER_SETUP=1
    volumes:
      - clickhouse_data:/var/lib/clickhouse/
      - clickhouse_user_scripts:/var/lib/clickhouse/user_scripts/
      - ./clickhouse/config.xml:/etc/clickhouse-server/config.xml:ro
      - ./clickhouse/users.xml:/etc/clickhouse-server/users.xml:ro
      - ./clickhouse/custom-function.xml:/etc/clickhouse-server/custom-function.xml:ro
      - ./clickhouse/cluster.xml:/etc/clickhouse-server/config.d/cluster.xml:ro
    deploy:
      resources:
        limits:
          cpus: ${SIGNOZ_CLICKHOUSE_CPU_LIMIT:-2.0}
          memory: ${SIGNOZ_CLICKHOUSE_MEMORY_LIMIT:-4G}
        reservations:
          cpus: ${SIGNOZ_CLICKHOUSE_CPU_RESERVATION:-0.1}
          memory: ${SIGNOZ_CLICKHOUSE_MEMORY_RESERVATION:-1G}

  # One-shot migration: bootstraps and runs schema migrations
  signoz-telemetrystore-migrator:
    <<: *db-depend
    image: ${GLOBAL_REGISTRY:-}signoz/signoz-otel-collector:${SIGNOZ_OTEL_COLLECTOR_VERSION:-v0.144.4}
    restart: on-failure
    environment:
      - TZ=${TZ:-UTC}
      - SIGNOZ_OTEL_COLLECTOR_CLICKHOUSE_DSN=tcp://clickhouse:9000
      - SIGNOZ_OTEL_COLLECTOR_CLICKHOUSE_CLUSTER=cluster
      - SIGNOZ_OTEL_COLLECTOR_CLICKHOUSE_REPLICATION=true
      - SIGNOZ_OTEL_COLLECTOR_TIMEOUT=10m
    entrypoint: [/bin/sh]
    command:
      - -c
      - |
        /signoz-otel-collector migrate bootstrap &&
        /signoz-otel-collector migrate sync up &&
        /signoz-otel-collector migrate async up
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 512M

  # SigNoz all-in-one backend + frontend + alertmanager
  signoz:
    <<: *db-depend
    image: ${GLOBAL_REGISTRY:-}${SIGNOZ_IMAGE_NAME:-signoz/signoz}:${SIGNOZ_VERSION:-v0.125.1}
    ports:
      - '${SIGNOZ_PORT_OVERRIDE:-8080}:8080'
    volumes:
      - signoz_data:/var/lib/signoz/
    environment:
      - TZ=${TZ:-UTC}
      - SIGNOZ_ALERTMANAGER_PROVIDER=signoz
      - SIGNOZ_TELEMETRYSTORE_CLICKHOUSE_DSN=tcp://clickhouse:9000
      - SIGNOZ_SQLSTORE_SQLITE_PATH=/var/lib/signoz/signoz.db
      - SIGNOZ_TOKENIZER_JWT_SECRET=${SIGNOZ_JWT_SECRET:-please-change-this-secret}
    healthcheck:
      test: [CMD, wget, --spider, -q, 'localhost:8080/api/v1/health']
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 60s
    deploy:
      resources:
        limits:
          cpus: ${SIGNOZ_CPU_LIMIT:-1.0}
          memory: ${SIGNOZ_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${SIGNOZ_CPU_RESERVATION:-0.1}
          memory: ${SIGNOZ_MEMORY_RESERVATION:-256M}

  # OTel Collector for receiving telemetry data
  otel-collector:
    <<: *db-depend
    image: ${GLOBAL_REGISTRY:-}signoz/signoz-otel-collector:${SIGNOZ_OTEL_COLLECTOR_VERSION:-v0.144.4}
    entrypoint: [/bin/sh]
    command:
      - -c
      - |
        /signoz-otel-collector migrate sync check &&
        /signoz-otel-collector --config=/etc/otel-collector-config.yaml --manager-config=/etc/manager-config.yaml --copy-path=/var/tmp/collector-config.yaml
    volumes:
      - ./otel-collector-config.yaml:/etc/otel-collector-config.yaml:ro
      - ./signoz/otel-collector-opamp-config.yaml:/etc/manager-config.yaml:ro
    environment:
      - TZ=${TZ:-UTC}
      - OTEL_RESOURCE_ATTRIBUTES=${SIGNOZ_OTEL_RESOURCE_ATTRIBUTES:-host.name=signoz-host,os.type=linux}
      - LOW_CARDINAL_EXCEPTION_GROUPING=${SIGNOZ_LOW_CARDINAL_EXCEPTION_GROUPING:-false}
      - SIGNOZ_OTEL_COLLECTOR_CLICKHOUSE_DSN=tcp://clickhouse:9000
      - SIGNOZ_OTEL_COLLECTOR_CLICKHOUSE_CLUSTER=cluster
      - SIGNOZ_OTEL_COLLECTOR_CLICKHOUSE_REPLICATION=true
      - SIGNOZ_OTEL_COLLECTOR_TIMEOUT=10m
    ports:
      - '${SIGNOZ_OTEL_GRPC_PORT_OVERRIDE:-4317}:4317' # OTLP gRPC receiver
      - '${SIGNOZ_OTEL_HTTP_PORT_OVERRIDE:-4318}:4318' # OTLP HTTP receiver
    healthcheck:
      test: [CMD, bash, -c, echo > /dev/tcp/localhost/13133]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    deploy:
      resources:
        limits:
          cpus: ${SIGNOZ_OTEL_COLLECTOR_CPU_LIMIT:-1.0}
          memory: ${SIGNOZ_OTEL_COLLECTOR_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${SIGNOZ_OTEL_COLLECTOR_CPU_RESERVATION:-0.1}
          memory: ${SIGNOZ_OTEL_COLLECTOR_MEMORY_RESERVATION:-512M}

volumes:
  clickhouse_data:
  clickhouse_user_scripts:
  signoz_data:
  zookeeper_data:
