Skip to content

Commit

Permalink
improve(logs): format logs in logfmt to make querying easier in Loki
Browse files Browse the repository at this point in the history
  • Loading branch information
sdnts committed Sep 15, 2023
1 parent c7276e8 commit 6b42f04
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
23 changes: 21 additions & 2 deletions src/endpoints/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,27 @@ export const endpoint: Endpoint<typeof schema> = {
if (l.timestamp.p === "ms")
l.timestamp.v = `${l.timestamp.v}000000`;

if (!l.kv && !params.data.kv) return [l.timestamp.v, l.message];
return [l.timestamp.v, l.message, { ...params.data.kv, ...l.kv }];
// Loki supports structured metadata that is supposed to be treated
// as labels, but it is experimental and doesn't seem to be working.
// Specifically, I don't see these structured metadata labels and
// cannot query using them.
// So instead I'll send a logfmt-style string as the message instead,
// which will let me use the `logfmt` stream processor.
// JSON is another option but stringified JSON be ugly.
// Reference: https://grafana.com/docs/loki/latest/get-started/labels/structured-metadata/
// TODO: We should just use structured metadata once it lands as stable

const log = Object.entries({
...params.data.kv,
...l.kv,
msg: l.message,
})
.map(([k, v]) => {
if (typeof v === "string") return `${k}="${v}"`;
return `${k}=${v}`;
})
.join(" ");
return [l.timestamp.v, log];
}),
};
}),
Expand Down
30 changes: 12 additions & 18 deletions tests/ingest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ test.describe("logs", async () => {
origin: "https://blob.city",
level: "info",
},
values: [["001000000", "Incoming request"]],
values: [["001000000", 'msg="Incoming request"']],
},
],
}),
Expand Down Expand Up @@ -326,7 +326,7 @@ test.describe("logs", async () => {
origin: "https://blob.city",
level: "fatal",
},
values: [["001000000", "Incoming request"]],
values: [["001000000", 'msg="Incoming request"']],
},
],
}),
Expand Down Expand Up @@ -363,7 +363,7 @@ test.describe("logs", async () => {
origin: "https://blob.city",
level: "info",
},
values: [["001000000", "Incoming request"]],
values: [["001000000", 'msg="Incoming request"']],
},
],
}),
Expand Down Expand Up @@ -400,7 +400,7 @@ test.describe("logs", async () => {
origin: "https://blob.city",
level: "info",
},
values: [["001", "Incoming request"]],
values: [["001", 'msg="Incoming request"']],
},
],
}),
Expand Down Expand Up @@ -437,7 +437,7 @@ test.describe("logs", async () => {
origin: "https://blob.city",
level: "info",
},
values: [["001", "Incoming request"]],
values: [["001", 'msg="Incoming request"']],
},
],
}),
Expand Down Expand Up @@ -475,7 +475,7 @@ test.describe("logs", async () => {
origin: "https://blob.city",
level: "info",
},
values: [["001", "Incoming request", { method: "GET" }]],
values: [["001", 'method="GET" msg="Incoming request"']],
},
],
}),
Expand Down Expand Up @@ -513,7 +513,7 @@ test.describe("logs", async () => {
origin: "https://blob.city",
level: "info",
},
values: [["001", "Incoming request", { rayId: "1234" }]],
values: [["001", 'rayId="1234" msg="Incoming request"']],
},
],
}),
Expand Down Expand Up @@ -553,7 +553,7 @@ test.describe("logs", async () => {
level: "info",
},
values: [
["001", "Incoming request", { rayId: "1234", method: "GET" }],
["001", 'rayId="1234" method="GET" msg="Incoming request"'],
],
},
],
Expand Down Expand Up @@ -611,14 +611,9 @@ test.describe("logs", async () => {
values: [
[
"001000000",
"Incoming request",
{
rayId: "abcd",
method: "GET",
path: "/tunnel",
},
'rayId="abcd" method="GET" path="/tunnel" msg="Incoming request"',
],
["004000000", "Response", { rayId: "abcd", status: 200 }],
["004000000", 'rayId="abcd" status=200 msg="Response"'],
],
},
{
Expand All @@ -630,8 +625,7 @@ test.describe("logs", async () => {
values: [
[
"002000000",
"Forwarding to DO",
{ rayId: "abcd", tunnelId: "1234" },
'rayId="abcd" tunnelId="1234" msg="Forwarding to DO"',
],
],
},
Expand All @@ -641,7 +635,7 @@ test.describe("logs", async () => {
origin: "https://blob.city",
level: "trace",
},
values: [["003", "Creating WebSocketPair", { rayId: "abcd" }]],
values: [["003", 'rayId="abcd" msg="Creating WebSocketPair"']],
},
],
}),
Expand Down

0 comments on commit 6b42f04

Please sign in to comment.