From 4b6926d5ab706361c9b9840a06239c1a1a73cf1b Mon Sep 17 00:00:00 2001 From: Will Arroyo Date: Fri, 12 Feb 2021 19:43:54 -0700 Subject: [PATCH] adding tags to artifact metrics (#80) --- harbor_exporter.go | 8 ++--- metrics_artifacts.go | 74 ++++++++++++++++++++++++-------------------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/harbor_exporter.go b/harbor_exporter.go index 4d6664d..3cf2626 100644 --- a/harbor_exporter.go +++ b/harbor_exporter.go @@ -72,10 +72,10 @@ var ( typeLabelNames = []string{"type"} quotaLabelNames = []string{"type", "repo_name", "repo_id"} repoLabelNames = []string{"repo_name", "repo_id"} - artifactLabelNames = []string{"project_name", "project_id", "repo_name", "repo_id", "artifact_name", "artifact_id"} - artifactVulnerabilitiesLabelNames = []string{"project_name", "project_id", "repo_name", "repo_id", "artifact_name", "artifact_id", "report_id", "status"} - artifactsVulnerabilitiesScansLabelNames = []string{"project_name", "project_id", "repo_name", "repo_id", "artifact_name", "artifact_id"} - artifactVulnerabilitiesDurationLabelNames = []string{"project_name", "project_id", "repo_name", "repo_id", "artifact_name", "artifact_id", "report_id"} + artifactLabelNames = []string{"project_name", "project_id", "repo_name", "repo_id", "artifact_name", "artifact_id", "tag"} + artifactVulnerabilitiesLabelNames = []string{"project_name", "project_id", "repo_name", "repo_id", "artifact_name", "artifact_id", "report_id", "status", "tag"} + artifactsVulnerabilitiesScansLabelNames = []string{"project_name", "project_id", "repo_name", "repo_id", "artifact_name", "artifact_id", "tag"} + artifactVulnerabilitiesDurationLabelNames = []string{"project_name", "project_id", "repo_name", "repo_id", "artifact_name", "artifact_id", "report_id", "tag"} storageLabelNames = []string{"storage"} replicationLabelNames = []string{"repl_pol_name"} replicationTaskLabelNames = []string{"repl_pol_name", "result"} diff --git a/metrics_artifacts.go b/metrics_artifacts.go index 6b0133b..84d3997 100644 --- a/metrics_artifacts.go +++ b/metrics_artifacts.go @@ -121,41 +121,47 @@ func (h *HarborExporter) collectArtifactsMetric(ch chan<- prometheus.Metric) boo artID = strconv.FormatInt(ap.ID, 10) artName = ap.Digest ) - - // Size. - ch <- prometheus.MustNewConstMetric(sizeMI.Desc, sizeMI.Type, float64(ap.Size), projectName, projectID, repoName, repoID, artName, artID) - - // Vulnerabilities. - var scanInfo = &ap.ScanOverview - - // No scan performed. - var reportID = scanInfo.ReportID - - if reportID == "" { - continue - } - - ch <- prometheus.MustNewConstMetric(vulnMI.Desc, vulnMI.Type, float64(scanInfo.Summary.Fixable), projectName, projectID, repoName, repoID, artName, artID, reportID, "fixable") - ch <- prometheus.MustNewConstMetric(vulnMI.Desc, vulnMI.Type, float64(scanInfo.Summary.Total), projectName, projectID, repoName, repoID, artName, artID, reportID, "total") - ch <- prometheus.MustNewConstMetric(vulnMI.Desc, vulnMI.Type, float64(scanInfo.Summary.Summary.Low), projectName, projectID, repoName, repoID, artName, artID, reportID, "low") - ch <- prometheus.MustNewConstMetric(vulnMI.Desc, vulnMI.Type, float64(scanInfo.Summary.Summary.Medium), projectName, projectID, repoName, repoID, artName, artID, reportID, "medium") - ch <- prometheus.MustNewConstMetric(vulnMI.Desc, vulnMI.Type, float64(scanInfo.Summary.Summary.High), projectName, projectID, repoName, repoID, artName, artID, reportID, "high") - ch <- prometheus.MustNewConstMetric(vulnMI.Desc, vulnMI.Type, float64(scanInfo.Summary.Summary.Critical), projectName, projectID, repoName, repoID, artName, artID, reportID, "critical") - - // Scan Status. - ch <- prometheus.MustNewConstMetric(scansDurMI.Desc, scansDurMI.Type, float64(scanInfo.Duration), projectName, projectID, repoName, repoID, artName, artID, reportID) - ch <- prometheus.MustNewConstMetric(scansStartTS.Desc, scansStartTS.Type, float64(scanInfo.StartTime.Unix()), projectName, projectID, repoName, repoID, artName, artID, reportID) - - var scanRes float64 - - switch strings.ToLower(scanInfo.ScanStatus) { - case "success": - scanRes = 1 - case "running": - scanRes = 2 + for ti := range ap.Tags { + var ( + tag = &ap.Tags[ti] + tagName = tag.Name + ) + + // Size. + ch <- prometheus.MustNewConstMetric(sizeMI.Desc, sizeMI.Type, float64(ap.Size), projectName, projectID, repoName, repoID, artName, artID, tagName) + + // Vulnerabilities. + var scanInfo = &ap.ScanOverview + + // No scan performed. + var reportID = scanInfo.ReportID + + if reportID == "" { + continue + } + + ch <- prometheus.MustNewConstMetric(vulnMI.Desc, vulnMI.Type, float64(scanInfo.Summary.Fixable), projectName, projectID, repoName, repoID, artName, artID, reportID, "fixable", tagName) + ch <- prometheus.MustNewConstMetric(vulnMI.Desc, vulnMI.Type, float64(scanInfo.Summary.Total), projectName, projectID, repoName, repoID, artName, artID, reportID, "total", tagName) + ch <- prometheus.MustNewConstMetric(vulnMI.Desc, vulnMI.Type, float64(scanInfo.Summary.Summary.Low), projectName, projectID, repoName, repoID, artName, artID, reportID, "low", tagName) + ch <- prometheus.MustNewConstMetric(vulnMI.Desc, vulnMI.Type, float64(scanInfo.Summary.Summary.Medium), projectName, projectID, repoName, repoID, artName, artID, reportID, "medium", tagName) + ch <- prometheus.MustNewConstMetric(vulnMI.Desc, vulnMI.Type, float64(scanInfo.Summary.Summary.High), projectName, projectID, repoName, repoID, artName, artID, reportID, "high", tagName) + ch <- prometheus.MustNewConstMetric(vulnMI.Desc, vulnMI.Type, float64(scanInfo.Summary.Summary.Critical), projectName, projectID, repoName, repoID, artName, artID, reportID, "critical", tagName) + + // Scan Status. + ch <- prometheus.MustNewConstMetric(scansDurMI.Desc, scansDurMI.Type, float64(scanInfo.Duration), projectName, projectID, repoName, repoID, artName, artID, reportID, tagName) + ch <- prometheus.MustNewConstMetric(scansStartTS.Desc, scansStartTS.Type, float64(scanInfo.StartTime.Unix()), projectName, projectID, repoName, repoID, artName, artID, reportID, tagName) + + var scanRes float64 + + switch strings.ToLower(scanInfo.ScanStatus) { + case "success": + scanRes = 1 + case "running": + scanRes = 2 + } + + ch <- prometheus.MustNewConstMetric(scansMI.Desc, scansMI.Type, scanRes, projectName, projectID, repoName, repoID, artName, artID, tagName) } - - ch <- prometheus.MustNewConstMetric(scansMI.Desc, scansMI.Type, scanRes, projectName, projectID, repoName, repoID, artName, artID) } } }