logging {
  level  = "info"
  format = "logfmt"
}

// The internal Loki address is redacted in this documentation.
// Set the Loki endpoint from your deployment before using this file.
loki.write "central_loki" {
  endpoint {
    url = "http://<loki-host>:3100/loki/api/v1/push"
  }
}

// SECTION A - Docker logs (enabled).
// To disable Docker collection, comment all components in SECTION A
// and disable the Docker socket mount in docker-compose.yml.
discovery.docker "local_docker" {
  host             = "unix:///var/run/docker.sock"
  refresh_interval = "10s"
}

discovery.relabel "docker_selected" {
  targets = discovery.docker.local_docker.targets

  // Keep containers whose name or Compose service contains "demo-app".
  rule {
    source_labels = [
      "__meta_docker_container_name",
      "__meta_docker_container_label_com_docker_compose_service",
    ]
    separator = ";"
    action    = "keep"
    regex     = ".*(demo-app).*"
  }

  rule {
    source_labels = ["__meta_docker_container_name"]
    regex         = "/(.*)"
    target_label  = "container"
  }

  rule {
    source_labels = ["__meta_docker_container_label_com_docker_compose_service"]
    target_label  = "compose_service"
  }

  rule {
    target_label = "job"
    replacement  = "docker"
  }

  rule {
    target_label = "host"
    replacement  = "demo-node-01"
  }

  rule {
    target_label = "env"
    replacement  = "demo-prod"
  }

  rule {
    target_label = "source"
    replacement  = "docker"
  }
}

loki.source.docker "docker_logs" {
  host       = "unix:///var/run/docker.sock"
  targets    = discovery.relabel.docker_selected.output
  labels     = {}
  forward_to = [loki.write.central_loki.receiver]
}

// SECTION B - systemd journal logs (disabled).
// Enable the journal mounts in docker-compose.yml first.
// Uncomment one leading "//" from each line in the component block below.
// Keep the remaining inline comments. Set the service regex, host and env.
// Use relabel rules for OR matching across multiple systemd units.
// BEGIN SYSTEMD CONFIG
// loki.relabel "systemd_selected_services" {
//   // This component exports rules; the journal source forwards logs directly.
//   forward_to = []
//
//   rule {
//     source_labels = ["__journal__systemd_unit"]
//     action        = "keep"
//     regex         = "(demo-systemd-log[.]service|docker[.]service|ssh[.]service)"
//   }
//
//   rule {
//     source_labels = ["__journal__systemd_unit"]
//     target_label  = "unit"
//   }
//
//   rule {
//     source_labels = ["__journal__hostname"]
//     target_label  = "journal_host"
//   }
//
//   rule {
//     source_labels = ["__journal_priority_keyword"]
//     target_label  = "level"
//   }
//
//   rule {
//     target_label = "job"
//     replacement  = "systemd"
//   }
//
//   rule {
//     target_label = "env"
//     replacement  = "demo-prod"
//   }
//
//   rule {
//     target_label = "source"
//     replacement  = "systemd"
//   }
// }
//
// loki.source.journal "systemd_logs" {
//   // Use /run/log/journal instead when the host uses volatile journals only.
//   path          = "/var/log/journal"
//   forward_to    = [loki.write.central_loki.receiver]
//   relabel_rules = loki.relabel.systemd_selected_services.rules
//   max_age       = "2h"
//
//   labels = {
//     job    = "systemd",
//     host   = "demo-node-01",
//     env    = "demo-prod",
//     source = "systemd",
//   }
// }
// END SYSTEMD CONFIG

// SECTION C - PM2 file logs (disabled).
// Enable only the required PM2 mounts in docker-compose.yml.
// Uncomment one leading "//" from each line in the component block below.
// Keep only targets that match actual log paths. Set host and env.
// The optional /opt and /var/www targets retain a second comment prefix.
// BEGIN PM2 CONFIG
// loki.source.file "pm2_logs" {
//   targets = [
//     {
//       __path__ = "/host/root/.pm2/logs/*.log",
//       job      = "pm2",
//       host     = "demo-node-01",
//       env      = "demo-prod",
//       pm2_user = "root",
//       source   = "pm2",
//     },
//
//     // Match PM2 logs for users under /home.
//     // Narrow the mount and glob when only one app user needs collection.
//     {
//       __path__ = "/host/home/*/.pm2/logs/*.log",
//       job      = "pm2",
//       host     = "demo-node-01",
//       env      = "demo-prod",
//       pm2_user = "home-users",
//       source   = "pm2",
//     },
//
//     // Optional: enable with the /opt mount only when apps log there.
//     // {
//     //   __path__ = "/host/opt/**/logs/*.log",
//     //   job      = "pm2",
//     //   host     = "demo-node-01",
//     //   env      = "demo-prod",
//     //   pm2_user = "opt-apps",
//     //   source   = "pm2",
//     // },
//
//     // Optional: enable with the /var/www mount only when apps log there.
//     // {
//     //   __path__ = "/host/var/www/**/logs/*.log",
//     //   job      = "pm2",
//     //   host     = "demo-node-01",
//     //   env      = "demo-prod",
//     //   pm2_user = "www-apps",
//     //   source   = "pm2",
//     // },
//   ]
//
//   forward_to    = [loki.write.central_loki.receiver]
//   // Start at the end only when no saved position exists for the file.
//   tail_from_end = true
//
//   file_match {
//     enabled     = true
//     sync_period = "10s"
//   }
// }
// END PM2 CONFIG
