Need to pass creds in authorization header
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
export default {
|
||||
async fetch(request: Request) {
|
||||
console.log("This message should appear in the logs.");
|
||||
switch (request.method) {
|
||||
case "OPTIONS":
|
||||
return handleOPTIONS(request);
|
||||
@@ -9,6 +10,7 @@ export default {
|
||||
return handleGET(request);
|
||||
default:
|
||||
console.log(`Unsupported HTTP method ${request.method}`);
|
||||
throw new Error("Unsupported HTTP method");
|
||||
return new Response(null, { status: 405 });
|
||||
}
|
||||
},
|
||||
@@ -28,6 +30,7 @@ const handleOPTIONS = (request: Request) => {
|
||||
};
|
||||
|
||||
const isAllowedOrigin = (origin: string | null) => {
|
||||
console.log(origin);
|
||||
if (!origin) return false;
|
||||
try {
|
||||
const url = new URL(origin);
|
||||
|
||||
@@ -4,30 +4,31 @@
|
||||
* https://developers.cloudflare.com/workers/observability/logging/tail-workers/
|
||||
*/
|
||||
export default {
|
||||
async tail(events: TraceItem[], env: Env) {
|
||||
async tail(events: TraceItem[], env: Env, ctx: ExecutionContext) {
|
||||
console.log("Length of LOKI_PUSH_URL", env.LOKI_PUSH_URL.length);
|
||||
|
||||
// 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.
|
||||
await handleTail(events, env.LOKI_PUSH_URL);
|
||||
ctx.waitUntil(handleTail(events, env.LOKI_PUSH_URL, env.LOKI_AUTH));
|
||||
},
|
||||
} satisfies ExportedHandler<Env>;
|
||||
|
||||
interface Env {
|
||||
LOKI_PUSH_URL: string;
|
||||
LOKI_AUTH: string;
|
||||
}
|
||||
|
||||
const handleTail = async (events: TraceItem[], lokiPushURL: string) => {
|
||||
for (const event of events.filter(hasLogOrException)) {
|
||||
await pushLogLine(Date.now(), JSON.stringify(event), lokiPushURL);
|
||||
}
|
||||
};
|
||||
const handleTail = async (events: TraceItem[], lokiPushURL: string, lokiAuth: string) =>
|
||||
await pushLogLine(Date.now(), JSON.stringify(events), lokiPushURL, lokiAuth);
|
||||
|
||||
/**
|
||||
* Return true if any of the events in {@link events} has at least one log or
|
||||
* exception.
|
||||
*
|
||||
* These are the
|
||||
*/
|
||||
// const handleTail1 = async (events: TraceItem[], lokiPushURL: string) => {
|
||||
// for (const event of events.filter(hasLogOrException)) {
|
||||
// await pushLogLine(Date.now(), JSON.stringify(event), lokiPushURL);
|
||||
// }
|
||||
// };
|
||||
|
||||
/** Return true if the {@link event} has at least one log or exception. */
|
||||
const hasLogOrException = (event: TraceItem) =>
|
||||
event.logs.length ?? event.exceptions.length;
|
||||
|
||||
@@ -47,19 +48,39 @@ const hasLogOrException = (event: TraceItem) =>
|
||||
const pushLogLine = async (
|
||||
timestampMs: number,
|
||||
logLine: string,
|
||||
lokiPushURL: string
|
||||
) =>
|
||||
fetch(lokiPushURL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
lokiPushURL: string,
|
||||
lokiAuth: string,
|
||||
) => {
|
||||
console.log(
|
||||
"Pushing",
|
||||
JSON.stringify({
|
||||
streams: [
|
||||
{
|
||||
stream: { job: "worker" },
|
||||
values: [[`${timestampMs * 1e6}`, logLine]],
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
})
|
||||
);
|
||||
try {
|
||||
const res = await fetch(lokiPushURL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Basic ${lokiAuth}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
streams: [
|
||||
{
|
||||
stream: { job: "worker" },
|
||||
values: [[`${timestampMs * 1e6}`, logLine]],
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
console.log(res.status, await res.text()); //JSON.stringify(res));
|
||||
return res;
|
||||
} catch (e) {
|
||||
console.log("Failed", e);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user