services:
  otel-conf:
    image: alpine:3.20
    command:
      - /bin/sh
      - -lc
      - |
        cat > /otel/config.yaml <<'YAML'
        receivers:
          otlp:
            protocols:
              grpc:
                endpoint: 0.0.0.0:4317
              http:
                endpoint: 0.0.0.0:4318

        processors:
          batch:

        exporters:
          clickhousetraces:
            datasource: tcp://clickhouse:9000/signoz_traces
            use_new_schema: true

          clickhousemetricswrite:
            endpoint: tcp://clickhouse:9000/signoz_metrics
            timeout: 15s
            resource_to_telemetry_conversion:
              enabled: true

          clickhouselogsexporter:
            dsn: tcp://clickhouse:9000/signoz_logs
            timeout: 10s
            use_new_schema: true

          metadataexporter:
            dsn: tcp://clickhouse:9000/signoz_metadata
            timeout: 10s

        service:
          pipelines:
            traces:
              receivers: [otlp]
              processors: [batch]
              exporters: [clickhousetraces, metadataexporter]
            metrics:
              receivers: [otlp]
              processors: [batch]
              exporters: [clickhousemetricswrite, metadataexporter]
            logs:
              receivers: [otlp]
              processors: [batch]
              exporters: [clickhouselogsexporter, metadataexporter]
        YAML
    volumes:
      - otel-config:/otel
    restart: "no"

  ch-conf:
    image: alpine:3.20
    command:
      - /bin/sh
      - -lc
      - |
        cat > /config/cluster.xml <<'XML'
        <?xml version="1.0"?>
        <clickhouse>
          <remote_servers>
            <cluster>
              <shard>
                <replica>
                  <host>clickhouse</host>
                  <port>9000</port>
                </replica>
              </shard>
            </cluster>
          </remote_servers>
          <zookeeper>
            <node index="1">
              <host>zookeeper-1</host>
              <port>2181</port>
            </node>
          </zookeeper>
          <distributed_ddl>
            <path>/clickhouse/task_queue/ddl</path>
          </distributed_ddl>
        </clickhouse>
        XML
    volumes:
      - ch-config:/config
    restart: "no"

  zookeeper-1:
    image: bitnami/zookeeper:3.9
    restart: unless-stopped
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
      - ZOO_4LW_COMMANDS_WHITELIST=ruok,stat,envi,conf,mntr
    healthcheck:
      test: ["CMD-SHELL", "echo ruok | nc -w 2 127.0.0.1 2181 | grep imok"]
      interval: 30s
      timeout: 5s
      retries: 3
    volumes:
      - zookeeper-data:/bitnami/zookeeper

  clickhouse:
    image: clickhouse/clickhouse-server:24.1.2-alpine
    restart: unless-stopped
    depends_on:
      ch-conf:
        condition: service_completed_successfully
    ulimits:
      nproc: 65535
      nofile:
        soft: 262144
        hard: 262144
    healthcheck:
      test: ["CMD","wget","--spider","-q","0.0.0.0:8123/ping"]
      interval: 30s
      timeout: 5s
      retries: 3
    volumes:
      - clickhouse-data:/var/lib/clickhouse
      - ch-config:/etc/clickhouse-server/config.d

  schema-migrator-sync:
    image: signoz/signoz-schema-migrator:v0.129.0
    command: ["sync","--dsn=tcp://clickhouse:9000","--cluster-name=cluster","--up="]
    depends_on:
      clickhouse:
        condition: service_healthy
    restart: "no"

  schema-migrator-async:
    image: signoz/signoz-schema-migrator:v0.129.0
    command: ["async","--dsn=tcp://clickhouse:9000","--cluster-name=cluster","--up="]
    depends_on:
      clickhouse:
        condition: service_healthy
      schema-migrator-sync:
        condition: service_completed_successfully
    restart: unless-stopped

  signoz:
    image: signoz/signoz:v0.92.1
    depends_on:
      clickhouse:
        condition: service_healthy
      schema-migrator-sync:
        condition: service_completed_successfully
    environment:
      - SIGNOZ_ALERTMANAGER_PROVIDER=signoz
      - STORAGE=clickhouse
      - SIGNOZ_TELEMETRYSTORE_CLICKHOUSE_DSN=tcp://clickhouse:9000
      - SIGNOZ_ANALYTICS_ENABLED=false
      - SIGNOZ_JWT_SECRET=${SIGNOZ_JWT_SECRET}
    ports:
      - 8080
    restart: unless-stopped
    volumes:
      - signoz-data:/var/lib/signoz

  signoz-otel-collector:
    image: signoz/signoz-otel-collector:v0.129.0
    depends_on:
      clickhouse:
        condition: service_healthy
      otel-conf:
        condition: service_completed_successfully
      schema-migrator-sync:
        condition: service_completed_successfully
    command: ["--config=/etc/otelcol/config.yaml"]
    volumes:
      - otel-config:/etc/otelcol:ro
    ports:
      - 4317
      - 4318
    restart: unless-stopped

volumes:
  clickhouse-data:
  ch-config:
  zookeeper-data:
  signoz-data:
  otel-config: