|
| 1 | +package prebuilds |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"time" |
| 6 | + |
| 7 | +"cdr.dev/slog" |
| 8 | + |
| 9 | +"github.com/prometheus/client_golang/prometheus" |
| 10 | + |
| 11 | +"github.com/coder/coder/v2/coderd/database" |
| 12 | +"github.com/coder/coder/v2/coderd/database/dbauthz" |
| 13 | +"github.com/coder/coder/v2/coderd/prebuilds" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | +labels= []string{"template_name","preset_name","organization_name"} |
| 18 | +createdPrebuildsDesc=prometheus.NewDesc( |
| 19 | +"coderd_prebuilt_workspaces_created_total", |
| 20 | +"Total number of prebuilt workspaces that have been created to meet the desired instance count of each "+ |
| 21 | +"template preset.", |
| 22 | +labels, |
| 23 | +nil, |
| 24 | +) |
| 25 | +failedPrebuildsDesc=prometheus.NewDesc( |
| 26 | +"coderd_prebuilt_workspaces_failed_total", |
| 27 | +"Total number of prebuilt workspaces that failed to build.", |
| 28 | +labels, |
| 29 | +nil, |
| 30 | +) |
| 31 | +claimedPrebuildsDesc=prometheus.NewDesc( |
| 32 | +"coderd_prebuilt_workspaces_claimed_total", |
| 33 | +"Total number of prebuilt workspaces which were claimed by users. Claiming refers to creating a workspace "+ |
| 34 | +"with a preset selected for which eligible prebuilt workspaces are available and one is reassigned to a user.", |
| 35 | +labels, |
| 36 | +nil, |
| 37 | +) |
| 38 | +desiredPrebuildsDesc=prometheus.NewDesc( |
| 39 | +"coderd_prebuilt_workspaces_desired", |
| 40 | +"Target number of prebuilt workspaces that should be available for each template preset.", |
| 41 | +labels, |
| 42 | +nil, |
| 43 | +) |
| 44 | +runningPrebuildsDesc=prometheus.NewDesc( |
| 45 | +"coderd_prebuilt_workspaces_running", |
| 46 | +"Current number of prebuilt workspaces that are in a running state. These workspaces have started "+ |
| 47 | +"successfully but may not yet be claimable by users (see coderd_prebuilt_workspaces_eligible).", |
| 48 | +labels, |
| 49 | +nil, |
| 50 | +) |
| 51 | +eligiblePrebuildsDesc=prometheus.NewDesc( |
| 52 | +"coderd_prebuilt_workspaces_eligible", |
| 53 | +"Current number of prebuilt workspaces that are eligible to be claimed by users. These are workspaces that "+ |
| 54 | +"have completed their build process with their agent reporting 'ready' status.", |
| 55 | +labels, |
| 56 | +nil, |
| 57 | +) |
| 58 | +) |
| 59 | + |
| 60 | +typeMetricsCollectorstruct { |
| 61 | +database database.Store |
| 62 | +logger slog.Logger |
| 63 | +snapshotter prebuilds.StateSnapshotter |
| 64 | +} |
| 65 | + |
| 66 | +var_ prometheus.Collector=new(MetricsCollector) |
| 67 | + |
| 68 | +funcNewMetricsCollector(db database.Store,logger slog.Logger,snapshotter prebuilds.StateSnapshotter)*MetricsCollector { |
| 69 | +return&MetricsCollector{ |
| 70 | +database:db, |
| 71 | +logger:logger.Named("prebuilds_metrics_collector"), |
| 72 | +snapshotter:snapshotter, |
| 73 | +} |
| 74 | +} |
| 75 | + |
| 76 | +func (*MetricsCollector)Describe(descChchan<-*prometheus.Desc) { |
| 77 | +descCh<-createdPrebuildsDesc |
| 78 | +descCh<-failedPrebuildsDesc |
| 79 | +descCh<-claimedPrebuildsDesc |
| 80 | +descCh<-desiredPrebuildsDesc |
| 81 | +descCh<-runningPrebuildsDesc |
| 82 | +descCh<-eligiblePrebuildsDesc |
| 83 | +} |
| 84 | + |
| 85 | +func (mc*MetricsCollector)Collect(metricsChchan<- prometheus.Metric) { |
| 86 | +// nolint:gocritic // We need to set an authz context to read metrics from the db. |
| 87 | +ctx,cancel:=context.WithTimeout(dbauthz.AsPrebuildsOrchestrator(context.Background()),10*time.Second) |
| 88 | +defercancel() |
| 89 | +prebuildMetrics,err:=mc.database.GetPrebuildMetrics(ctx) |
| 90 | +iferr!=nil { |
| 91 | +mc.logger.Error(ctx,"failed to get prebuild metrics",slog.Error(err)) |
| 92 | +return |
| 93 | +} |
| 94 | + |
| 95 | +for_,metric:=rangeprebuildMetrics { |
| 96 | +metricsCh<-prometheus.MustNewConstMetric(createdPrebuildsDesc,prometheus.CounterValue,float64(metric.CreatedCount),metric.TemplateName,metric.PresetName,metric.OrganizationName) |
| 97 | +metricsCh<-prometheus.MustNewConstMetric(failedPrebuildsDesc,prometheus.CounterValue,float64(metric.FailedCount),metric.TemplateName,metric.PresetName,metric.OrganizationName) |
| 98 | +metricsCh<-prometheus.MustNewConstMetric(claimedPrebuildsDesc,prometheus.CounterValue,float64(metric.ClaimedCount),metric.TemplateName,metric.PresetName,metric.OrganizationName) |
| 99 | +} |
| 100 | + |
| 101 | +snapshot,err:=mc.snapshotter.SnapshotState(ctx,mc.database) |
| 102 | +iferr!=nil { |
| 103 | +mc.logger.Error(ctx,"failed to get latest prebuild state",slog.Error(err)) |
| 104 | +return |
| 105 | +} |
| 106 | + |
| 107 | +for_,preset:=rangesnapshot.Presets { |
| 108 | +if!preset.UsingActiveVersion { |
| 109 | +continue |
| 110 | +} |
| 111 | + |
| 112 | +presetSnapshot,err:=snapshot.FilterByPreset(preset.ID) |
| 113 | +iferr!=nil { |
| 114 | +mc.logger.Error(ctx,"failed to filter by preset",slog.Error(err)) |
| 115 | +continue |
| 116 | +} |
| 117 | +state:=presetSnapshot.CalculateState() |
| 118 | + |
| 119 | +metricsCh<-prometheus.MustNewConstMetric(desiredPrebuildsDesc,prometheus.GaugeValue,float64(state.Desired),preset.TemplateName,preset.Name,preset.OrganizationName) |
| 120 | +metricsCh<-prometheus.MustNewConstMetric(runningPrebuildsDesc,prometheus.GaugeValue,float64(state.Actual),preset.TemplateName,preset.Name,preset.OrganizationName) |
| 121 | +metricsCh<-prometheus.MustNewConstMetric(eligiblePrebuildsDesc,prometheus.GaugeValue,float64(state.Eligible),preset.TemplateName,preset.Name,preset.OrganizationName) |
| 122 | +} |
| 123 | +} |