Add request_duration_seconds metric #72
							
								
								
									
										29
									
								
								metrics.go
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								metrics.go
									
									
									
									
									
								
							@ -87,19 +87,22 @@ func (mh *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (mh *MetricsHandler) updateMetrics() {
 | 
					func (mh *MetricsHandler) updateMetrics() {
 | 
				
			||||||
	// Update metricURLsTotalGauge
 | 
						// Update metricURLsTotalGauge
 | 
				
			||||||
| 
						
							
	
	
	
	
	
	
	
	 
					
					electricdusk marked this conversation as resolved
					
						
						
							Outdated
						
					
				 
				 | 
				|||||||
	for state := db.PasteStateUndef; state <= db.PasteStateDeleted; state++ {
 | 
						results := make([](struct {
 | 
				
			||||||
		for ty := db.PasteTypeUndef; ty <= db.PasteTypeFileUpload; ty++ {
 | 
							Type  db.PasteType
 | 
				
			||||||
			var count int64
 | 
							State db.PasteState
 | 
				
			||||||
			query := mh.db.Unscoped().Model(&db.Paste{}).Where("type = ? AND state = ?", ty, state).Count(&count)
 | 
							Count float64
 | 
				
			||||||
			if err := query.Error; err != nil {
 | 
						}), 0)
 | 
				
			||||||
				log.Printf("error: %v", errors.Wrap(err, "fetching pastes_total metric"))
 | 
						query := mh.db.Unscoped().Model(&db.Paste{}).Select("type", "state", "COUNT(*) as count").Group("type, state").Find(&results)
 | 
				
			||||||
				return
 | 
						if err := query.Error; err != nil {
 | 
				
			||||||
			}
 | 
							log.Printf("error: %v", errors.Wrap(err, "fetching pastes_total metric"))
 | 
				
			||||||
			labels := map[string]string{
 | 
							return
 | 
				
			||||||
				"state": state.String(),
 | 
						}
 | 
				
			||||||
				"type":  ty.String(),
 | 
						metricURLsTotalGauge.Reset()
 | 
				
			||||||
			}
 | 
						for _, r := range results {
 | 
				
			||||||
			metricURLsTotalGauge.With(labels).Set(float64(count))
 | 
							labels := map[string]string{
 | 
				
			||||||
 | 
								"type":  r.Type.String(),
 | 
				
			||||||
 | 
								"state": r.State.String(),
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							metricURLsTotalGauge.With(labels).Set(r.Count)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	
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.
Ran this query on the test migration database:
Would that roughly be what you expect to get here?