forked from openshift/console
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
metrics: add /metrics endpoint and console_helm_install_count metric
Works with openshift/console-operator#601 to add a console_helm_install_count counter vector metric. This change will increment the counter each time a user installs a Helm chart in the console. Closes: https://issues.redhat.com/browse/HELM-235 Signed-off-by: Allen Bai <abai@redhat.com>
- Loading branch information
Allen Bai
committed
Oct 7, 2021
1 parent
92c113f
commit e80702e
Showing
5 changed files
with
186 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
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,34 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
var ( | ||
consoleHelmInstallCount = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "console_helm_install_count", | ||
Help: "Number of Helm installation from console.", | ||
}, | ||
[]string{"console_helm_chart_name", "console_helm_chart_version"}, | ||
) | ||
) | ||
|
||
func init() { | ||
prometheus.MustRegister(consoleHelmInstallCount) | ||
} | ||
|
||
func HandleConsoleHelmInstallCount(chartName, chartVersion string) { | ||
defer recoverMetricPanic() | ||
|
||
klog.V(4).Infof("metric console_helm_install_count: %s %s", chartName, chartVersion) | ||
consoleHelmInstallCount.WithLabelValues(chartName, chartVersion).Add(1) | ||
} | ||
|
||
// Reference: https://github.com/openshift/console-operator/blob/master/pkg/console/metrics/metrics.go#L80 | ||
func recoverMetricPanic() { | ||
if r := recover(); r != nil { | ||
klog.Errorf("Recovering from metric function - %v", r) | ||
} | ||
} |
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,136 @@ | ||
package metrics | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
const ( | ||
consoleHelmInstallCountMetric = "console_helm_install_count" | ||
) | ||
|
||
func TestHandleConsoleHelmInstallCountNoRelease(t *testing.T) { | ||
consoleHelmInstallCount.Reset() | ||
ts := httptest.NewServer(promhttp.Handler()) | ||
defer ts.Close() | ||
|
||
count := countMetric(t, ts, consoleHelmInstallCountMetric) | ||
if count > 0 { | ||
t.Errorf("%s should not be available", consoleHelmInstallCountMetric) | ||
} | ||
} | ||
|
||
func TestHandleConsoleHelmInstallCountSingleRelease(t *testing.T) { | ||
consoleHelmInstallCount.Reset() | ||
ts := httptest.NewServer(promhttp.Handler()) | ||
defer ts.Close() | ||
|
||
chartName, chartVersion := "test-chart", "0.0.1" | ||
chartNameLabel, chartVersionLabel := fmt.Sprintf("console_helm_chart_name=\"%v\"", chartName), fmt.Sprintf("console_helm_chart_version=\"%v\"", chartVersion) | ||
HandleConsoleHelmInstallCount(chartName, chartVersion) | ||
|
||
count := countMetric(t, ts, consoleHelmInstallCountMetric, chartNameLabel, chartVersionLabel) | ||
if count != 1 { | ||
t.Errorf("%s with labels %s, %s should be 1: %v", consoleHelmInstallCountMetric, chartNameLabel, chartVersionLabel, count) | ||
} | ||
} | ||
|
||
func TestHandleConsoleHelmInstallCountMultipleReleases(t *testing.T) { | ||
consoleHelmInstallCount.Reset() | ||
ts := httptest.NewServer(promhttp.Handler()) | ||
defer ts.Close() | ||
|
||
chartName, chartVersion := "test-chart", "0.0.1" | ||
chartNameLabel, chartVersionLabel := fmt.Sprintf("console_helm_chart_name=\"%v\"", chartName), fmt.Sprintf("console_helm_chart_version=\"%v\"", chartVersion) | ||
HandleConsoleHelmInstallCount(chartName, chartVersion) | ||
HandleConsoleHelmInstallCount(chartName, chartVersion) | ||
|
||
count := countMetric(t, ts, consoleHelmInstallCountMetric, chartNameLabel, chartVersionLabel) | ||
if count != 2 { | ||
t.Errorf("%s with labels %s, %s should be 2: %v", consoleHelmInstallCountMetric, chartNameLabel, chartVersionLabel, count) | ||
} | ||
|
||
chartName, chartVersion = "test-chart-2", "0.0.2" | ||
chartNameLabel, chartVersionLabel = fmt.Sprintf("console_helm_chart_name=\"%v\"", chartName), fmt.Sprintf("console_helm_chart_version=\"%v\"", chartVersion) | ||
HandleConsoleHelmInstallCount(chartName, chartVersion) | ||
|
||
count = countMetric(t, ts, consoleHelmInstallCountMetric, chartNameLabel, chartVersionLabel) | ||
if count != 1 { | ||
t.Errorf("%s with labels %s, %s should be 1: %v", consoleHelmInstallCountMetric, chartNameLabel, chartVersionLabel, count) | ||
} | ||
|
||
count = countMetric(t, ts, consoleHelmInstallCountMetric) | ||
if count != 3 { | ||
t.Errorf("%s without labels should be 3: %v", consoleHelmInstallCountMetric, count) | ||
} | ||
|
||
} | ||
|
||
func getMetrics(t *testing.T, ts *httptest.Server) *http.Response { | ||
res, err := http.Get(ts.URL + "/metrics") | ||
if err != nil { | ||
t.Errorf("http error: %s", err) | ||
} | ||
|
||
if !httpOK(res) { | ||
t.Errorf("http error: %d %s", res.StatusCode, http.StatusText(res.StatusCode)) | ||
} | ||
return res | ||
} | ||
|
||
func countMetric(t *testing.T, ts *httptest.Server, metric string, labels ...string) int { | ||
res := getMetrics(t, ts) | ||
defer res.Body.Close() | ||
|
||
bytes, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
t.Fatalf("read error: %s", err) | ||
} | ||
|
||
return countMetricWithLabels(t, string(bytes), metric, labels...) | ||
} | ||
|
||
func countMetricWithLabels(t *testing.T, response, metric string, labels ...string) (count int) { | ||
scanner := bufio.NewScanner(strings.NewReader(response)) | ||
for scanner.Scan() { | ||
text := scanner.Text() | ||
// skip comments | ||
if strings.HasPrefix(text, "#") { | ||
continue | ||
} | ||
if strings.Contains(text, metric) { | ||
t.Logf("found %s\n", scanner.Text()) | ||
curr_count, _ := strconv.Atoi(text[len(text)-1:]) | ||
// no specific labels, count all | ||
if len(labels) == 0 { | ||
count += curr_count | ||
} | ||
// return metric value with specified labels | ||
for i, label := range labels { | ||
if !strings.Contains(text, label) { | ||
break | ||
} | ||
if i == len(labels)-1 { | ||
// return directly since metrics are aggregated | ||
return curr_count | ||
} | ||
} | ||
} | ||
} | ||
return count | ||
} | ||
|
||
func httpOK(resp *http.Response) bool { | ||
if resp.StatusCode >= 200 && resp.StatusCode <= 299 { | ||
return true | ||
} | ||
return false | ||
} |
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 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