Skip to content

Instantly share code, notes, and snippets.

@tpaschalis
Created January 18, 2023 15:46
Show Gist options
  • Save tpaschalis/3cdcec734cf1cdf8f7c1893e699dd1c8 to your computer and use it in GitHub Desktop.
Save tpaschalis/3cdcec734cf1cdf8f7c1893e699dd1c8 to your computer and use it in GitHub Desktop.
logging {
level = "debug"
format = "logfmt"
// Soon™
// write_to = loki.write.grafanacloud.receiver
}
/* CRI-formatted log lines.
I'm cheating and just redirecting from stdin for the demo :sweat_smile: */
discovery.file "tmp" {
path_targets = [{"__path__" = "/tmp/mylogfiles/**/*.log"}]
}
loki.source.file "tmpfiles" {
targets = discovery.file.tmp.targets
forward_to = [loki.process.crio.receiver]
}
loki.process "crio" {
stage {
cri { }
}
/* 2023-01-01T00:02:17.9143717Z stdout F level=WARN duration=125 message="something happened" foo=bar
extracts:
timestamp: 2023-01-01T00:02:17.9143717Z
stream: stdout
flags: F
content: level=WARN duration=125 message="something happened" foo=bar
*/
// syntax will change to stage.cri, stage.logfmt etc
stage {
logfmt {
source = "content"
mapping = {lvl = "level", msg = "message"}
}
}
/* The extracted map now also contains:
lvl: "WARN"
msg: "something happened"
*/
stage {
labels {
values = {loglevel = "lvl"}
}
}
stage {
static_labels {
values = {hostname = env("HOSTNAME"), source = "logfile"}
}
}
stage {
output {
source = "msg"
}
}
forward_to = [loki.write.grafanacloud.receiver, loki.stdout.debug.receiver]
}
/* Syslog messages and OTel logs */
loki.source.syslog "tcp" {
listener {
address = "127.0.0.1:51893"
protocol = "tcp"
labels = {source = "syslog", protocol = "tcp"}
}
forward_to = [loki.relabel.add_hostname.receiver]
}
otelcol.receiver.otlp "default" {
http { }
output {
logs = [otelcol.exporter.loki.default.input]
}
}
otelcol.exporter.loki "default" {
forward_to = [loki.relabel.add_hostname.receiver]
}
loki.relabel "add_hostname" {
rule {
action = "replace"
target_label = "hostname"
replacement = env("HOSTNAME")
}
forward_to = [loki.write.grafanacloud.receiver, loki.stdout.debug.receiver]
}
// Not merged component, only for demo purposes
loki.stdout "debug" { }
/* Send to Loki */
loki.write "grafanacloud" {
endpoint {
url = "<loki_url>"
http_client_config {
basic_auth {
username = "<username>"
password_file = "/tmp/token.txt"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment