Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 8825515

Browse files
authored
Add monitored label to metrics (#51)
Snyk supports disabling projects, but this is not reflected in the metrics exposed. This change introduces a 'monitored' label that can be used to filter disabled projects out.
1 parent 47b91cd commit 8825515

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ The API results are aggregated and recorded on the `snyk_vulnerabiilities_total`
8080
- `ignored` - The issue is ignored in Snyk.
8181
- `upgradeable` - The issue can be fixed by upgrading to a later version of the dependency.
8282
- `patchable` - The issue is patchable through Snyk.
83+
- `monitored` - The project is actively monitored by Snyk.
8384

8485
Here is an example.
8586

8687
```
87-
snyk_vulnerabilities_total{organization="my-org",project="my-app",severity="high",issue_type="vuln",issue_title="Privilege Escalation",ignored="false",upgradeable="false",patchable="false"} 1.0
88-
snyk_vulnerabilities_total{organization="my-org",project="my-app",severity="low",issue_type="vuln",issue_title="Sandbox (chroot) Escape",ignored="true",upgradeable="false",patchable="false"} 2.0
89-
snyk_vulnerabilities_total{organization="my-org",project="my-app",severity="medium",issue_type="license",issue_title="MPL-2.0 license",ignored="true",upgradeable="false",patchable="false"} 1
88+
snyk_vulnerabilities_total{organization="my-org",project="my-app",severity="high",issue_type="vuln",issue_title="Privilege Escalation",ignored="false",upgradeable="false",patchable="false",monitored="true"} 1.0
89+
snyk_vulnerabilities_total{organization="my-org",project="my-app",severity="low",issue_type="vuln",issue_title="Sandbox (chroot) Escape",ignored="true",upgradeable="false",patchable="false",monitored="false"} 2.0
90+
snyk_vulnerabilities_total{organization="my-org",project="my-app",severity="medium",issue_type="license",issue_title="MPL-2.0 license",ignored="true",upgradeable="false",patchable="false",monitored="true"} 1
9091
```
9192

9293
# Build

main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
ignoredLabel = "ignored"
2828
upgradeableLabel = "upgradeable"
2929
patchableLabel = "patchable"
30+
monitoredLabel = "monitored"
3031
)
3132

3233
var (
@@ -35,7 +36,7 @@ var (
3536
Name: "snyk_vulnerabilities_total",
3637
Help: "Gauge of Snyk vulnerabilities",
3738
},
38-
[]string{organizationLabel, projectLabel, issueTypeLabel, issueTitleLabel, severityLabel, ignoredLabel, upgradeableLabel, patchableLabel},
39+
[]string{organizationLabel, projectLabel, issueTypeLabel, issueTitleLabel, severityLabel, ignoredLabel, upgradeableLabel, patchableLabel, monitoredLabel},
3940
)
4041
)
4142

@@ -264,14 +265,15 @@ func register(results []gaugeResult) {
264265
vulnerabilityGauge.Reset()
265266
for _, r := range results {
266267
for _, result := range r.results {
267-
vulnerabilityGauge.WithLabelValues(r.organization, r.project, result.issueType, result.title, result.severity, strconv.FormatBool(result.ignored), strconv.FormatBool(result.upgradeable), strconv.FormatBool(result.patchable)).Set(float64(result.count))
268+
vulnerabilityGauge.WithLabelValues(r.organization, r.project, result.issueType, result.title, result.severity, strconv.FormatBool(result.ignored), strconv.FormatBool(result.upgradeable), strconv.FormatBool(result.patchable), strconv.FormatBool(r.isMonitored)).Set(float64(result.count))
268269
}
269270
}
270271
}
271272

272273
type gaugeResult struct {
273274
organization string
274275
project string
276+
isMonitored bool
275277
results []aggregateResult
276278
}
277279

@@ -294,6 +296,7 @@ func collect(ctx context.Context, client *client, organization org) ([]gaugeResu
294296
organization: organization.Name,
295297
project: project.Name,
296298
results: results,
299+
isMonitored: project.IsMonitored,
297300
})
298301
duration := time.Since(start)
299302
log.Debugf("Collected data in %v for %s %s", duration, project.ID, project.Name)

snyk.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ type projectOrg struct {
127127
}
128128

129129
type project struct {
130-
Name string `json:"name,omitempty"`
131-
ID string `json:"id,omitempty"`
130+
Name string `json:"name,omitempty"`
131+
ID string `json:"id,omitempty"`
132+
IsMonitored bool `json:"isMonitored,omitempty"`
132133
}
133134

134135
type issuesResponse struct {

0 commit comments

Comments
 (0)