Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f76ef98

Browse files
authored
chore!: Standardize prometheus time metrics to seconds (#5709)
* chore!: Standardize prometheus time metrics to seconds * Update prometheus docs
1 parent f91a0d8 commit f76ef98

File tree

5 files changed

+650
-524
lines changed

5 files changed

+650
-524
lines changed

coderd/httpmw/prometheus.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ import (
1414
"github.com/prometheus/client_golang/prometheus/promauto"
1515
)
1616

17-
func durationToFloatMs(d time.Duration) float64 {
18-
return float64(d.Milliseconds())
19-
}
20-
2117
func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler {
2218
factory := promauto.With(register)
2319
requestsProcessed := factory.NewCounterVec(prometheus.CounterOpts{
@@ -30,34 +26,34 @@ func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler
3026
Namespace: "coderd",
3127
Subsystem: "api",
3228
Name: "concurrent_requests",
33-
Help: "The number of concurrent API requests",
29+
Help: "The number of concurrent API requests.",
3430
})
3531
websocketsConcurrent := factory.NewGauge(prometheus.GaugeOpts{
3632
Namespace: "coderd",
3733
Subsystem: "api",
3834
Name: "concurrent_websockets",
39-
Help: "The total number of concurrent API websockets",
35+
Help: "The total number of concurrent API websockets.",
4036
})
4137
websocketsDist := factory.NewHistogramVec(prometheus.HistogramOpts{
4238
Namespace: "coderd",
4339
Subsystem: "api",
44-
Name: "websocket_durations_ms",
45-
Help: "Websocket duration distribution of requests in milliseconds",
40+
Name: "websocket_durations_seconds",
41+
Help: "Websocket duration distribution of requests in seconds.",
4642
Buckets: []float64{
47-
durationToFloatMs(01 * time.Millisecond),
48-
durationToFloatMs(01 * time.Second),
49-
durationToFloatMs(01 * time.Minute),
50-
durationToFloatMs(01 * time.Hour),
51-
durationToFloatMs(15 * time.Hour),
52-
durationToFloatMs(30 * time.Hour),
43+
0.001, // 1ms
44+
1,
45+
60, // 1 minute
46+
60 * 60, // 1 hour
47+
60 * 60 * 15, // 15 hours
48+
60 * 60 * 30, // 30 hours
5349
},
5450
}, []string{"path"})
5551
requestsDist := factory.NewHistogramVec(prometheus.HistogramOpts{
5652
Namespace: "coderd",
5753
Subsystem: "api",
58-
Name: "request_latencies_ms",
59-
Help: "Latency distribution of requests in milliseconds",
60-
Buckets: []float64{1, 5, 10, 25, 50, 100, 500, 1000, 5000, 10000, 30000},
54+
Name: "request_latencies_seconds",
55+
Help: "Latency distribution of requests in seconds.",
56+
Buckets: []float64{0.001, 0.005, 0.010, 0.025, 0.050, 0.100, 0.500, 1, 5, 10, 30},
6157
}, []string{"method", "path"})
6258

6359
return func(next http.Handler) http.Handler {
@@ -98,7 +94,7 @@ func Prometheus(register prometheus.Registerer) func(http.Handler) http.Handler
9894
statusStr := strconv.Itoa(sw.Status)
9995

10096
requestsProcessed.WithLabelValues(statusStr, method, path).Inc()
101-
dist.WithLabelValues(distOpts...).Observe(float64(time.Since(start)) / 1e6)
97+
dist.WithLabelValues(distOpts...).Observe(time.Since(start).Seconds())
10298
})
10399
}
104100
}

