Skip to content

feat: expose user seat limits as Prometheus metrics #10169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use const metrics
  • Loading branch information
mtojek committed Oct 13, 2023
commit 8845fb3825a71eb58bd362456454266b1f10947d
5 changes: 2 additions & 3 deletions enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
psk: options.ProvisionerDaemonPSK,
authorizer: options.Authorizer,
},
licenseMetricsCollector: license.NewMetricsCollector(),
}
defer func() {
if err != nil {
Expand Down Expand Up @@ -376,7 +375,7 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
api.AGPL.WorkspaceProxyHostsFn.Store(&f)
}

err = api.PrometheusRegistry.Register(api.licenseMetricsCollector)
err = api.PrometheusRegistry.Register(&api.licenseMetricsCollector)
if err != nil {
return nil, xerrors.Errorf("unable to register license metrics collector")
}
Expand Down Expand Up @@ -441,7 +440,7 @@ type API struct {

provisionerDaemonAuth *provisionerDaemonAuth

licenseMetricsCollector *license.MetricsCollector
licenseMetricsCollector license.MetricsCollector
}

func (api *API) Close() error {
Expand Down
50 changes: 13 additions & 37 deletions enterprise/coderd/license/metricscollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,22 @@ import (
"github.com/coder/coder/v2/codersdk"
)

var (
activeUsersDesc = prometheus.NewDesc("coderd_license_active_users", "The number of active users.", nil, nil)
limitUsersDesc = prometheus.NewDesc("coderd_license_limit_users", "The user seats limit based on the active Coder license.", nil, nil)
userLimitEnabledDesc = prometheus.NewDesc("coderd_license_user_limit_enabled", "Returns 1 if the current license enforces the user limit.", nil, nil)
)

type MetricsCollector struct {
Entitlements atomic.Pointer[codersdk.Entitlements]

activeUsersGauge prometheus.Gauge
limitUsersGauge prometheus.Gauge
userLimitEnabledGauge prometheus.Gauge
}

var _ prometheus.Collector = new(MetricsCollector)

func NewMetricsCollector() *MetricsCollector {
return &MetricsCollector{
activeUsersGauge: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "coderd",
Subsystem: "license",
Name: "active_users",
Help: `The number of active users.`,
}),
limitUsersGauge: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "coderd",
Subsystem: "license",
Name: "limit_users",
Help: "The user seats limit based on the active Coder license.",
}),
userLimitEnabledGauge: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "coderd",
Subsystem: "license",
Name: "user_limit_enabled",
Help: "Returns 1 if the current license enforces the user limit.",
}),
}
}

func (mc *MetricsCollector) Describe(descCh chan<- *prometheus.Desc) {
descCh <- mc.activeUsersGauge.Desc()
descCh <- mc.limitUsersGauge.Desc()
descCh <- mc.userLimitEnabledGauge.Desc()
func (*MetricsCollector) Describe(descCh chan<- *prometheus.Desc) {
descCh <- activeUsersDesc
descCh <- limitUsersDesc
descCh <- userLimitEnabledDesc
}

func (mc *MetricsCollector) Collect(metricsCh chan<- prometheus.Metric) {
Expand All @@ -62,16 +41,13 @@ func (mc *MetricsCollector) Collect(metricsCh chan<- prometheus.Metric) {
if userLimitEntitlement.Enabled {
enabled = 1
}
mc.userLimitEnabledGauge.Set(enabled)
metricsCh <- mc.userLimitEnabledGauge
metricsCh <- prometheus.MustNewConstMetric(userLimitEnabledDesc, prometheus.GaugeValue, enabled)

if userLimitEntitlement.Actual != nil {
mc.activeUsersGauge.Set(float64(*userLimitEntitlement.Actual))
metricsCh <- mc.activeUsersGauge
metricsCh <- prometheus.MustNewConstMetric(activeUsersDesc, prometheus.GaugeValue, float64(*userLimitEntitlement.Actual))
}

if userLimitEntitlement.Limit != nil {
mc.limitUsersGauge.Set(float64(*userLimitEntitlement.Limit))
metricsCh <- mc.limitUsersGauge
metricsCh <- prometheus.MustNewConstMetric(limitUsersDesc, prometheus.GaugeValue, float64(*userLimitEntitlement.Limit))
}
}
4 changes: 2 additions & 2 deletions enterprise/coderd/license/metricscollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCollectLicenseMetrics(t *testing.T) {
// Given
registry := prometheus.NewRegistry()

sut := license.NewMetricsCollector()
var sut license.MetricsCollector

const (
actualUsers = 4
Expand All @@ -35,7 +35,7 @@ func TestCollectLicenseMetrics(t *testing.T) {
},
})

registry.Register(sut)
registry.Register(&sut)

// When
metrics, err := registry.Gather()
Expand Down