Skip to content

Commit

Permalink
adding tags to artifact metrics (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
warroyo authored Feb 13, 2021
1 parent 80b4714 commit 4b6926d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
8 changes: 4 additions & 4 deletions harbor_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
74 changes: 40 additions & 34 deletions metrics_artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit 4b6926d

Please sign in to comment.