loki_send "nginx_access" {
	job = "nginx/access"
}
loki_send "nginx_error" {
	job = "nginx/error"
}
loki_send "nginx_stream" {
	job = "nginx/stream"
}
loki_send "nginx_stream_error" {
	job = "nginx/stream-error"
}
loki.source.file "nginx_access_logs" {
	targets    = [{
		__path__ = "/var/log/nginx/access.logfmt.log",
	}]
	forward_to   = [loki.process.nginx_access_logs.receiver]
}

loki.process "nginx_access_logs" {
	forward_to = [loki_send.nginx_access.receiver]
	stage.logfmt {
		mapping = {
			timestamp = "msec",
			http_host = "http_host",
			level = "status",
		}
	}
	stage.timestamp {
		source = "timestamp"
		format = "Unix"
	}
	stage.template {
		source = "level"
		template = "{{$v := atoi .Value}}{{if gt $v 499}}error{{else if gt $v 399}}warn{{ else if lt $v 200}}debug{{else}}info{{end}}"
	}
	stage.structured_metadata {
		values = {
			level = "",
			http_host = "",
		}
	}
}

loki.source.file "nginx_error_logs" {
	targets = [{
		__path__ = "/var/log/nginx/error.log",
	}]
	forward_to = [loki.process.error_logs.receiver]
}

loki.process "error_logs" {
	forward_to = [loki_send.nginx_error.receiver]
	stage.regex {
		expression = "^(?P<timestamp>\\S* \\S*) .+client: (?P<remote_ip>\\S+), (?:.*)"
	}

	stage.timestamp {
		source = "timestamp"
		format = "02/Jan/2006:15:04:05 +0000"
	}
}

loki.source.file "nginx_stream_logs" {
	targets = [{
		__path__ = "/var/log/nginx/stream.log",
	}]
	forward_to = [loki.process.stream_logs.receiver]
}

loki.process "stream_logs" {
	forward_to = [loki_send.nginx_stream.receiver]
	stage.regex {
		expression = "^(?P<remote_ip>[^ ]+) \\[(?P<timestamp>.*)\\] (?:.*)"
	}

	stage.timestamp {
		source = "timestamp"
		format = "02/Jan/2006:15:04:05 +0000"
	}
}

loki.source.file "nginx_stream_error_logs" {
	targets = [{
		__path__ = "/var/log/nginx/stream-error.log",
	}]
	forward_to = [loki.process.stream_error_logs.receiver]
}

loki.process "stream_error_logs" {
	forward_to = [loki_send.nginx_stream_error.receiver]
	stage.regex {
		expression = "^(?P<timestamp>\\S* \\S*) .+client: (?P<remote_ip>\\S+), (?:.*)"
	}

	stage.timestamp {
		source = "timestamp"
		format = "02/Jan/2006:15:04:05 +0000"
	}
}
