loki_send "node_exporter" {
	job = "integrations/node_exporter"
}

// 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_send.node_exporter.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}",
	}]
}

// 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_send.node_exporter.receiver]
}
