Skip to content
This repository has been archived by the owner on Apr 29, 2020. It is now read-only.

Commit

Permalink
Reduce duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpherrinm committed Dec 6, 2019
1 parent 6cce7f4 commit dff1f70
Showing 1 changed file with 17 additions and 34 deletions.
51 changes: 17 additions & 34 deletions pkg/ds/farm.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,17 +580,10 @@ func (dsf *Farm) spawnDaemonSet(
for {
select {
case <-ctx.Done():
for _, name := range ds.MetricNames("healthy") {
p2metrics.Registry.Unregister(name)
}
for _, name := range ds.MetricNames("critical") {
p2metrics.Registry.Unregister(name)
}
for _, name := range ds.MetricNames("unknown") {
p2metrics.Registry.Unregister(name)
}
for _, name := range ds.MetricNames("warning") {
p2metrics.Registry.Unregister(name)
for _, suffix := range []string{"healthy", "critical", "unknown", "warning"} {
for _, name := range ds.MetricNames(suffix) {
p2metrics.Registry.Unregister(name)
}
}
return
case <-ticks.C:
Expand All @@ -600,29 +593,19 @@ func (dsf *Farm) spawnDaemonSet(
continue
}

numHealthy := aggregateHealth.NumHealthyOf(eligible)
numUnhealthy := aggregateHealth.NumUnhealthyOf(eligible)
numUnknownHealth := aggregateHealth.NumUnknownHealthOf(eligible)
numWarningHealth := aggregateHealth.NumWarningHealthOf(eligible)

for _, name := range ds.MetricNames("healthy") {
gauge := metrics.GetOrRegisterGauge(name, p2metrics.Registry)
gauge.Update(int64(numHealthy))
}

for _, name := range ds.MetricNames("critical") {
gauge := metrics.GetOrRegisterGauge(name, p2metrics.Registry)
gauge.Update(int64(numUnhealthy))
}

for _, name := range ds.MetricNames("unknown") {
gauge := metrics.GetOrRegisterGauge(name, p2metrics.Registry)
gauge.Update(int64(numUnknownHealth))
}

for _, name := range ds.MetricNames("warning") {
gauge := metrics.GetOrRegisterGauge(name, p2metrics.Registry)
gauge.Update(int64(numWarningHealth))
for _, metric := range []struct {
suffix string
val int
}{
{"healthy", aggregateHealth.NumHealthyOf(eligible)},
{"critical", aggregateHealth.NumUnhealthyOf(eligible)},
{"unknown", aggregateHealth.NumUnknownHealthOf(eligible)},
{"warning", aggregateHealth.NumWarningHealthOf(eligible)},
} {
for _, name := range ds.MetricNames(metric.suffix) {
gauge := metrics.GetOrRegisterGauge(name, p2metrics.Registry)
gauge.Update(int64(metric.val))
}
}
}
}
Expand Down

0 comments on commit dff1f70

Please sign in to comment.