Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prometheus metrics evaluation example #677

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 keptnsandbox/klc-runtime:${VERSION}
shardulsrivastava marked this conversation as resolved.
Show resolved Hide resolved
```

<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);
}