From 4091c1b19dae3d39daec3b9203e036fb4e0723e0 Mon Sep 17 00:00:00 2001 From: Gerben Wiersma <10595726+gwiersma@users.noreply.github.com> Date: Tue, 23 Mar 2021 16:26:21 +0100 Subject: [PATCH] Add scan errors and success metrics (#83) * Add scan errors and success metrics * linting * double value Co-authored-by: wierg01 --- README.md | 2 ++ harbor_exporter.go | 2 ++ metrics_scan.go | 24 +++++++++++++++++++----- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 430ee22..80c3f03 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ make dockerbuild |harbor_health_latency| | | |harbor_scans_completed | | | |harbor_scans_total | | | +|harbor_scans_success | | | +|harbor_scans_error | | | |harbor_scans_requester | | | |harbor_scans_latency| | | |harbor_project_count_total| |type=[private_project, public_project, total_project]| diff --git a/harbor_exporter.go b/harbor_exporter.go index 3505123..8061f74 100644 --- a/harbor_exporter.go +++ b/harbor_exporter.go @@ -109,7 +109,9 @@ func createMetrics(instanceName string) { allMetrics["scans_total"] = newMetricInfo(instanceName, "scans_total", "metrics of the latest scan all process", prometheus.GaugeValue, nil, nil) allMetrics["scans_completed"] = newMetricInfo(instanceName, "scans_completed", "metrics of the latest scan all process", prometheus.GaugeValue, nil, nil) allMetrics["scans_requester"] = newMetricInfo(instanceName, "scans_requester", "metrics of the latest scan all process", prometheus.GaugeValue, nil, nil) + allMetrics["scans_success"] = newMetricInfo(instanceName, "scans_success", "metrics of the current amount of succeeded scans", prometheus.GaugeValue, nil, nil) allMetrics["scans_latency"] = newMetricInfo(instanceName, "scans_latency", "Time in seconds to collect scan metrics", prometheus.GaugeValue, nil, nil) + allMetrics["scans_error"] = newMetricInfo(instanceName, "scans_error", "Amount of failed scans", prometheus.GaugeValue, nil, nil) allMetrics["project_count_total"] = newMetricInfo(instanceName, "project_count_total", "projects number relevant to the user", prometheus.GaugeValue, typeLabelNames, nil) allMetrics["repo_count_total"] = newMetricInfo(instanceName, "repo_count_total", "repositories number relevant to the user", prometheus.GaugeValue, typeLabelNames, nil) allMetrics["statistics_latency"] = newMetricInfo(instanceName, "statistics_latency", "Time in seconds to collect statistics metrics", prometheus.GaugeValue, nil, nil) diff --git a/metrics_scan.go b/metrics_scan.go index ca34199..3a3433e 100644 --- a/metrics_scan.go +++ b/metrics_scan.go @@ -13,12 +13,17 @@ func (h *HarborExporter) collectScanMetric(ch chan<- prometheus.Metric) bool { start := time.Now() type scanMetric struct { - Total float64 - Completed float64 - metrics []interface{} - Requester string - Ongoing bool + Total float64 `json:"total"` + Completed float64 `json:"completed"` + Metrics struct { + Error float64 `json:"Error"` + Success float64 `json:"Success"` + } `json:"metrics"` + Requester string `json:"requester"` + Ongoing bool `json:"ongoing"` } + + body, _ := h.request("/scans/all/metrics") var data scanMetric @@ -36,6 +41,15 @@ func (h *HarborExporter) collectScanMetric(ch chan<- prometheus.Metric) bool { allMetrics["scans_total"].Desc, allMetrics["scans_total"].Type, float64(data.Total), ) + ch <- prometheus.MustNewConstMetric( + allMetrics["scans_error"].Desc, allMetrics["scans_error"].Type, float64(data.Metrics.Error), + ) + + ch <- prometheus.MustNewConstMetric( + allMetrics["scans_success"].Desc, allMetrics["scans_success"].Type, float64(data.Metrics.Success), + ) + + ch <- prometheus.MustNewConstMetric( allMetrics["scans_completed"].Desc, allMetrics["scans_completed"].Type, float64(data.Completed), )