From 23ba60bbe3abc8bad1af82bcd9987746049b9e58 Mon Sep 17 00:00:00 2001 From: Louis Blin <45168934+lbpdt@users.noreply.github.com> Date: Thu, 3 Dec 2020 17:10:39 +0000 Subject: [PATCH] Add support for exporting any users (#49) Instead of enumerating the types of users, how about dynamically creating metrics based of the realms that exist on the instance? For example, `http-sso` is another user realm that is currently not supported. There is a breaking change in this implementation though: user realms containing no users will no longer publish a `0` metric, but dashboards can default to `0` if data is missing so it feels reasonable. --- collector/security.go | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/collector/security.go b/collector/security.go index 023041d..08a08db 100644 --- a/collector/security.go +++ b/collector/security.go @@ -13,30 +13,16 @@ type user struct { Realm string `json:"realm"` } -type usersCount struct { - count float64 - realm string -} +// map[] +type realmUserCounts map[string] float64 -func (e *Exporter) countUsers(users []artifactory.User) []usersCount { +func (e *Exporter) countUsersPerRealm(users []artifactory.User) realmUserCounts { level.Debug(e.logger).Log("msg", "Counting users") - userCount := []usersCount{ - {0, "saml"}, - {0, "internal"}, - {0, "ldap"}, - } - + usersPerRealm := realmUserCounts {} for _, user := range users { - switch user.Realm { - case "saml": - userCount[0].count++ - case "internal": - userCount[1].count++ - case "ldap": - userCount[2].count++ - } + usersPerRealm[user.Realm]++; } - return userCount + return usersPerRealm } func (e *Exporter) exportUsersCount(metricName string, metric *prometheus.Desc, ch chan<- prometheus.Metric) error { @@ -48,17 +34,21 @@ func (e *Exporter) exportUsersCount(metricName string, metric *prometheus.Desc, return err } - // Count Users - usersCount := e.countUsers(users) + usersPerRealm := e.countUsersPerRealm(users) + + totalUserCount := 0 + for _, count := range usersPerRealm { + totalUserCount += int(count) + } - if usersCount[0].count == 0 && usersCount[1].count == 0 && usersCount[2].count == 0{ + if totalUserCount == 0 { e.jsonParseFailures.Inc() level.Error(e.logger).Log("err", "There was an issue getting users respond") return fmt.Errorf("There was an issue getting users respond") } - for _, user := range usersCount { - level.Debug(e.logger).Log("msg", "Registering metric", "metric", metricName, "realm", user.realm, "value", user.count) - ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, user.count, user.realm) + for realm, count := range usersPerRealm { + level.Debug(e.logger).Log("msg", "Registering metric", "metric", metricName, "realm", realm, "value", count) + ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, count, realm) } return nil }