- Notifications
You must be signed in to change notification settings - Fork905
feat: add prebuilds metrics collector#17547
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
e8ff926
Add metrics collector
dannykopping7b78523
Renaming prebuilds to "prebuilt workspaces" in metrics
dannykopping547c1b0
Eligibility tests
dannykopping131307a
Review & linter feedback
dannykopping7287bd8
Reword metric descriptions for clarity
dannykopping1d66ade
make fmt
dannykoppingFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletionscoderd/prebuilds/api.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletionenterprise/coderd/coderd.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletionsenterprise/coderd/prebuilds/claim_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletionsenterprise/coderd/prebuilds/metricscollector.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package prebuilds | ||
import ( | ||
"context" | ||
"time" | ||
"cdr.dev/slog" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/dbauthz" | ||
"github.com/coder/coder/v2/coderd/prebuilds" | ||
) | ||
var ( | ||
labels = []string{"template_name", "preset_name", "organization_name"} | ||
createdPrebuildsDesc = prometheus.NewDesc( | ||
"coderd_prebuilt_workspaces_created_total", | ||
"Total number of prebuilt workspaces that have been created to meet the desired instance count of each "+ | ||
"template preset.", | ||
labels, | ||
nil, | ||
) | ||
failedPrebuildsDesc = prometheus.NewDesc( | ||
"coderd_prebuilt_workspaces_failed_total", | ||
"Total number of prebuilt workspaces that failed to build.", | ||
labels, | ||
nil, | ||
) | ||
claimedPrebuildsDesc = prometheus.NewDesc( | ||
"coderd_prebuilt_workspaces_claimed_total", | ||
"Total number of prebuilt workspaces which were claimed by users. Claiming refers to creating a workspace "+ | ||
"with a preset selected for which eligible prebuilt workspaces are available and one is reassigned to a user.", | ||
labels, | ||
nil, | ||
) | ||
desiredPrebuildsDesc = prometheus.NewDesc( | ||
"coderd_prebuilt_workspaces_desired", | ||
"Target number of prebuilt workspaces that should be available for each template preset.", | ||
labels, | ||
nil, | ||
) | ||
runningPrebuildsDesc = prometheus.NewDesc( | ||
"coderd_prebuilt_workspaces_running", | ||
"Current number of prebuilt workspaces that are in a running state. These workspaces have started "+ | ||
"successfully but may not yet be claimable by users (see coderd_prebuilt_workspaces_eligible).", | ||
labels, | ||
nil, | ||
) | ||
eligiblePrebuildsDesc = prometheus.NewDesc( | ||
"coderd_prebuilt_workspaces_eligible", | ||
"Current number of prebuilt workspaces that are eligible to be claimed by users. These are workspaces that "+ | ||
"have completed their build process with their agent reporting 'ready' status.", | ||
labels, | ||
nil, | ||
) | ||
) | ||
type MetricsCollector struct { | ||
database database.Store | ||
logger slog.Logger | ||
snapshotter prebuilds.StateSnapshotter | ||
} | ||
var _ prometheus.Collector = new(MetricsCollector) | ||
func NewMetricsCollector(db database.Store, logger slog.Logger, snapshotter prebuilds.StateSnapshotter) *MetricsCollector { | ||
return &MetricsCollector{ | ||
database: db, | ||
logger: logger.Named("prebuilds_metrics_collector"), | ||
snapshotter: snapshotter, | ||
} | ||
} | ||
func (*MetricsCollector) Describe(descCh chan<- *prometheus.Desc) { | ||
descCh <- createdPrebuildsDesc | ||
descCh <- failedPrebuildsDesc | ||
descCh <- claimedPrebuildsDesc | ||
descCh <- desiredPrebuildsDesc | ||
descCh <- runningPrebuildsDesc | ||
descCh <- eligiblePrebuildsDesc | ||
} | ||
func (mc *MetricsCollector) Collect(metricsCh chan<- prometheus.Metric) { | ||
// nolint:gocritic // We need to set an authz context to read metrics from the db. | ||
ctx, cancel := context.WithTimeout(dbauthz.AsPrebuildsOrchestrator(context.Background()), 10*time.Second) | ||
defer cancel() | ||
prebuildMetrics, err := mc.database.GetPrebuildMetrics(ctx) | ||
if err != nil { | ||
mc.logger.Error(ctx, "failed to get prebuild metrics", slog.Error(err)) | ||
return | ||
} | ||
for _, metric := range prebuildMetrics { | ||
metricsCh <- prometheus.MustNewConstMetric(createdPrebuildsDesc, prometheus.CounterValue, float64(metric.CreatedCount), metric.TemplateName, metric.PresetName, metric.OrganizationName) | ||
metricsCh <- prometheus.MustNewConstMetric(failedPrebuildsDesc, prometheus.CounterValue, float64(metric.FailedCount), metric.TemplateName, metric.PresetName, metric.OrganizationName) | ||
metricsCh <- prometheus.MustNewConstMetric(claimedPrebuildsDesc, prometheus.CounterValue, float64(metric.ClaimedCount), metric.TemplateName, metric.PresetName, metric.OrganizationName) | ||
} | ||
snapshot, err := mc.snapshotter.SnapshotState(ctx, mc.database) | ||
if err != nil { | ||
mc.logger.Error(ctx, "failed to get latest prebuild state", slog.Error(err)) | ||
return | ||
} | ||
for _, preset := range snapshot.Presets { | ||
if !preset.UsingActiveVersion { | ||
continue | ||
} | ||
presetSnapshot, err := snapshot.FilterByPreset(preset.ID) | ||
if err != nil { | ||
mc.logger.Error(ctx, "failed to filter by preset", slog.Error(err)) | ||
continue | ||
} | ||
state := presetSnapshot.CalculateState() | ||
metricsCh <- prometheus.MustNewConstMetric(desiredPrebuildsDesc, prometheus.GaugeValue, float64(state.Desired), preset.TemplateName, preset.Name, preset.OrganizationName) | ||
metricsCh <- prometheus.MustNewConstMetric(runningPrebuildsDesc, prometheus.GaugeValue, float64(state.Actual), preset.TemplateName, preset.Name, preset.OrganizationName) | ||
metricsCh <- prometheus.MustNewConstMetric(eligiblePrebuildsDesc, prometheus.GaugeValue, float64(state.Eligible), preset.TemplateName, preset.Name, preset.OrganizationName) | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.