Skip to content

Commit

Permalink
Put active and tags into an info metric
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Best <robertbest89@gmail.com>
  • Loading branch information
ribbybibby committed Mar 16, 2022
1 parent 92fd3a8 commit c3e0ff8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 42 deletions.
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ Flags:
| Metric | Meaning | Labels |
| ----------------------------------------------- | ------------------------------------------------------ | ------------------------------------------------ |
| dependency_track_portfolio_inherited_risk_score | The inherited risk score of the whole portfolio. | |
| dependency_track_project_active | Is this project active? | uuid, name, version |
| dependency_track_project_tags | Project tags. | uuid, name, version, tags |
| dependency_track_project_info | Project information. | uuid, name, version, active, tags |
| dependency_track_project_vulnerabilities | Number of vulnerabilities for a project by severity. | uuid, name, version, severity |
| dependency_track_project_policy_violations | Policy violations for a project. | uuid, name, version, state, analysis, suppressed |
| dependency_track_project_last_bom_import | Last BOM import date, represented as a Unix timestamp. | uuid, name, version |
Expand All @@ -47,23 +46,20 @@ Exclude inactive projects:

```
dependency_track_project_policy_violations{state="WARN",analysis!="APPROVED",analysis!="REJECTED",suppressed="false"} > 0
and on(uuid,name,version) dependency_track_project_active == 1
and on(uuid) dependency_track_project_info{active="true"}
```

Only include projects tagged with `prod`:

```
dependency_track_project_policy_violations{state="WARN",analysis!="APPROVED",analysis!="REJECTED",suppressed="false"} > 0
and on(uuid,name,version) dependency_track_project_active == 1
and on(uuid,name,version) dependency_track_project_tags{tags=~".*,prod,.*"}
and on(uuid) dependency_track_project_info{active="true",tags=~".*,prod,.*"}
```

Or, join the tags label into the returned series for use in alerting rules:
Or, join the tags label into the returned series. Filtering on active/tag could
then happen in alert routes:

```
(
dependency_track_project_policy_violations{state="WARN",analysis!="APPROVED",analysis!="REJECTED",suppressed="false"} > 0
and on(uuid,name,version) dependency_track_project_active == 1
)
* on (uuid,name,version) group_left(tags) dependency_track_project_tags
(dependency_track_project_policy_violations{state="WARN",analysis!="APPROVED",analysis!="REJECTED",suppressed="false"} > 0)
* on (uuid) group_left(tags,active) dependency_track_project_info
```
1 change: 1 addition & 0 deletions internal/dependencytrack/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Project struct {
Name string `json:"name"`
Version string `json:"version"`
Classifier string `json:"classifier"`
Active bool `json:"active"`
LastBomImport Time `json:"lastBomImport"`
Metrics ProjectMetrics `json:"metrics"`
Expand Down
4 changes: 4 additions & 0 deletions internal/dependencytrack/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestGetProjects(t *testing.T) {
"name": "foo",
"version": "bar",
"active": true,
"classifier": "CONTAINER",
"lastBomImport": %d,
"metrics": {
"critical": 0,
Expand All @@ -50,6 +51,7 @@ func TestGetProjects(t *testing.T) {
"name": "bar",
"version": "foo",
"active": false,
"classifier": "APPLICATION",
"metrics": {
"critical": 50,
"high": 25,
Expand Down Expand Up @@ -80,6 +82,7 @@ func TestGetProjects(t *testing.T) {
{
Name: "foo",
Version: "bar",
Classifier: "CONTAINER",
Active: true,
LastBomImport: Time{now},
Metrics: ProjectMetrics{
Expand All @@ -103,6 +106,7 @@ func TestGetProjects(t *testing.T) {
{
Name: "bar",
Version: "foo",
Classifier: "APPLICATION",
Active: false,
LastBomImport: Time{},
Metrics: ProjectMetrics{
Expand Down
44 changes: 13 additions & 31 deletions internal/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,17 @@ func (e *Exporter) collectPortfolioMetrics(registry *prometheus.Registry) error

func (e *Exporter) collectProjectMetrics(registry *prometheus.Registry) error {
var (
active = prometheus.NewGaugeVec(
info = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: prometheus.BuildFQName(Namespace, "project", "active"),
Help: "Is this project active?",
},
[]string{
"uuid",
"name",
"version",
},
)
tags = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: prometheus.BuildFQName(Namespace, "project", "tags"),
Help: "Project tags.",
Name: prometheus.BuildFQName(Namespace, "project", "info"),
Help: "Project information.",
},
[]string{
"uuid",
"name",
"version",
"classifier",
"active",
"tags",
},
)
Expand Down Expand Up @@ -145,8 +136,7 @@ func (e *Exporter) collectProjectMetrics(registry *prometheus.Registry) error {
)
)
registry.MustRegister(
active,
tags,
info,
vulnerabilities,
policyViolations,
lastBOMImport,
Expand All @@ -159,25 +149,17 @@ func (e *Exporter) collectProjectMetrics(registry *prometheus.Registry) error {
}

for _, project := range projects {
var isActive float64
if project.Active {
isActive = 1
}
active.With(prometheus.Labels{
"uuid": project.UUID,
"name": project.Name,
"version": project.Version,
}).Set(isActive)

projTags := ","
for _, t := range project.Tags {
projTags = projTags + t.Name + ","
}
tags.With(prometheus.Labels{
"uuid": project.UUID,
"name": project.Name,
"version": project.Version,
"tags": projTags,
info.With(prometheus.Labels{
"uuid": project.UUID,
"name": project.Name,
"version": project.Version,
"classifier": project.Classifier,
"active": strconv.FormatBool(project.Active),
"tags": projTags,
}).Set(1)

severities := map[string]int32{
Expand Down

0 comments on commit c3e0ff8

Please sign in to comment.