Add request_duration_seconds metric #72

Merged
mrngm merged 3 commits from metrics into master 2021-05-16 21:10:49 +02:00
1 changed files with 16 additions and 13 deletions
Showing only changes of commit 306705cb28 - Show all commits

View File

@ -87,19 +87,22 @@ func (mh *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (mh *MetricsHandler) updateMetrics() {
// Update metricURLsTotalGauge
electricdusk marked this conversation as resolved Outdated
Outdated
Review

Is it possible to query the database once, yielding all counts for all types and states?

Is it possible to query the database once, yielding all counts for all types and states?

I tried to find this, but thought it was not not possible. I have retried searching for this and found this: https://stackoverflow.com/a/19046871/5207081
That would fix this.

I tried to find this, but thought it was not not possible. I have retried searching for this and found this: https://stackoverflow.com/a/19046871/5207081 That would fix this.
Outdated
Review

Ran this query on the test migration database:

sqlite> SELECT type, state, COUNT(*) FROM pastes GROUP BY type, state;
0|2|136  // type: undef      state: deleted
2|1|118  // type: redirect   state: present
3|1|1376 // type: fileupload state: present

Would that roughly be what you expect to get here?

Ran this query on the test migration database: ``` sqlite> SELECT type, state, COUNT(*) FROM pastes GROUP BY type, state; 0|2|136 // type: undef state: deleted 2|1|118 // type: redirect state: present 3|1|1376 // type: fileupload state: present ``` Would that roughly be what you expect to get here?
for state := db.PasteStateUndef; state <= db.PasteStateDeleted; state++ {
for ty := db.PasteTypeUndef; ty <= db.PasteTypeFileUpload; ty++ {
var count int64
query := mh.db.Unscoped().Model(&db.Paste{}).Where("type = ? AND state = ?", ty, state).Count(&count)
if err := query.Error; err != nil {
log.Printf("error: %v", errors.Wrap(err, "fetching pastes_total metric"))
return
}
labels := map[string]string{
"state": state.String(),
"type": ty.String(),
}
metricURLsTotalGauge.With(labels).Set(float64(count))
results := make([](struct {
Type db.PasteType
State db.PasteState
Count float64
}), 0)
query := mh.db.Unscoped().Model(&db.Paste{}).Select("type", "state", "COUNT(*) as count").Group("type, state").Find(&results)
if err := query.Error; err != nil {
log.Printf("error: %v", errors.Wrap(err, "fetching pastes_total metric"))
return
}
metricURLsTotalGauge.Reset()
for _, r := range results {
labels := map[string]string{
"type": r.Type.String(),
"state": r.State.String(),
}
metricURLsTotalGauge.With(labels).Set(r.Count)
}
}