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 6, 2021
1 parent
92c113f
commit 1806dba
Showing
6 changed files
with
151 additions
and
3 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.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,98 @@ | ||
package metrics | ||
|
||
import ( | ||
"bufio" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
|
||
const ( | ||
consoleHelmChartName = "console_helm_chart_name" | ||
) | ||
|
||
func TestHandleConsoleHelmInstallCountNoRelease(t *testing.T) { | ||
ts := httptest.NewServer(promhttp.Handler()) | ||
|
||
defer ts.Close() | ||
|
||
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)) | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
bytes, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
t.Fatalf("read error: %s", err) | ||
} | ||
|
||
found := findLineInResponse(t, string(bytes), consoleHelmChartName) | ||
if found { | ||
t.Error("console_helm_chart_name should not be available") | ||
} | ||
} | ||
|
||
func TestHandleConsoleHelmInstallCountSingleRelease(t *testing.T) { | ||
ts := httptest.NewServer(promhttp.Handler()) | ||
|
||
defer ts.Close() | ||
|
||
chartName, chartVersion := "test-chart", "0.0.1" | ||
HandleConsoleHelmInstallCount(chartName, chartVersion) | ||
|
||
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)) | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
bytes, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
t.Errorf("read error: %s", err) | ||
} | ||
|
||
found := findLineInResponse(t, string(bytes), consoleHelmChartName) | ||
if !found { | ||
t.Error("console_helm_chart_name should be available") | ||
} | ||
} | ||
|
||
// Reference: https://github.com/openshift/console-operator/blob/master/test/e2e/metrics_test.go#L71 | ||
func findLineInResponse(t *testing.T, haystack, needle string) (found bool) { | ||
scanner := bufio.NewScanner(strings.NewReader(haystack)) | ||
for scanner.Scan() { | ||
text := scanner.Text() | ||
// skip comments | ||
if strings.HasPrefix(text, "#") { | ||
continue | ||
} | ||
found := strings.Contains(text, needle) | ||
if found { | ||
t.Logf("found %s\n", scanner.Text()) | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
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
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