Skip to content

Commit

Permalink
Add feature flag DRONE_AUTOSCALER_REGISTER_KNOWN_SERVERS
Browse files Browse the repository at this point in the history
Only expose the new metrics when this variable is set
  • Loading branch information
iainlane committed Jul 21, 2022
1 parent b5f8ab0 commit 8ee3814
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package metrics

import (
"context"
"os"
"strconv"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -15,6 +17,16 @@ import (

var noContext = context.Background()

// this is a feature flag that can be used to enable
// metrics to track registering/unregistering of servers
var registerKnownServers = false

func init() {
registerKnownServers, _ = strconv.ParseBool(
os.Getenv("DRONE_AUTOSCALER_REGISTER_KNOWN_SERVERS"),
)
}

// Collector defines a metrics collector.
type Collector interface {
// TrackServerCreateTime registers the elapsed time it takes
Expand Down Expand Up @@ -105,7 +117,9 @@ func New() *Prometheus {
prometheus.MustRegister(p.countServerCreateErr)
prometheus.MustRegister(p.countServerInitErr)
prometheus.MustRegister(p.countServerSetupErr)
prometheus.MustRegister(p.knownInstance)
if registerKnownServers {
prometheus.MustRegister(p.knownInstance)
}
return p
}

Expand Down Expand Up @@ -155,22 +169,26 @@ func (m *Prometheus) IncrServerSetupError() {

// RegisterKnownInstance registers that we know about a server.
func (m *Prometheus) RegisterKnownInstance(instance *autoscaler.Instance) {
m.knownInstance.With(prometheus.Labels{
"name": instance.Name,
"provider": string(instance.Provider),
"region": instance.Region,
"size": instance.Size,
}).Set(1)
if registerKnownServers {
m.knownInstance.With(prometheus.Labels{
"name": instance.Name,
"provider": string(instance.Provider),
"region": instance.Region,
"size": instance.Size,
}).Set(1)
}
}

// UnregisterKnownInstance forgets a server we once knew.
func (m *Prometheus) UnregisterKnownInstance(instance *autoscaler.Instance) {
m.knownInstance.Delete(prometheus.Labels{
"name": instance.Name,
"provider": string(instance.Provider),
"region": instance.Region,
"size": instance.Size,
})
if registerKnownServers {
m.knownInstance.Delete(prometheus.Labels{
"name": instance.Name,
"provider": string(instance.Provider),
"region": instance.Region,
"size": instance.Size,
})
}
}

// NopCollector provides a no-op metrics collector.
Expand Down

0 comments on commit 8ee3814

Please sign in to comment.