-
Notifications
You must be signed in to change notification settings - Fork 699
metrics: add /metrics endpoint and console_helm_install_count metric #10194
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,75 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| const ( | ||
| consoleHelmInstallsTotalMetric = "console_helm_installs_total" | ||
| consoleHelmUpgradesTotalMetric = "console_helm_upgrades_total" | ||
| consoleHelmUninstallsTotalMetric = "console_helm_uninstalls_total" | ||
|
|
||
| consoleHelmChartNameLabel = "chart_name" | ||
| consoleHelmChartVersionLabel = "chart_version" | ||
| ) | ||
|
|
||
| var ( | ||
| consoleHelmInstallsTotal = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Name: consoleHelmInstallsTotalMetric, | ||
| Help: "Number of Helm installations from console by chart name and version.", | ||
| }, | ||
| []string{consoleHelmChartNameLabel, consoleHelmChartVersionLabel}, | ||
| ) | ||
| consoleHelmUpgradesTotal = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Name: consoleHelmUpgradesTotalMetric, | ||
| Help: "Number of Helm release upgrades from console by chart name and version.", | ||
| }, | ||
| []string{consoleHelmChartNameLabel, consoleHelmChartVersionLabel}, | ||
| ) | ||
| consoleHelmUninstallsTotal = prometheus.NewCounterVec( | ||
| prometheus.CounterOpts{ | ||
| Name: consoleHelmUninstallsTotalMetric, | ||
| Help: "Number of Helm release uninstallations from console by chart name and version.", | ||
| }, | ||
| []string{consoleHelmChartNameLabel, consoleHelmChartVersionLabel}, | ||
| ) | ||
| ) | ||
|
|
||
| func init() { | ||
| prometheus.MustRegister(consoleHelmInstallsTotal) | ||
| prometheus.MustRegister(consoleHelmUpgradesTotal) | ||
| prometheus.MustRegister(consoleHelmUninstallsTotal) | ||
| } | ||
|
|
||
| func HandleconsoleHelmInstallsTotal(chartName, chartVersion string) { | ||
| klog.V(4).Infof("metric %s: %s %s", consoleHelmInstallsTotalMetric, chartName, chartVersion) | ||
| counter, err := consoleHelmInstallsTotal.GetMetricWithLabelValues(chartName, chartVersion) | ||
| if err != nil { | ||
| klog.Errorf("Recovering from metric function - %v", err) | ||
| return | ||
| } | ||
| counter.Add(1) | ||
| } | ||
|
|
||
| func HandleconsoleHelmUpgradesTotal(chartName, chartVersion string) { | ||
| klog.V(4).Infof("metric %s: %s %s", consoleHelmUpgradesTotalMetric, chartName, chartVersion) | ||
| counter, err := consoleHelmUpgradesTotal.GetMetricWithLabelValues(chartName, chartVersion) | ||
| if err != nil { | ||
| klog.Errorf("Recovering from metric function - %v", err) | ||
| return | ||
| } | ||
| counter.Add(1) | ||
| } | ||
|
|
||
| func HandleconsoleHelmUninstallsTotal(chartName, chartVersion string) { | ||
| klog.V(4).Infof("metric %s: %s %s", consoleHelmUninstallsTotalMetric, chartName, chartVersion) | ||
| counter, err := consoleHelmUninstallsTotal.GetMetricWithLabelValues(chartName, chartVersion) | ||
| if err != nil { | ||
| klog.Errorf("Recovering from metric function - %v", err) | ||
| return | ||
| } | ||
| counter.Add(1) | ||
| } | ||
This file contains hidden or 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,191 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strconv" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||
| ) | ||
|
|
||
| func TestMetricsNoRelease(t *testing.T) { | ||
| consoleHelmInstallsTotal.Reset() | ||
| consoleHelmUpgradesTotal.Reset() | ||
| consoleHelmUninstallsTotal.Reset() | ||
| ts := httptest.NewServer(promhttp.Handler()) | ||
| defer ts.Close() | ||
|
|
||
| count := countMetric(t, ts, consoleHelmInstallsTotalMetric) | ||
| if count > 0 { | ||
| t.Errorf("%s should not be available", consoleHelmInstallsTotalMetric) | ||
| } | ||
|
|
||
| count = countMetric(t, ts, consoleHelmUpgradesTotalMetric) | ||
| if count > 0 { | ||
| t.Errorf("%s should not be available", consoleHelmUpgradesTotalMetric) | ||
| } | ||
|
|
||
| count = countMetric(t, ts, consoleHelmUninstallsTotalMetric) | ||
| if count > 0 { | ||
| t.Errorf("%s should not be available", consoleHelmUninstallsTotalMetric) | ||
| } | ||
| } | ||
|
|
||
| func TestMetricsSingleRelease(t *testing.T) { | ||
| consoleHelmInstallsTotal.Reset() | ||
| consoleHelmUpgradesTotal.Reset() | ||
| consoleHelmUninstallsTotal.Reset() | ||
| ts := httptest.NewServer(promhttp.Handler()) | ||
| defer ts.Close() | ||
|
|
||
| chartName, chartVersion := "test-chart", "0.0.1" | ||
| chartNameLabel, chartVersionLabel := fmt.Sprintf("%s=\"%v\"", consoleHelmChartNameLabel, chartName), fmt.Sprintf("%s=\"%v\"", consoleHelmChartVersionLabel, chartVersion) | ||
| HandleconsoleHelmInstallsTotal(chartName, chartVersion) | ||
| HandleconsoleHelmUpgradesTotal(chartName, chartVersion) | ||
| HandleconsoleHelmUninstallsTotal(chartName, chartVersion) | ||
|
|
||
| count := countMetric(t, ts, consoleHelmInstallsTotalMetric, chartNameLabel, chartVersionLabel) | ||
| if count != 1 { | ||
| t.Errorf("%s with labels %s, %s should be 1: %v", consoleHelmInstallsTotalMetric, chartNameLabel, chartVersionLabel, count) | ||
| } | ||
|
|
||
| count = countMetric(t, ts, consoleHelmUpgradesTotalMetric, chartNameLabel, chartVersionLabel) | ||
| if count != 1 { | ||
| t.Errorf("%s with labels %s, %s should be 1: %v", consoleHelmUpgradesTotalMetric, chartNameLabel, chartVersionLabel, count) | ||
| } | ||
|
|
||
| count = countMetric(t, ts, consoleHelmUninstallsTotalMetric, chartNameLabel, chartVersionLabel) | ||
| if count != 1 { | ||
| t.Errorf("%s with labels %s, %s should be 1: %v", consoleHelmUninstallsTotalMetric, chartNameLabel, chartVersionLabel, count) | ||
| } | ||
| } | ||
|
|
||
| func TestMetricsMultipleReleases(t *testing.T) { | ||
| consoleHelmInstallsTotal.Reset() | ||
| consoleHelmUpgradesTotal.Reset() | ||
| consoleHelmUninstallsTotal.Reset() | ||
| ts := httptest.NewServer(promhttp.Handler()) | ||
| defer ts.Close() | ||
|
|
||
| chartName, chartVersion := "test-chart", "0.0.1" | ||
| chartNameLabel, chartVersionLabel := fmt.Sprintf("%s=\"%v\"", consoleHelmChartNameLabel, chartName), fmt.Sprintf("%s=\"%v\"", consoleHelmChartVersionLabel, chartVersion) | ||
| HandleconsoleHelmInstallsTotal(chartName, chartVersion) | ||
| HandleconsoleHelmInstallsTotal(chartName, chartVersion) | ||
|
|
||
| HandleconsoleHelmUpgradesTotal(chartName, chartVersion) | ||
| HandleconsoleHelmUpgradesTotal(chartName, chartVersion) | ||
|
|
||
| HandleconsoleHelmUninstallsTotal(chartName, chartVersion) | ||
| HandleconsoleHelmUninstallsTotal(chartName, chartVersion) | ||
|
|
||
| count := countMetric(t, ts, consoleHelmInstallsTotalMetric, chartNameLabel, chartVersionLabel) | ||
| if count != 2 { | ||
| t.Errorf("%s with labels %s, %s should be 2: %v", consoleHelmInstallsTotalMetric, chartNameLabel, chartVersionLabel, count) | ||
| } | ||
|
|
||
| count = countMetric(t, ts, consoleHelmUpgradesTotalMetric, chartNameLabel, chartVersionLabel) | ||
| if count != 2 { | ||
| t.Errorf("%s with labels %s, %s should be 2: %v", consoleHelmUpgradesTotalMetric, chartNameLabel, chartVersionLabel, count) | ||
| } | ||
|
|
||
| count = countMetric(t, ts, consoleHelmUninstallsTotalMetric, chartNameLabel, chartVersionLabel) | ||
| if count != 2 { | ||
| t.Errorf("%s with labels %s, %s should be 2: %v", consoleHelmUninstallsTotalMetric, chartNameLabel, chartVersionLabel, count) | ||
| } | ||
|
|
||
| chartName, chartVersion = "test-chart-2", "0.0.2" | ||
| chartNameLabel, chartVersionLabel = fmt.Sprintf("%s=\"%v\"", consoleHelmChartNameLabel, chartName), fmt.Sprintf("%s=\"%v\"", consoleHelmChartVersionLabel, chartVersion) | ||
| HandleconsoleHelmInstallsTotal(chartName, chartVersion) | ||
| HandleconsoleHelmUpgradesTotal(chartName, chartVersion) | ||
| HandleconsoleHelmUninstallsTotal(chartName, chartVersion) | ||
|
|
||
| count = countMetric(t, ts, consoleHelmInstallsTotalMetric, chartNameLabel, chartVersionLabel) | ||
| if count != 1 { | ||
| t.Errorf("%s with labels %s, %s should be 1: %v", consoleHelmInstallsTotalMetric, chartNameLabel, chartVersionLabel, count) | ||
| } | ||
|
|
||
| count = countMetric(t, ts, consoleHelmUpgradesTotalMetric, chartNameLabel, chartVersionLabel) | ||
| if count != 1 { | ||
| t.Errorf("%s with labels %s, %s should be 1: %v", consoleHelmUpgradesTotalMetric, chartNameLabel, chartVersionLabel, count) | ||
| } | ||
|
|
||
| count = countMetric(t, ts, consoleHelmUninstallsTotalMetric, chartNameLabel, chartVersionLabel) | ||
| if count != 1 { | ||
| t.Errorf("%s with labels %s, %s should be 1: %v", consoleHelmUninstallsTotalMetric, chartNameLabel, chartVersionLabel, count) | ||
| } | ||
|
|
||
| // Metrics without specific labels | ||
| count = countMetric(t, ts, consoleHelmInstallsTotalMetric) | ||
| if count != 3 { | ||
| t.Errorf("%s without labels should be 3: %v", consoleHelmInstallsTotalMetric, count) | ||
| } | ||
|
|
||
| count = countMetric(t, ts, consoleHelmUpgradesTotalMetric) | ||
| if count != 3 { | ||
| t.Errorf("%s without labels should be 3: %v", consoleHelmUpgradesTotalMetric, count) | ||
| } | ||
|
|
||
| count = countMetric(t, ts, consoleHelmUninstallsTotalMetric) | ||
| if count != 3 { | ||
| t.Errorf("%s without labels should be 3: %v", consoleHelmUninstallsTotalMetric, 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 res.StatusCode != 200 { | ||
| 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 | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this wont recover the function since the
GetMetricWithLabelValuesthrows a panic.Instead
We should do the same for the
HandleconsoleHelmUpgradesTotal&HandleconsoleHelmUninstallsTotalfunctionsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the official doc it seems like
WithLabelValueswill panic in the case whenGetMetricWithLabelValuesreturns an error: https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#CounterVec.GetMetricWithLabelValues vs. https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#CounterVec.WithLabelValues.Could you confirm the above doc and
GetMetricWithLabelValueswill indeed panic? Will proceed to update after confirming :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetMetricWithLabelValueswon't panic.WithLabelValueswould only panic if you give it an incorrect number of parameters (e.g. your metric declares 2 labels but you only passed 1 value).In general, it's better to let
WithLabelValuescrash the program since it means that there's a programmatic error.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@simonpasquier you are right,
WithLabelValueswould only panic. Misread the code 👍