# Arize Phoenix - AI Observability and Evaluation Platform
# https://docs.arize.com/phoenix

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

x-phoenix-common: &phoenix-common
  <<: *defaults
  image: ${GLOBAL_REGISTRY:-}arizephoenix/phoenix:${PHOENIX_VERSION:-15.5.0}
  ports:
    - '${PHOENIX_PORT_OVERRIDE:-6006}:6006' # UI and OTLP HTTP collector
    - '${PHOENIX_GRPC_PORT_OVERRIDE:-4317}:4317' # OTLP gRPC collector
    - '${PHOENIX_PROMETHEUS_PORT_OVERRIDE:-9090}:9090' # Prometheus metrics
  environment:
    - TZ=${TZ:-UTC}
    - PHOENIX_ENABLE_PROMETHEUS=${PHOENIX_ENABLE_PROMETHEUS:-false}
    - PHOENIX_SECRET=${PHOENIX_SECRET:-}
  healthcheck:
    test:
      - CMD
      - python3
      - -c
      - "import httpx;r=httpx.get('http://localhost:6006/healthz').raise_for_status()"

    interval: 30s
    timeout: 10s
    retries: 3
    start_period: 30s
  deploy:
    resources:
      limits:
        cpus: ${PHOENIX_CPU_LIMIT:-2.0}
        memory: ${PHOENIX_MEMORY_LIMIT:-2G}
      reservations:
        cpus: ${PHOENIX_CPU_RESERVATION:-0.1}
        memory: ${PHOENIX_MEMORY_RESERVATION:-512M}

services:
  # Default SQLite configuration
  phoenix:
    <<: *phoenix-common
    profiles:
      - sqlite
      - ${COMPOSE_PROFILES:-}
    environment:
      - TZ=${TZ:-UTC}
      - PHOENIX_ENABLE_PROMETHEUS=${PHOENIX_ENABLE_PROMETHEUS:-false}
      - PHOENIX_SECRET=${PHOENIX_SECRET:-NOT_SECURE_0fdf298eefb2ceef8ab3d7bd5319060e} # Default secret for SQLite, should be overridden in production
      - PHOENIX_WORKING_DIR=/data
    volumes:
      - phoenix_data:/data

  # PostgreSQL configuration
  phoenix-pg:
    <<: *phoenix-common
    profiles:
      - postgres
    environment:
      - TZ=${TZ:-UTC}
      - PHOENIX_ENABLE_PROMETHEUS=${PHOENIX_ENABLE_PROMETHEUS:-false}
      - PHOENIX_SECRET=${PHOENIX_SECRET:-NOT_SECURE_0fdf298eefb2ceef8ab3d7bd5319060e} # Default secret for PostgreSQL, should be overridden in production
      - PHOENIX_SQL_DATABASE_URL=postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@phoenix-db:5432/${POSTGRES_DB:-phoenix}
    depends_on:
      phoenix-db:
        condition: service_healthy

  phoenix-db:
    <<: *defaults
    profiles:
      - postgres
    image: ${GLOBAL_REGISTRY:-}postgres:${POSTGRES_VERSION:-17.2-alpine3.21}
    environment:
      - TZ=${TZ:-UTC}
      - POSTGRES_USER=${POSTGRES_USER:-postgres}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgres}
      - POSTGRES_DB=${POSTGRES_DB:-phoenix}
    volumes:
      - phoenix_db_data:/var/lib/postgresql/data
    healthcheck:
      test: [CMD-SHELL, 'pg_isready -U ${POSTGRES_USER:-postgres}']
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    deploy:
      resources:
        limits:
          cpus: ${PHOENIX_DB_CPU_LIMIT:-1.0}
          memory: ${PHOENIX_DB_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${PHOENIX_DB_CPU_RESERVATION:-0.1}
          memory: ${PHOENIX_DB_MEMORY_RESERVATION:-256M}

volumes:
  phoenix_data:
  phoenix_db_data:
