discovery.docker "docker" {
  host             = "unix:///var/run/docker.sock"
  refresh_interval = "30s"
  filter {
    name   = "label"
    values = ["com.docker.compose.project=akvorado"]
  }
}

prometheus.remote_write "default" {
  endpoint {
    url = "http://prometheus:9090/prometheus/api/v1/write"
  }
}

// Docker relabeling. We match entries with metrics.port as the port (it needs to
// be exposed) and use metrics.path if defined. The job name is the service name
// minus the numbered suffix.
discovery.relabel "prometheus" {
  targets = discovery.docker.docker.targets

  // Normalize the case where network is host.
  rule {
    source_labels = ["__meta_docker_container_label_metrics_port", "__meta_docker_network_name"]
    regex         = `(.+);host`
    target_label  = "__address__"
    replacement   = "host.docker.internal:$1"
  }
  rule {
    source_labels = ["__meta_docker_container_label_metrics_port", "__meta_docker_network_name"]
    regex         = `(.+);host`
    target_label  = "__meta_docker_port_private"
    replacement   = "$1"
  }
  // Then keep if we metrics.port matches private port
  rule {
    source_labels = ["__meta_docker_container_label_metrics_port"]
    regex         = `.+`
    action        = "keep"
  }
  rule {
    source_labels = ["__meta_docker_container_label_metrics_port"]
    target_label  = "__meta_docker_port_private"
    action        = "keepequal"
  }

  // Set job and instance name
  rule {
    source_labels = ["__meta_docker_container_label_com_docker_compose_service"]
    regex         = `(.+)(?:-\d+)?`
    target_label  = "job"
  }
  rule {
    source_labels = ["__address__"]
    regex         = `(.+):\d+`
    target_label  = "instance"
  }

  // Set metrics path from metrics.path label, default to /metrics
  rule {
    source_labels = ["__meta_docker_container_label_metrics_path"]
    regex         = `(.+)`
    target_label  = "__metrics_path__"
  }
  rule {
    source_labels = ["__metrics_path__"]
    regex         = ""
    target_label  = "__metrics_path__"
    replacement   = "/metrics"
  }
}
prometheus.scrape "docker" {
  targets         = discovery.relabel.prometheus.output
  forward_to      = [prometheus.remote_write.default.receiver]
  scrape_interval = "30s"
}

prometheus.exporter.redis "docker" {
  redis_addr = "redis:6379"
}
discovery.relabel "redis" {
  targets = prometheus.exporter.redis.docker.targets
  rule {
    target_label = "job"
    replacement  = "redis"
  }
}
prometheus.scrape "redis" {
  targets         = discovery.relabel.redis.output
  forward_to      = [prometheus.remote_write.default.receiver]
  scrape_interval = "30s"
}

prometheus.exporter.kafka "docker" {
  kafka_uris = ["kafka:9092"]
}
discovery.relabel "kafka" {
  targets = prometheus.exporter.kafka.docker.targets
  rule {
    target_label = "job"
    replacement  = "kafka"
  }
}
prometheus.scrape "kafka" {
  targets         = discovery.relabel.kafka.output
  forward_to      = [prometheus.remote_write.default.receiver]
  scrape_interval = "30s"
}
