Skip to content

Commit

Permalink
feat: add prometheus metrics evaluation example (#677)
Browse files Browse the repository at this point in the history
Co-authored-by: Giovanni Liva <giovanni.liva@dynatrace.com>
Closes #65
  • Loading branch information
shardulsrivastava committed Feb 1, 2023
1 parent bc75f00 commit e5f644c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions functions-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ docker run -e SCRIPT=https://raw.githubusercontent.com/keptn/lifecycle-toolkit/m
docker run -e SCRIPT=https://raw.githubusercontent.com/keptn/lifecycle-toolkit/main/functions-runtime/samples/ts/slack.ts -e SECURE_DATA='{ "slack_hook":"hook/parts","text":"this is my test message" }' -it keptnsandbox/klc-runtime:${VERSION}
```

### Docker with function and external data - prometheus
```
docker run -e SCRIPT=https://raw.githubusercontent.com/keptn/lifecycle-toolkit/main/functions-runtime/samples/ts/prometheus.ts -e DATA='{ "url":"http://localhost:9090", "metrics": "up{service=\"kubernetes\"}", "expected_value": "1" }' -it ghcr.keptn.sh/keptn/functions-runtime:${VERSION}
```

<img referrerpolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=858843d8-8da2-4ce5-a325-e5321c770a78" />
63 changes: 63 additions & 0 deletions functions-runtime/samples/ts/prometheus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const text = Deno.env.get("DATA")!;
let data;
if (text && text != "") {
try {
data = JSON.parse(text);
if (!data.metrics || !data.url || !data.expected_value) {
console.log("Missing mandatory arguments.");
printUsage();
}
} catch {
console.error("Error Parsing Json => ", text);
Deno.exit(1);
}
} else {
console.log("Missing mandatory enviornment variable DATA.");
printUsage();
}

function printUsage() {
console.log(
'Expecting environment variable DATA in this format => DATA=\'{ "url":"<PROMETHEUS_URL>", "metrics": "<PROMETHEUS_QUERY>", "expected_value": "<EXPECTED_VALUE>" }\' ',
);
console.log(
'Example: export DATA=\'{ "url":"http://localhost:9090", "metrics": "up{service=\\"kubernetes\\"}", "expected_value": "1" }\' ',
);
Deno.exit(1);
}

function getPrometheusURL(url: string, metrics: string): string {
const dateTime = new Date().toISOString();
const hasPort = url.includes(":9090");
const 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;
}

const promtheusURL = getPrometheusURL(data.url, data.metrics);
console.log("Prometheus URL => " + promtheusURL);
let value;
try {
const jsonResponse = await fetch(promtheusURL);
const jsonData = await jsonResponse.json();
value = jsonData.data.result[0].value[1];
} catch (error) {
console.error("Could not fetch data from Prometheus.\n", error);
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);
}

0 comments on commit e5f644c

Please sign in to comment.