The protocol

This commit is contained in:
Manav Rathi
2024-06-15 18:27:44 +05:30
parent 5e080a90e3
commit 8fe2b9cb27
2 changed files with 41 additions and 6 deletions

View File

@@ -4,16 +4,51 @@
* https://developers.cloudflare.com/workers/observability/logging/tail-workers/
*/
export default {
async tail(events: TailItems[], env: Env) {
async tail(events: TraceItem[], env: Env) {
// If the tail worker itself throws an exception (it shouldn't, unless
// Loki is down), we don't catch it so that it counts as an "error" in
// the worker stats.
return handleTail(events, env.LOKI_URL);
return handleTail(events, env.LOKI_PUSH_URL);
},
} satisfies ExportedHander<Env>;
} satisfies ExportedHandler<Env>;
interface Env {
LOKI_URL: string;
LOKI_PUSH_URL: string;
}
const handleTail = (events: TailItems[], lokiURL: string) => {};
const handleTail = async (events: TraceItem[], lokiPushURL: string) => {};
/**
* Send a log entry to (Grafana) Loki
*
* 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 logLine The message to log.
*
* @param lokiPushURL The URL of the Loki instance to push logs to. The
* credentials are part of the URL.
*/
const pushLogLine = async (
tsNano: number,
logLine: string,
lokiPushURL: string
) =>
fetch(lokiPushURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
streams: [
{
stream: {
job: "cf-worker",
},
values: [[`${tsNano}`, logLine]],
},
],
}),
});

View File

@@ -4,4 +4,4 @@ compatibility_date = "2024-06-14"
[vars]
# Added as a secret via the Cloudflare dashboard
# LOKI_URL = "https://discord.com/api/webhooks/{webhook.id}/{webhook.token}"
# LOKI_PUSH_URL = "https://{loki_base_url}>/loki/api/v1/push"