Skip to content

feat: expose template insights as Prometheus metrics #10255

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

Closed
wants to merge 9 commits into from
Closed
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
Next Next commit
Use db
  • Loading branch information
mtojek committed Oct 13, 2023
commit a6c9c1a3bdad14a6dbf2b161c3c4a4cab9bda293
5 changes: 4 additions & 1 deletion cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ func enablePrometheus(
}
afterCtx(ctx, closeWorkspacesFunc)

insightsMetricsCollector := insights.NewMetricsCollector(options.Database, 0)
insightsMetricsCollector, err := insights.NewMetricsCollector(options.Database, options.Logger, 0)
if err != nil {
return nil, xerrors.Errorf("unable to initialize insights metrics collector: %w", err)
}
err = options.PrometheusRegistry.Register(insightsMetricsCollector)
if err != nil {
return nil, xerrors.Errorf("unable to register insights metrics collector: %w", err)
Expand Down
23 changes: 21 additions & 2 deletions coderd/prometheusmetrics/insights/metricscollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"time"

"cdr.dev/slog"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/coderd/database"
)
Expand All @@ -17,20 +19,25 @@ var (

type MetricsCollector struct {
database database.Store
logger slog.Logger
duration time.Duration
}

var _ prometheus.Collector = new(MetricsCollector)

func NewMetricsCollector(db database.Store, duration time.Duration) *MetricsCollector {
func NewMetricsCollector(db database.Store, logger slog.Logger, duration time.Duration) (*MetricsCollector, error) {
if duration == 0 {
duration = 5 * time.Minute
}
if duration < 5*time.Minute {
return nil, xerrors.Errorf("refresh interval must be at least 5 mins")
}

return &MetricsCollector{
database: db,
logger: logger.Named("insights_metrics_collector"),
duration: duration,
}
}, nil
}

func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {
Expand All @@ -42,6 +49,18 @@ func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {
ticker := time.NewTicker(time.Nanosecond)
doTick := func() {
defer ticker.Reset(mc.duration)

now := time.Now()

parameterRows, err := mc.database.GetTemplateInsights(ctx, database.GetTemplateInsightsParams{
StartTime: now.Add(-mc.duration),
EndTime: now,
})
if err != nil {
mc.logger.Error(ctx, "unable to fetch template insights from database: %w", err)
return
}

}

go func() {
Expand Down