Skip to content

Commit

Permalink
Add support for exporting any users (#49)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lbpdt authored Dec 3, 2020
1 parent f8969f5 commit 23ba60b
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions collector/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,16 @@ type user struct {
Realm string `json:"realm"`
}

type usersCount struct {
count float64
realm string
}
// map[<realm>] <count>
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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 23ba60b

Please sign in to comment.