Skip to content

feat: expose template insights as Prometheus metrics #10325

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 21 commits into from
Oct 19, 2023
Merged
Next Next commit
WIP
  • Loading branch information
mtojek committed Oct 13, 2023
commit 24e4ff388fd182124b63f25edf1cb68c04ed2287
13 changes: 13 additions & 0 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import (
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/coderd/oauthpki"
"github.com/coder/coder/v2/coderd/prometheusmetrics"
"github.com/coder/coder/v2/coderd/prometheusmetrics/insights"
"github.com/coder/coder/v2/coderd/schedule"
"github.com/coder/coder/v2/coderd/telemetry"
"github.com/coder/coder/v2/coderd/tracing"
Expand Down Expand Up @@ -198,6 +199,18 @@ func enablePrometheus(
}
afterCtx(ctx, closeWorkspacesFunc)

insightsMetricsCollector := insights.NewMetricsCollector(options.Database, 0)
err = options.PrometheusRegistry.Register(insightsMetricsCollector)
if err != nil {
return nil, xerrors.Errorf("unable to register insights metrics collector: %w", err)
}

closeInsightsMetricsCollector, err := insightsMetricsCollector.Run(ctx)
if err != nil {
return nil, xerrors.Errorf("unable to run insights metrics collector: %w", err)
}
afterCtx(ctx, closeInsightsMetricsCollector)

if vals.Prometheus.CollectAgentStats {
closeAgentStatsFunc, err := prometheusmetrics.AgentStats(ctx, logger, options.PrometheusRegistry, options.Database, time.Now(), 0)
if err != nil {
Expand Down
72 changes: 72 additions & 0 deletions coderd/prometheusmetrics/insights/metricscollector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package insights

import (
"context"
"time"

"github.com/prometheus/client_golang/prometheus"

"github.com/coder/coder/v2/coderd/database"
)

var (
activeUsersDesc = prometheus.NewDesc("coderd_insights_active_users", "The number of active users of the template.", []string{"template_name"}, nil)
applicationsUsageSecondsDesc = prometheus.NewDesc("coderd_insights_applications_usage_seconds", "The application usage per template.", []string{"application_name", "template_name"}, nil)
parametersDesc = prometheus.NewDesc("coderd_insights_parameters", "The parameter usage per template.", []string{"template_name", "name", "value"}, nil)
)

type MetricsCollector struct {
database database.Store
duration time.Duration
}

var _ prometheus.Collector = new(MetricsCollector)

func NewMetricsCollector(db database.Store, duration time.Duration) *MetricsCollector {
if duration == 0 {
duration = 5 * time.Minute
}

return &MetricsCollector{
database: db,
duration: duration,
}
}

func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {
ctx, closeFunc := context.WithCancel(ctx)
done := make(chan struct{})

// Use time.Nanosecond to force an initial tick. It will be reset to the
// correct duration after executing once.
ticker := time.NewTicker(time.Nanosecond)
doTick := func() {
defer ticker.Reset(mc.duration)
}

go func() {
defer close(done)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
doTick()
}
}
}()
return func() {
closeFunc()
<-done
}, nil
}

func (*MetricsCollector) Describe(descCh chan<- *prometheus.Desc) {
descCh <- activeUsersDesc
descCh <- applicationsUsageSecondsDesc
descCh <- parametersDesc
}

func (mc *MetricsCollector) Collect(metricsCh chan<- prometheus.Metric) {
}