Files
2025-12-08 10:34:54 +01:00

7.8 KiB

Alloy sample config

This configuration is far from finished, it serves as an example to get started with collecting logs and metrics.

Structure

The config consists of following components:

  • Write component - Where should the logs be sent. There can be multiple write components to send gathered logs and metrics to different endpoints on one system.
  • Metrics and Logs configurations - Collecting metrics and logs, to make logs and especially metrics meaningful in some cases, you need to define relabeling rules.
  • Labels - Labels are used for multi tennancy, I set them all to a neutral default, you can configure them as needed (Environments would make sense!)

Config

// ##########################
// #### Write Components ####
// ##########################
prometheus.remote_write "default" {
	endpoint {
		url = "https://mimir-test.psi.ch/api/v1/push"
		basic_auth {
			username = "lx-collector"
			password = "SUPER_SECRET_PLAINTEXT_PASSWORD"
		}
	}
}
loki.write "default" {
	endpoint {
		url = "https://loki-test.psi.ch/loki/api/v1/push"
		basic_auth {
			username = "lx-collector"
			password = "SUPER_SECRET_PLAINTEXT_PASSWORD"
		}
	}
}
otelcol.exporter.otlp "default" {
	client {
		endpoint = "tempo-test.psi.ch:4317"
		auth     = otelcol.auth.basic.default.handler
	}
}
otelcol.auth.basic "default" {
	username = "lx-collector"
	password = "SUPER_SECRET_PLAINTEXT_PASSWORD"
}

// #####################################
// #### Docker Metrics Configuration ####
// ######################################

// Host Cadvisor on the Docker socket to expose container metrics.
prometheus.exporter.cadvisor "docker" {
  docker_only = true
}

discovery.relabel "example" {
    targets = prometheus.exporter.cadvisor.docker.targets

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

    rule {
        target_label = "instance"
        replacement  = constants.hostname
    }
}

// Configure a prometheus.scrape component to collect cadvisor metrics.
prometheus.scrape "scraper" {
  targets    = discovery.relabel.example.output
  forward_to = [ prometheus.remote_write.default.receiver ]


  scrape_interval = "10s"
}

// ######################################
// #### Docker Logging Configuration ####
// ######################################

// Discover Docker containers and extract metadata.
discovery.docker "linux" {
  host = "unix:///var/run/docker.sock"
}

// Define a relabeling rule to create a service name from the container name.
discovery.relabel "logs_integrations_docker" {
      targets = []
  
      rule {
          source_labels = ["__meta_docker_container_name"]
          regex = "/(.*)"
          target_label = "container_name"
      }

     rule {
        target_label = "instance"
        replacement  = constants.hostname
    }

  }

// Configure a loki.source.docker component to collect logs from Docker containers.
loki.source.docker "default" {
  host       = "unix:///var/run/docker.sock"
  targets    = discovery.docker.linux.targets
  relabel_rules = discovery.relabel.logs_integrations_docker.rules
  forward_to = [loki.write.default.receiver]
}

// ###############################
// #### Metrics Configuration ####
// ###############################
// This block relabels metrics coming from node_exporter to add standard labels
discovery.relabel "integrations_node_exporter" {
  targets = prometheus.exporter.unix.integrations_node_exporter.targets

  rule {
    // Set the instance label to the hostname of the machine
    target_label = "instance"
    replacement  = constants.hostname
  }

  rule {
    // Set a standard job name for all node_exporter metrics
    target_label = "job"
    replacement = "integrations/node_exporter"
  }
}