docs/admin/prometheus.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ The environment variable `CODER_PROMETHEUS_ENABLE` will be enabled automatically
3232
| Name | Type | Description | Labels |
3333
| -------------------------------------------- | --------- | ------------------------------------------------------------------ | ---------------------- |
3434
| `coderd_api_active_users_duration_hour` | gauge | The number of users that have been active within the last hour. | |
35-
| `coderd_api_concurrent_requests` | gauge | The number of concurrent API requests | |
36-
| `coderd_api_concurrent_websockets` | gauge | The total number of concurrent API websockets | |
37-
| `coderd_api_request_latencies_ms` | histogram | Latency distribution of requests in milliseconds | `method` `path` |
35+
| `coderd_api_concurrent_requests` | gauge | The number of concurrent API requests. | |
36+
| `coderd_api_concurrent_websockets` | gauge | The total number of concurrent API websockets. | |
37+
| `coderd_api_request_latencies_seconds` | histogram | Latency distribution of requests in seconds. | `method` `path` |
3838
| `coderd_api_requests_processed_total` | counter | The total number of processed API requests | `code` `method` `path` |
39-
| `coderd_api_websocket_durations_ms` | histogram | Websocket duration distribution of requests in milliseconds | `path` |
39+
| `coderd_api_websocket_durations_seconds` | histogram | Websocket duration distribution of requests in seconds. | `path` |
4040
| `coderd_api_workspace_latest_build_total` | gauge | The latest workspace builds with a status. | `status` |
41-
| `coderd_provisionerd_job_timings_ms` | histogram | The provisioner job time duration. | `provisioner` `status` |
41+
| `coderd_provisionerd_job_timings_seconds` | histogram | The provisioner job time duration in seconds. | `provisioner` `status` |
4242
| `coderd_provisionerd_jobs_current` | gauge | The number of currently running provisioner jobs. | `provisioner` |
4343
| `go_gc_duration_seconds` | summary | A summary of the pause duration of garbage collection cycles. | |
4444
| `go_goroutines` | gauge | Number of goroutines that currently exist. | |
@@ -67,6 +67,13 @@ The environment variable `CODER_PROMETHEUS_ENABLE` will be enabled automatically
6767
| `go_memstats_stack_sys_bytes` | gauge | Number of bytes obtained from system for stack allocator. | |
6868
| `go_memstats_sys_bytes` | gauge | Number of bytes obtained from system. | |
6969
| `go_threads` | gauge | Number of OS threads created. | |
70+
| `process_cpu_seconds_total` | counter | Total user and system CPU time spent in seconds. | |
71+
| `process_max_fds` | gauge | Maximum number of open file descriptors. | |
72+
| `process_open_fds` | gauge | Number of open file descriptors. | |
73+
| `process_resident_memory_bytes` | gauge | Resident memory size in bytes. | |
74+
| `process_start_time_seconds` | gauge | Start time of the process since unix epoch in seconds. | |
75+
| `process_virtual_memory_bytes` | gauge | Virtual memory size in bytes. | |
76+
| `process_virtual_memory_max_bytes` | gauge | Maximum amount of virtual memory available in bytes. | |
7077
| `promhttp_metric_handler_requests_in_flight` | gauge | Current number of scrapes being served. | |
7178
| `promhttp_metric_handler_requests_total` | counter | Total number of scrapes by HTTP status code. | `code` |
7279

provisionerd/provisionerd.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ type Metrics struct {
132132

133133
func NewMetrics(reg prometheus.Registerer) Metrics {
134134
auto := promauto.With(reg)
135-
durationToFloatMs := func(d time.Duration) float64 {
136-
return float64(d.Milliseconds())
137-
}
138135

139136
return Metrics{
140137
Runner: runner.Metrics{
@@ -147,17 +144,17 @@ func NewMetrics(reg prometheus.Registerer) Metrics {
147144
JobTimings: auto.NewHistogramVec(prometheus.HistogramOpts{
148145
Namespace: "coderd",
149146
Subsystem: "provisionerd",
150-
Name: "job_timings_ms",
151-
Help: "The provisioner job time duration.",
147+
Name: "job_timings_seconds",
148+
Help: "The provisioner job time duration in seconds.",
152149
Buckets: []float64{
153-
durationToFloatMs(1 * time.Second),
154-
durationToFloatMs(10 * time.Second),
155-
durationToFloatMs(30 * time.Second),
156-
durationToFloatMs(1 * time.Minute),
157-
durationToFloatMs(5 * time.Minute),
158-
durationToFloatMs(10 * time.Minute),
159-
durationToFloatMs(30 * time.Minute),
160-
durationToFloatMs(1 * time.Hour),
150+
1, // 1s
151+
10,
152+
30,
153+
60, // 1min
154+
60 * 5,
155+
60 * 10,
156+
60 * 30, // 30min
157+
60 * 60, // 1hr
161158
},
162159
}, []string{"provisioner", "status"}),
163160
},

provisionerd/runner/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (r *Runner) Run() {
163163
}
164164

165165
concurrentGauge.Dec()
166-
r.metrics.JobTimings.WithLabelValues(r.job.Provisioner, status).Observe(float64(time.Since(start).Milliseconds()))
166+
r.metrics.JobTimings.WithLabelValues(r.job.Provisioner, status).Observe(time.Since(start).Seconds())
167167
}()
168168

169169
r.mutex.Lock()

0 commit comments

Comments
 (0)