VictoriaMetrics/metrics

how to replace prometheus Histogram and Summary counter

Closed this issue · 4 comments

I want to switch to tis package, but don't understand from docs how to do that i'm already have in prometheus:

timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {       
  us := v * 1000000 // make microseconds                                     
  timeCounterSummary.WithLabelValues(name).Observe(us)                       
  timeCounterHistogram.WithLabelValues(name).Observe(v)                      
}))                                                                          
defer timer.ObserveDuration()                                                

how can i do that in this package?

Hi @vtolstov! You can try following:

        startTime := time.Now()
	defer func() {
		us := time.Since(startTime).Microseconds()
		metrics.GetOrCreateSummary(name).Update(float64(us))
		metrics.GetOrCreateHistogram(name).UpdateDuration(startTime)
	}()

thanks, why you use UpdateDuration and not update? i'm write something like this, does it looks right?

 timeCounterSummary := metrics.GetOrCreateSummary(                                              
   getName("upstream_latency_microseconds", map[string]interface{}{"method": name}),            
 )                                                                                              
 timeCounterHistogram := metrics.GetOrCreateSummary(                                            
   getName("request_duration_seconds", map[string]interface{}{"method": name}),                 
 )                                                                                              
                                                                                                
 ts := time.Now()                                                                               
 err := fn(ctx, req, rsp)                                                                       
 te := time.Since(ts)                                                                           
                                                                                                
 timeCounterSummary.Update(float64(te.Microseconds()))                                          
 timeCounterHistogram.Update(te.Seconds())                                                      

why you use UpdateDuration and not update?

UpdateDuration does h.Update(time.Since(startTime).Seconds()) under the hood and in your example you want to observe microseconds. However, it is recommended to always use basic types like seconds.

i'm write something like this, does it looks right?

Yes, looks good to me.

thanks!