Step-by-Step Working Solution for Filtering GitLab Logs in Loki Alloy

Step 1: Open Your Loki Alloy Configuration File

Command: sudo nano /etc/alloy/alloy-config.hcl
Step 2: Replace with the Correct Configuration

loki.source.file “logs_gitlab_01” {
targets = [
{“path” = “/var/log/gitlab/gitlab-rails/production_json.log”},
]
forward_to = [loki.process.logs_gitlab_01.receiver]
}

loki.process “logs_gitlab_01” {
stage.json {
expressions = {
extracted_status = “status”,
}
}

stage.labels {
values = {
“status” = “extracted_status”,
}
}
stage.match {
selector = “{status=~"2[0-9]{2}"}”

stage.labels {
  values = {
    "level" = "info",
  }
}

}
forward_to = [loki.process.logs_gitlab_00.receiver]
}
Step 3: Save and Exit
After pasting the configuration, do the following:

  1. Press CTRL + X (exit editor).
  2. Press Y (to save changes).
  3. Press Enter (confirm the filename).

Step 4: Restart Loki Alloy

Command: sudo systemctl restart alloy

Step 5: Verify If Alloy Is Running Properly

Command: sudo systemctl status alloy

Step 6: Verify That Filtering Works in Loki

loki query ‘{status=~“2[0-9]{2}”}’