I use stage.regex
to read a log file. I want to apply the request template and push to Loki if the line matches the request-type regex, and apply the response template and push to Loki if it matches the response-type regex.
However, I can only apply both templates at the same time. Please help me separate them.
local.file_match "tmplogs" {
path_targets = [{"__path__" = "/var/log/alloy/mono-log.txt"}]
}
loki.source.file "local_files" {
targets = local.file_match.tmplogs.targets
forward_to = [loki.process.request.receiver, loki.process.response.receiver]
}
loki.process "request" {
stage.regex {
expression = `^\[(?P<time>.*?)\] .(?P<level>\w+): (?P<method>\w+) (?P<url>.*?) \[Authorization\]: (?P<authorization>.*?) \[\] {"uid":"(?P<uid>.*?)"}$`
}
stage.template {
source = "request"
template = `{"time": "{{.time}}", "level": "{{.level}}", "method": "{{.method}}", "url": "{{.url}}", "authorization": "{{.authorization}}", "uid": "{{.uid}}"}`
}
stage.output {
source = "request"
}
forward_to = [loki.write.local_loki.receiver]
}
loki.process "response" {
stage.regex {
expression = `^\[(?P<time>.*?)\] .(?P<level>\w+): (?P<status_code>\d+) (?P<status_text>\w+) (?P<response_body>\{[\s\S]*?\} }) \[\] {"uid":"(?P<uid>[\s\S]*?)"}$`
}
stage.template {
source = "response"
template = `{"time": "{{.time}}", "level": "{{.level}}", "status_code": "{{.status_code}}", "response_body": {{.response_body}}, "uid": "{{.uid}}"}`
}
stage.output {
source = "response"
}
forward_to = [loki.write.local_loki.receiver]
}
loki.write "local_loki" {
endpoint {
url = "http://gateway:3100/loki/api/v1/push"
}
}