// Configure the node_exporter integration to collect system metrics
prometheus.exporter.unix "integrations_node_exporter" {
  // Disable unnecessary collectors to reduce overhead
  disable_collectors = ["ipvs", "btrfs", "infiniband", "zfs"]
  enable_collectors = ["meminfo", "xfs"]

  filesystem {
    // Exclude filesystem types that aren't relevant for monitoring
    fs_types_exclude     = "^(autofs|binfmt_misc|bpf|cgroup2?|configfs|debugfs|devpts|devtmpfs|tmpfs|fusectl|hugetlbfs|iso9660|mqueue|nsfs|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|selinuxfs|squashfs|sysfs|tracefs)$"
    // Exclude mount points that aren't relevant for monitoring
    mount_points_exclude = "^/(dev|proc|run/credentials/.+|sys|var/lib/docker/.+)($|/)"
    // Timeout for filesystem operations
    mount_timeout        = "5s"
  }

  netclass {
    // Ignore virtual and container network interfaces
    ignored_devices = "^(veth.*|cali.*|[a-f0-9]{15})$"
  }

  netdev {
    // Exclude virtual and container network interfaces from device metrics
    device_exclude = "^(veth.*|cali.*|[a-f0-9]{15})$"
  }


}

// Define how to scrape metrics from the node_exporter
prometheus.scrape "integrations_node_exporter" {
scrape_interval = "15s"
  // Use the targets with labels from the discovery.relabel component
  targets    = discovery.relabel.integrations_node_exporter.output
  // Send the scraped metrics to the relabeling component
  forward_to = [prometheus.remote_write.default.receiver]
}

// ############################
// #### Logs Configuration ####
// ############################

// Collect logs from systemd journal for node_exporter integration
loki.source.journal "logs_integrations_integrations_node_exporter_journal_scrape" {
  // Only collect logs from the last 24 hours
  max_age       = "24h0m0s"
  // Apply relabeling rules to the logs
  relabel_rules = discovery.relabel.logs_integrations_integrations_node_exporter_journal_scrape.rules
  // Send logs to the local Loki instance
  forward_to    = [loki.write.default.receiver]
}

// Define which log files to collect for node_exporter
local.file_match "logs_integrations_integrations_node_exporter_direct_scrape" {
  path_targets = [{
    // Target localhost for log collection
    __address__ = "localhost",
    // Collect standard system logs
    __path__    = "/var/log/{syslog,messages,*.log}",
    // Add instance label with hostname
    instance    = constants.hostname,
    // Add job label for logs
    job         = "integrations/node_exporter",
  }]
}

// Define relabeling rules for systemd journal logs
discovery.relabel "logs_integrations_integrations_node_exporter_journal_scrape" {
  targets = []

  rule {
    // Extract systemd unit information into a label
    source_labels = ["__journal__systemd_unit"]
    target_label  = "unit"
  }

  rule {
    // Extract boot ID information into a label
    source_labels = ["__journal__boot_id"]
    target_label  = "boot_id"
  }

  rule {
    // Extract transport information into a label
    source_labels = ["__journal__transport"]
    target_label  = "transport"
  }

  rule {
    // Extract log priority into a level label
    source_labels = ["__journal_priority_keyword"]
    target_label  = "level"
  }
}

// Collect logs from files for node_exporter
loki.source.file "logs_integrations_integrations_node_exporter_direct_scrape" {
  // Use targets defined in local.file_match
  targets    = local.file_match.logs_integrations_integrations_node_exporter_direct_scrape.targets
  // Send logs to the local Loki instance
  forward_to = [loki.write.default.receiver]
}

// #######################
// #### Gitea Metrics ####
// #######################

// Enable live debugging features (empty config means use defaults)
livedebugging{}

discovery.relabel "metrics_integrations_integrations_gitea" {
	targets = [{
		__address__ = "gitea.psi.ch",
		__scheme__ = "https",
		__metrics_path__ = "/metrics",
	}]

	rule {
		target_label = "instance"
		replacement  = constants.hostname
	}
}

prometheus.scrape "metrics_integrations_integrations_gitea" {
	targets    = discovery.relabel.metrics_integrations_integrations_gitea.output
	forward_to = [prometheus.remote_write.default.receiver]
	job_name   = "integrations/gitea"
}