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
Prev Previous commit
Next Next commit
DB calls
  • Loading branch information
mtojek committed Oct 17, 2023
commit a3c29a7471e77ceb2501608cb01dd908ce05f6cc
56 changes: 51 additions & 5 deletions coderd/prometheusmetrics/insights/metricscollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"

"cdr.dev/slog"
Expand Down Expand Up @@ -52,19 +53,61 @@ func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {
defer ticker.Reset(mc.duration)

now := time.Now()
startTime := now.Add(-mc.duration)
endTime := now

// TODO collect iteration time

parameterRows, err := mc.database.GetTemplateInsights(ctx, database.GetTemplateInsightsParams{
StartTime: now.Add(-mc.duration),
EndTime: now,
var templateInsights database.GetTemplateInsightsRow
var appInsights []database.GetTemplateAppInsightsRow
var parameterInsights []database.GetTemplateParameterInsightsRow

// Phase I: Fetch insights from database
eg, egCtx := errgroup.WithContext(ctx)
eg.SetLimit(3)

eg.Go(func() error {
var err error
templateInsights, err = mc.database.GetTemplateInsights(egCtx, database.GetTemplateInsightsParams{
StartTime: startTime,
EndTime: endTime,
})
if err != nil {
mc.logger.Error(ctx, "unable to fetch template insights from database", slog.Error(err))
}
return err
})
eg.Go(func() error {
var err error
appInsights, err = mc.database.GetTemplateAppInsights(ctx, database.GetTemplateAppInsightsParams{
StartTime: startTime,
EndTime: endTime,
})
if err != nil {
mc.logger.Error(ctx, "unable to fetch app insights from database", slog.Error(err))
}
return err
})
eg.Go(func() error {
var err error
parameterInsights, err = mc.database.GetTemplateParameterInsights(ctx, database.GetTemplateParameterInsightsParams{
StartTime: startTime,
EndTime: endTime,
})
if err != nil {
mc.logger.Error(ctx, "unable to fetch parameter insights from database", slog.Error(err))
}
return err
})

err := eg.Wait()
if err != nil {
mc.logger.Error(ctx, "unable to fetch template insights from database", slog.Error(err))
return
}

mc.logger.Info(ctx, "debug", slog.F("parameter_rows", parameterRows))
// Phase 2: Collect resource IDs (templates, applications, parameters), and fetch relevant details

// TODO
}

go func() {
Expand Down Expand Up @@ -92,4 +135,7 @@ func (*MetricsCollector) Describe(descCh chan<- *prometheus.Desc) {
}

func (mc *MetricsCollector) Collect(metricsCh chan<- prometheus.Metric) {
// Phase 3: Collect metrics

// TODO
}