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

services:
  logstash:
    <<: *defaults
    image: docker.elastic.co/logstash/logstash:${LOGSTASH_VERSION:-8.16.1}
    ports:
      - '${LOGSTASH_BEATS_PORT_OVERRIDE:-5044}:5044'
      - '${LOGSTASH_TCP_PORT_OVERRIDE:-5000}:5000/tcp'
      - '${LOGSTASH_UDP_PORT_OVERRIDE:-5000}:5000/udp'
      - '${LOGSTASH_HTTP_PORT_OVERRIDE:-9600}:9600'
    volumes:
      - logstash_data:/usr/share/logstash/data
      - logstash_logs:/usr/share/logstash/logs
      - ./pipeline:/usr/share/logstash/pipeline:ro

      # Custom configuration
      # - ./logstash.yml:/usr/share/logstash/config/logstash.yml:ro
      # - ./pipelines.yml:/usr/share/logstash/config/pipelines.yml:ro
    environment:
      - TZ=${TZ:-UTC}
      - XPACK_MONITORING_ENABLED=${LOGSTASH_MONITORING_ENABLED:-false}
      - XPACK_MONITORING_ELASTICSEARCH_HOSTS=${ELASTICSEARCH_HOSTS:-http://elasticsearch:9200}
      - ELASTICSEARCH_HOSTS=${ELASTICSEARCH_HOSTS:-http://elasticsearch:9200}
      - ELASTICSEARCH_USERNAME=${ELASTICSEARCH_USERNAME:-}
      - ELASTICSEARCH_PASSWORD=${ELASTICSEARCH_PASSWORD:-}
      - LS_JAVA_OPTS=${LS_JAVA_OPTS:--Xmx1g -Xms1g}
      - PIPELINE_WORKERS=${LOGSTASH_PIPELINE_WORKERS:-2}
      - PIPELINE_BATCH_SIZE=${LOGSTASH_PIPELINE_BATCH_SIZE:-125}
      - PIPELINE_BATCH_DELAY=${LOGSTASH_PIPELINE_BATCH_DELAY:-50}
      - LOG_LEVEL=${LOGSTASH_LOG_LEVEL:-info}
    deploy:
      resources:
        limits:
          cpus: ${LOGSTASH_CPU_LIMIT:-1.50}
          memory: ${LOGSTASH_MEMORY_LIMIT:-2G}
        reservations:
          cpus: ${LOGSTASH_CPU_RESERVATION:-0.50}
          memory: ${LOGSTASH_MEMORY_RESERVATION:-1G}
    healthcheck:
      test: [CMD-SHELL, 'curl -f http://localhost:9600/_node/stats || exit 1']
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s

volumes:
  logstash_data:
  logstash_logs:
