Skip to content

Commit

Permalink
Merge pull request #163 from NipunaMadhushan/fix-codecov
Browse files Browse the repository at this point in the history
Fix metric exposition format
  • Loading branch information
sm1990 authored Oct 24, 2024
2 parents a8b0df0 + 65833b3 commit 7d0b8e8
Showing 1 changed file with 53 additions and 13 deletions.
66 changes: 53 additions & 13 deletions prometheus-extension-ballerina/metric_reporter.bal
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,75 @@ isolated function startReporter(string host, int port) returns error? {
# and reformats based on the expected format by prometheus server.
resource function get metrics(http:Caller caller) {
observe:Metric?[] metrics = observe:getAllMetrics();
string[] payload = [];
map<string[]> payload = {};
foreach var m in metrics {
observe:Metric metric = <observe:Metric> m;
string qualifiedMetricName = getEscapedName(metric.name);
string metricReportName = getMetricName(qualifiedMetricName, "value");
payload.push(generateMetricHelp(metricReportName, metric.desc));
payload.push(generateMetricInfo(metricReportName, metric.metricType));
payload.push(generateMetric(metricReportName, metric.tags, metric.value));
if !payload.hasKey(metricReportName) {
payload[metricReportName] = [];
(<string[]> payload[metricReportName]).push(generateMetricHelp(metricReportName, metric.desc));
(<string[]> payload[metricReportName]).push(generateMetricInfo(metricReportName, metric.metricType));
}
(<string[]> payload[metricReportName]).push(generateMetric(metricReportName, metric.tags, metric.value));
if ((str:toLowerAscii(metric.metricType) == METRIC_TYPE_GAUGE) && !(metric.summary is ())){
map<string> tags = metric.tags;
observe:Snapshot[] summaries = <observe:Snapshot[]> metric.summary;
foreach var aSnapshot in summaries {
tags[EXPIRY_TAG] = aSnapshot.timeWindow.toString();
payload.push(generateMetricHelp(qualifiedMetricName, string `A Summary of ${
qualifiedMetricName} for window of ${aSnapshot.timeWindow.toString()}`));
payload.push(generateMetricInfo(qualifiedMetricName, METRIC_TYPE_SUMMARY));
payload.push(generateMetric(getMetricName(qualifiedMetricName, "mean"), tags, aSnapshot.mean));
payload.push(generateMetric(getMetricName(qualifiedMetricName, "max"), tags, aSnapshot.max));
payload.push(generateMetric(getMetricName(qualifiedMetricName, "min"), tags, aSnapshot.min));
payload.push(generateMetric(getMetricName(qualifiedMetricName, "stdDev"), tags, aSnapshot.stdDev));
if !payload.hasKey(qualifiedMetricName) {
payload[qualifiedMetricName] = [];
(<string[]> payload[qualifiedMetricName]).push(generateMetricHelp(qualifiedMetricName, string `A summary of ${
qualifiedMetricName}`));
(<string[]> payload[qualifiedMetricName]).push(generateMetricInfo(qualifiedMetricName, METRIC_TYPE_SUMMARY));
}

string meanMetricName = getMetricName(qualifiedMetricName, "mean");
if !payload.hasKey(meanMetricName) {
payload[meanMetricName] = [];
(<string[]> payload[meanMetricName]).push(generateMetricHelp(meanMetricName, string `Mean of ${
qualifiedMetricName}`));
(<string[]> payload[meanMetricName]).push(generateMetricInfo(meanMetricName, METRIC_TYPE_GAUGE));
}
(<string[]> payload[meanMetricName]).push(generateMetric(meanMetricName, tags, aSnapshot.mean));

string maxMetricName = getMetricName(qualifiedMetricName, "max");
if !payload.hasKey(maxMetricName) {
payload[maxMetricName] = [];
(<string[]> payload[maxMetricName]).push(generateMetricHelp(maxMetricName, string `Maximum of ${
qualifiedMetricName}`));
(<string[]> payload[maxMetricName]).push(generateMetricInfo(maxMetricName, METRIC_TYPE_GAUGE));
}
(<string[]> payload[maxMetricName]).push(generateMetric(maxMetricName, tags, aSnapshot.max));

string minMetricName = getMetricName(qualifiedMetricName, "min");
if !payload.hasKey(minMetricName) {
payload[minMetricName] = [];
(<string[]> payload[minMetricName]).push(generateMetricHelp(minMetricName, string `Minimum of ${
qualifiedMetricName}`));
(<string[]> payload[minMetricName]).push(generateMetricInfo(minMetricName, METRIC_TYPE_GAUGE));
}
(<string[]> payload[minMetricName]).push(generateMetric(minMetricName, tags, aSnapshot.min));

string stdDevMetricName = getMetricName(qualifiedMetricName, "stdDev");
if !payload.hasKey(stdDevMetricName) {
payload[stdDevMetricName] = [];
(<string[]> payload[stdDevMetricName]).push(generateMetricHelp(stdDevMetricName, string `Standard deviation of ${
qualifiedMetricName}`));
(<string[]> payload[stdDevMetricName]).push(generateMetricInfo(stdDevMetricName, METRIC_TYPE_GAUGE));
}
(<string[]> payload[stdDevMetricName]).push(generateMetric(stdDevMetricName, tags, aSnapshot.stdDev));

foreach var percentileValue in aSnapshot.percentileValues {
tags[PERCENTILE_TAG] = percentileValue.percentile.toString();
payload.push(generateMetric(qualifiedMetricName, tags, percentileValue.value));
(<string[]> payload[qualifiedMetricName]).push(generateMetric(qualifiedMetricName, tags, percentileValue.value));
}
_ = tags.remove(PERCENTILE_TAG);
}
}
}
string stringPayload = str:'join("\n", ...payload);

string stringPayload = string:'join("\n", ...payload.map(arr => string:'join("\n", ...arr)).toArray());
checkpanic caller->respond(stringPayload);
}
};
Expand Down

0 comments on commit 7d0b8e8

Please sign in to comment.