# OpenLIT - AI Engineering Observability Platform
# OpenLIT provides OpenTelemetry-native LLM Observability, GPU Monitoring,
# Guardrails, Evaluations, Prompt Management, Vault, and Playground.
# Repository: https://github.com/openlit/openlit

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

services:
  clickhouse:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}clickhouse/clickhouse-server:${CLICKHOUSE_VERSION:-24.4.1}
    environment:
      - TZ=${TZ:-UTC}
      - CLICKHOUSE_PASSWORD=${OPENLIT_DB_PASSWORD:-OPENLIT}
      - CLICKHOUSE_USER=${OPENLIT_DB_USER:-default}
      - CLICKHOUSE_DATABASE=${OPENLIT_DB_NAME:-openlit}
      - CLICKHOUSE_ALWAYS_RUN_INITDB_SCRIPTS=true
    volumes:
      - clickhouse_data:/var/lib/clickhouse
      - ./assets/clickhouse-config.xml:/etc/clickhouse-server/config.d/custom-config.xml:ro
      - ./assets/clickhouse-init.sh:/docker-entrypoint-initdb.d/init.sh:ro
    ports:
      - '${CLICKHOUSE_HTTP_PORT_OVERRIDE:-8123}:8123'
      - '${CLICKHOUSE_NATIVE_PORT_OVERRIDE:-9000}:9000'
    healthcheck:
      test: [CMD-SHELL, "clickhouse-client --user=$${CLICKHOUSE_USER} --password=$${CLICKHOUSE_PASSWORD} --query='SELECT 1' || exit 1"]
      interval: 5s
      timeout: 3s
      retries: 10
      start_period: 100s
    deploy:
      resources:
        limits:
          cpus: ${CLICKHOUSE_CPU_LIMIT:-2.0}
          memory: ${CLICKHOUSE_MEMORY_LIMIT:-4G}
        reservations:
          cpus: ${CLICKHOUSE_CPU_RESERVATION:-0.5}
          memory: ${CLICKHOUSE_MEMORY_RESERVATION:-2G}

  openlit:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}ghcr.io/openlit/openlit:${OPENLIT_VERSION:-latest}
    environment:
      - TZ=${TZ:-UTC}
      - TELEMETRY_ENABLED=${TELEMETRY_ENABLED:-true}
      - INIT_DB_HOST=clickhouse
      - INIT_DB_PORT=8123
      - INIT_DB_DATABASE=${OPENLIT_DB_NAME:-openlit}
      - INIT_DB_USERNAME=${OPENLIT_DB_USER:-default}
      - INIT_DB_PASSWORD=${OPENLIT_DB_PASSWORD:-OPENLIT}
      - SQLITE_DATABASE_URL=file:/app/client/data/data.db
      - PORT=${OPENLIT_INTERNAL_PORT:-3000}
      - DOCKER_PORT=${OPENLIT_INTERNAL_PORT:-3000}
      # OAuth Configuration (Optional)
      - GOOGLE_CLIENT_ID=${GOOGLE_CLIENT_ID:-}
      - GOOGLE_CLIENT_SECRET=${GOOGLE_CLIENT_SECRET:-}
      - GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID:-}
      - GITHUB_CLIENT_SECRET=${GITHUB_CLIENT_SECRET:-}
      # OpAMP Server Configuration
      - OPAMP_ENVIRONMENT=${OPAMP_ENVIRONMENT:-production}
      - OPAMP_CERTS_DIR=/app/opamp/certs
      - OPAMP_TLS_INSECURE_SKIP_VERIFY=${OPAMP_TLS_INSECURE_SKIP_VERIFY:-false}
      - OPAMP_TLS_REQUIRE_CLIENT_CERT=${OPAMP_TLS_REQUIRE_CLIENT_CERT:-true}
      - OPAMP_TLS_MIN_VERSION=${OPAMP_TLS_MIN_VERSION:-1.2}
      - OPAMP_TLS_MAX_VERSION=${OPAMP_TLS_MAX_VERSION:-1.3}
      - OPAMP_LOG_LEVEL=${OPAMP_LOG_LEVEL:-info}
    ports:
      - '${OPENLIT_PORT_OVERRIDE:-3000}:${OPENLIT_INTERNAL_PORT:-3000}'
      - '${OPENLIT_OTLP_GRPC_PORT_OVERRIDE:-4317}:4317'
      - '${OPENLIT_OTLP_HTTP_PORT_OVERRIDE:-4318}:4318'
    depends_on:
      clickhouse:
        condition: service_healthy
    volumes:
      - openlit_data:/app/client/data
      - ./assets/otel-collector-config.yaml:/etc/otel/otel-collector-config.yaml:ro
    healthcheck:
      test: [CMD, wget, --quiet, --tries=1, --spider, 'http://localhost:${OPENLIT_INTERNAL_PORT:-3000}/health']
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    deploy:
      resources:
        limits:
          cpus: ${OPENLIT_CPU_LIMIT:-1.0}
          memory: ${OPENLIT_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${OPENLIT_CPU_RESERVATION:-0.25}
          memory: ${OPENLIT_MEMORY_RESERVATION:-512M}

volumes:
  clickhouse_data:
  openlit_data:
