Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fill in

Manav Rathia1059c543be7
diff --git a/infra/workers/README.md b/infra/workers/README.md
index c4615419d7..54cfc21be8 100644
--- a/infra/workers/README.md
+++ b/infra/workers/README.md
@@ -37,4 +37,4 @@ npm create cloudflare@2 existing-worker-name -- --type pre-existing --existing-s
## Logging
Attach our tail worker (called `tail`) to your worker. Then, any `console.log`
-statements called by your worker will be forwarded to Grafana.
+(etc) statements called by your worker will be forwarded to Grafana.
diff --git a/infra/workers/tail/src/index.ts b/infra/workers/tail/src/index.ts
index de3c019fe2..013219cf00 100644
--- a/infra/workers/tail/src/index.ts
+++ b/infra/workers/tail/src/index.ts
@@ -1,5 +1,5 @@
/**
- * A tail worker that forwards all `console.log`s to Loki.
+ * A tail worker that forwards all `console.log` (and siblings) to Loki.
*
* https://developers.cloudflare.com/workers/observability/logging/tail-workers/
*/
@@ -16,7 +16,33 @@ interface Env {
LOKI_PUSH_URL: string;
}
-const handleTail = async (events: TraceItem[], lokiPushURL: string) => {};
+const handleTail = async (events: TraceItem[], lokiPushURL: string) => {
+ for (const event of events) {
+ for (const log of event.logs) {
+ pushLogLine(log.timestamp, logLineForLog(log), lokiPushURL);
+ }
+ for (const e of event.exceptions) {
+ pushLogLine(e.timestamp, logLineForException(e), lokiPushURL);
+ }
+ }
+};
+
+const logLineForLog = ({ level, message }: TraceLog) =>
+ // https://developers.cloudflare.com/workers/runtime-apis/handlers/tail/#taillog
+ //
+ // - level: A string indicating the console function that was called. One
+ // of: "debug", "info", "log", "warn", "error".
+ //
+ // - message: The array of parameters passed to the console function.
+ `[${level}] ${(Array.isArray(message) ? message : [message]).join(" ")}`;
+
+const logLineForException = ({ name, message }: TraceException) =>
+ // https://developers.cloudflare.com/workers/runtime-apis/handlers/tail/#tailexception
+ //
+ // - name: The error type (e.g. "Error", "TypeError")
+ //
+ // - message: The error description.
+ `${name}: ${message}`;
/**
* Send a log entry to (Grafana) Loki
@@ -24,7 +50,7 @@ const handleTail = async (events: TraceItem[], lokiPushURL: string) => {};
* For more details about the protocol, see
* https://grafana.com/docs/loki/latest/reference/loki-http-api/#ingest-logs
*
- * @param tsNano Unix epoch in nanoseconds when the event occurred.
+ * @param tsNano Unix epoch (in milliseconds) when the event occurred.
*
* @param logLine The message to log.
*
@@ -32,7 +58,7 @@ const handleTail = async (events: TraceItem[], lokiPushURL: string) => {};
* credentials are part of the URL.
*/
const pushLogLine = async (
- tsNano: number,
+ timestampMs: number,
logLine: string,
lokiPushURL: string
) =>
@@ -44,10 +70,8 @@ const pushLogLine = async (
body: JSON.stringify({
streams: [
{
- stream: {
- job: "cf-worker",
- },
- values: [[`${tsNano}`, logLine]],
+ stream: { job: "cf-worker" },
+ values: [[`${timestampMs * 1e6}`, logLine]],
},
],
}),