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
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
WIP
  • Loading branch information
mtojek committed Oct 17, 2023
commit 24ae33227e513e858ee7cedf26fad9525bc9cd57
55 changes: 51 additions & 4 deletions coderd/prometheusmetrics/insights/metricscollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -58,7 +59,7 @@ func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {

// TODO collect iteration time

var templateInsights database.GetTemplateInsightsRow
var userActivity []database.GetUserActivityInsightsRow
var appInsights []database.GetTemplateAppInsightsRow
var parameterInsights []database.GetTemplateParameterInsightsRow

Expand All @@ -68,7 +69,7 @@ func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {

eg.Go(func() error {
var err error
templateInsights, err = mc.database.GetTemplateInsights(egCtx, database.GetTemplateInsightsParams{
userActivity, err = mc.database.GetUserActivityInsights(egCtx, database.GetUserActivityInsightsParams{
StartTime: startTime,
EndTime: endTime,
})
Expand Down Expand Up @@ -105,9 +106,17 @@ func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {
return
}

// Phase 2: Collect resource IDs (templates, applications, parameters), and fetch relevant details
// Phase 2: Collect template IDs, and fetch relevant details
templateIDs := uniqueTemplateIDs(userActivity, appInsights, parameterInsights)
templates, err := mc.database.GetTemplatesWithFilter(ctx, database.GetTemplatesWithFilterParams{
IDs: templateIDs,
})
if err != nil {
mc.logger.Error(ctx, "unable to fetch template details from database", slog.Error(err))
return
}

// TODO
templateNames := onlyTemplateNames(templates)
}

go func() {
Expand Down Expand Up @@ -139,3 +148,41 @@ func (mc *MetricsCollector) Collect(metricsCh chan<- prometheus.Metric) {

// TODO
}

// Helper functions below.

func uniqueTemplateIDs(userActivity []database.GetUserActivityInsightsRow, appInsights []database.GetTemplateAppInsightsRow, parameterInsights []database.GetTemplateParameterInsightsRow) []uuid.UUID {
tids := map[uuid.UUID]bool{}
for _, t := range userActivity {
for _, tid := range t.TemplateIDs {
tids[tid] = true
}
}

for _, a := range appInsights {
for _, tid := range a.TemplateIDs {
tids[tid] = true
}
}

for _, p := range parameterInsights {
for _, tid := range p.TemplateIDs {
tids[tid] = true
}
}

uniqueUUIDs := make([]uuid.UUID, len(tids))
var i int
for t := range tids {
uniqueUUIDs[i] = t
}
return uniqueUUIDs
}

func onlyTemplateNames(templates []database.Template) map[uuid.UUID]string {
m := map[uuid.UUID]string{}
for _, t := range templates {
m[t.ID] = t.Name
}
return m
}