-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add prometheus metrics evaluation example
Signed-off-by: Shardul Srivastava <[email protected]>
- Loading branch information
1 parent
a3b0f7b
commit 378e5fc
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
let text = Deno.env.get("DATA"); | ||
let data; | ||
if (text != "") { | ||
data = JSON.parse(text); | ||
} | ||
|
||
function getPrometheusURL(url: string, metrics: string): string { | ||
let dateTime = new Date().toISOString(); | ||
let hasPort = url.includes(":9090"); | ||
let hasProtocol = url.includes("http://"); | ||
let queryURL: string = url; | ||
if (!hasPort) { | ||
queryURL = queryURL + ":9090"; | ||
} | ||
if (!hasProtocol) { | ||
queryURL = "http://" + queryURL; | ||
} | ||
|
||
return queryURL + "/api/v1/query?query=" + metrics + "&time=" + dateTime; | ||
} | ||
|
||
let promtheusURL = getPrometheusURL(data.url, data.metrics); | ||
console.log("Prometheus URL => " + promtheusURL); | ||
let value; | ||
try { | ||
let jsonResponse = await fetch(promtheusURL); | ||
let jsonData = await jsonResponse.json(); | ||
value = jsonData.data.result[0].value[1]; | ||
//status = jsonData.status | ||
} catch (error) { | ||
console.error("Could not fetch "); | ||
Deno.exit(1); | ||
} | ||
|
||
console.log("Expected Value => " + data.expected_value + ", Value => " + value); | ||
if (value == data.expected_value) { | ||
console.log("Evaluation Successful"); | ||
Deno.exit(0); | ||
} else { | ||
console.log("Evaluation Failed"); | ||
Deno.exit(1); | ||
} |