# Monitoring Defaults - Monitoring stack configuration
# Version: 1.0.0
#
# Scenario: Complete monitoring stack with Prometheus, Grafana, Loki, Jaeger
# Use case: Production observability, performance monitoring

{
  monitoring_defaults = {
    monitoring = {
      prometheus = {
        enabled = true,
        port = 9090,
        image = "prom/prometheus:latest",
        scrape_interval = "15s",
        evaluation_interval = "15s",
        retention = "30d",
        scrape_configs = [
          {
            job_name = "prometheus",
            static_configs = [{
              targets = ["localhost:9090"],
            }],
          },
          {
            job_name = "node-exporter",
            static_configs = [{
              targets = ["node-exporter:9100"],
            }],
          },
        ],
        storage_path = "/prometheus",
        external_labels = {
          cluster = "main",
          environment = "production",
        },
      },

      grafana = {
        enabled = true,
        port = 3000,
        image = "grafana/grafana:latest",
        admin_user = "admin",
        admin_password = "changeme",
        datasources = [
          {
            name = "Prometheus",
            type = 'prometheus,
            access = 'proxy,
            url = "http://prometheus:9090",
            is_default = true,
            basic_auth = false,
          },
          {
            name = "Loki",
            type = 'loki,
            access = 'proxy,
            url = "http://loki:3100",
            is_default = false,
            basic_auth = false,
          },
          {
            name = "Jaeger",
            type = 'jaeger,
            access = 'proxy,
            url = "http://jaeger:16686",
            is_default = false,
            basic_auth = false,
          },
        ],
        dashboards_path = "/etc/grafana/dashboards",
        plugins = [
          "grafana-piechart-panel",
          "grafana-clock-panel",
          "grafana-polystat-panel",
        ],
        environment = {
          GF_SECURITY_ADMIN_PASSWORD = "changeme",
          GF_INSTALL_PLUGINS = "grafana-piechart-panel,grafana-clock-panel",
          GF_SERVER_ROOT_URL = "http://localhost:3000",
        },
        anonymous_access = false,
        serve_from_sub_path = false,
      },

      loki = {
        enabled = true,
        port = 3100,
        image = "grafana/loki:latest",
        storage_path = "/loki",
        retention_period = "30d",
      },

      jaeger = {
        enabled = true,
        collector_port = 14268,
        query_port = 16686,
        image = "jaegertracing/all-in-one:latest",
        sampling_strategy = 'probabilistic,
        sampling_param = 0.1,
      },

      alerts_path = "./alerts",
      dashboard_refresh_interval = "30s",
    },

    # Monitoring services
    services = {
      prometheus = {
        type = 'backend,
        image = {
          name = "prom/prometheus",
          tag = "latest",
        },
        ports = [{
          container_port = 9090,
          host_port = 9090,
          protocol = 'tcp,
        }],
        environment = {},
        volumes = [
          "./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro",
          "./prometheus/alerts:/etc/prometheus/alerts:ro",
          "prometheus-data:/prometheus",
        ],
        command = [
          "--config.file=/etc/prometheus/prometheus.yml",
          "--storage.tsdb.path=/prometheus",
          "--web.console.libraries=/usr/share/prometheus/console_libraries",
          "--web.console.templates=/usr/share/prometheus/consoles",
          "--storage.tsdb.retention.time=30d",
        ],
        restart_policy = 'unless-stopped,
      },

      grafana = {
        type = 'backend,
        image = {
          name = "grafana/grafana",
          tag = "latest",
        },
        ports = [{
          container_port = 3000,
          host_port = 3000,
          protocol = 'tcp,
        }],
        environment = {
          GF_SECURITY_ADMIN_PASSWORD = "changeme",
          GF_INSTALL_PLUGINS = "grafana-piechart-panel,grafana-clock-panel",
        },
        volumes = [
          "./grafana/provisioning:/etc/grafana/provisioning:ro",
          "./grafana/dashboards:/etc/grafana/dashboards:ro",
          "grafana-data:/var/lib/grafana",
        ],
        restart_policy = 'unless-stopped,
        depends_on = ["prometheus"],
      },

      loki = {
        type = 'backend,
        image = {
          name = "grafana/loki",
          tag = "latest",
        },
        ports = [{
          container_port = 3100,
          host_port = 3100,
          protocol = 'tcp,
        }],
        environment = {},
        volumes = [
          "./loki/loki-config.yml:/etc/loki/local-config.yaml:ro",
          "loki-data:/loki",
        ],
        command = [
          "-config.file=/etc/loki/local-config.yaml",
        ],
        restart_policy = 'unless-stopped,
      },

      promtail = {
        type = 'backend,
        image = {
          name = "grafana/promtail",
          tag = "latest",
        },
        ports = [],
        environment = {},
        volumes = [
          "./promtail/promtail-config.yml:/etc/promtail/config.yml:ro",
          "/var/log:/var/log:ro",
          "/var/lib/docker/containers:/var/lib/docker/containers:ro",
        ],
        command = [
          "-config.file=/etc/promtail/config.yml",
        ],
        restart_policy = 'unless-stopped,
        depends_on = ["loki"],
      },

      jaeger = {
        type = 'backend,
        image = {
          name = "jaegertracing/all-in-one",
          tag = "latest",
        },
        ports = [
          {
            container_port = 5775,
            host_port = 5775,
            protocol = 'udp,
          },
          {
            container_port = 6831,
            host_port = 6831,
            protocol = 'udp,
          },
          {
            container_port = 6832,
            host_port = 6832,
            protocol = 'udp,
          },
          {
            container_port = 5778,
            host_port = 5778,
            protocol = 'tcp,
          },
          {
            container_port = 16686,
            host_port = 16686,
            protocol = 'tcp,
          },
          {
            container_port = 14268,
            host_port = 14268,
            protocol = 'tcp,
          },
        ],
        environment = {
          COLLECTOR_ZIPKIN_HTTP_PORT = "9411",
        },
        restart_policy = 'unless-stopped,
      },

      "node-exporter" = {
        type = 'backend,
        image = {
          name = "prom/node-exporter",
          tag = "latest",
        },
        ports = [{
          container_port = 9100,
          host_port = 9100,
          protocol = 'tcp,
        }],
        environment = {},
        volumes = [
          "/proc:/host/proc:ro",
          "/sys:/host/sys:ro",
          "/:/rootfs:ro",
        ],
        command = [
          "--path.procfs=/host/proc",
          "--path.sysfs=/host/sys",
          "--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)",
        ],
        restart_policy = 'unless-stopped,
      },
    },

    # Storage for monitoring
    storage = {
      docker_volumes = {
        "prometheus-data" = {
          name = "prometheus-data",
          driver = 'local,
          external = false,
        },
        "grafana-data" = {
          name = "grafana-data",
          driver = 'local,
          external = false,
        },
        "loki-data" = {
          name = "loki-data",
          driver = 'local,
          external = false,
        },
      },
    },

    # Network for monitoring
    network = {
      docker_networks = {
        monitoring = {
          name = "monitoring-network",
          driver = 'bridge,
          internal = false,
        },
      },
    },
  },
}
