Last active
November 30, 2023 17:32
-
-
Save andresjesse/a8559cb7a8ab7a26c97fb37e62bb42ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a deno script. To run it: $ deno run --allow-all post-grafana.ts | |
import * as base64 from "https://deno.land/std@0.207.0/encoding/base64.ts"; | |
const lokiUrl = "https://your.loki.server/loki/api/v1/push"; | |
const lokiBasicAuth = { | |
username: "user", | |
password: "pass", | |
}; | |
const server = "frontend.staging"; //your server name | |
const debugLogFetches = false; //just to debug this script | |
async function log(message: string, level: "info" | "warn" | "error") { | |
const epoch_ns = Date.now() * 1000000; | |
const data = { | |
streams: [ | |
{ | |
stream: { | |
label: `${server}.${level}`, | |
}, | |
values: [[epoch_ns.toString(), message]], | |
}, | |
], | |
}; | |
try { | |
const response = await fetch(lokiUrl, { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: | |
"Basic " + | |
base64.encode(lokiBasicAuth.username + ":" + lokiBasicAuth.password), | |
}, | |
body: JSON.stringify(data), | |
}); | |
const result = await response.text(); | |
if (debugLogFetches) { | |
console.log(response); | |
console.log("Success:", result); | |
} | |
} catch (error) { | |
if (debugLogFetches) { | |
console.error("Error:", error); | |
} | |
} | |
} | |
export const Log = { | |
i: (message: string) => log(message, "info"), | |
w: (message: string) => log(message, "warn"), | |
e: (message: string) => log(message, "error"), | |
}; | |
Log.w(JSON.stringify("basic auth ok!")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment