Reorganize instrument code
This commit is contained in:
parent
8446c116a8
commit
455fb65e77
4 changed files with 22 additions and 18 deletions
|
@ -2,13 +2,10 @@
|
|||
|
||||
package instrument
|
||||
|
||||
import "time"
|
||||
|
||||
type dummyInst struct{}
|
||||
|
||||
func Init() Instrument {
|
||||
return &dummyInst{}
|
||||
}
|
||||
|
||||
func (i dummyInst) Visit(section string, id string, search string, fmt string) {}
|
||||
func (i dummyInst) Duration(section string, search string, duration time.Duration) {}
|
||||
func (i dummyInst) Request(req RequestData) {}
|
||||
|
|
|
@ -2,7 +2,14 @@ package instrument
|
|||
|
||||
import "time"
|
||||
|
||||
type Instrument interface {
|
||||
Visit(section string, id string, search string, fmt string)
|
||||
Duration(section string, search string, duration time.Duration)
|
||||
type RequestData struct {
|
||||
Section string
|
||||
ID string
|
||||
Search string
|
||||
Fmt string
|
||||
Duration time.Duration
|
||||
}
|
||||
|
||||
type Instrument interface {
|
||||
Request(req RequestData)
|
||||
}
|
||||
|
|
|
@ -3,8 +3,6 @@
|
|||
package instrument
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
log "github.com/cihub/seelog"
|
||||
|
||||
"net/http"
|
||||
|
@ -37,7 +35,7 @@ func Init() Instrument {
|
|||
Name: "trantor_request_duration_seconds",
|
||||
Help: "Duration of the request in seconds.",
|
||||
},
|
||||
[]string{"section", "search"},
|
||||
[]string{"section"},
|
||||
)
|
||||
|
||||
prometheus.MustRegister(visits)
|
||||
|
@ -57,10 +55,7 @@ func promHandle() {
|
|||
log.Error(server.ListenAndServe())
|
||||
}
|
||||
|
||||
func (in promInst) Visit(section string, id string, search string, fmt string) {
|
||||
in.visits.WithLabelValues(section, id, search, fmt).Inc()
|
||||
}
|
||||
|
||||
func (in promInst) Duration(section string, search string, duration time.Duration) {
|
||||
in.reqDur.WithLabelValues(section, search).Observe(duration.Seconds())
|
||||
func (in promInst) Request(req RequestData) {
|
||||
in.visits.WithLabelValues(req.Section, req.ID, req.Search, req.Fmt).Inc()
|
||||
in.reqDur.WithLabelValues(req.Section).Observe(req.Duration.Seconds())
|
||||
}
|
||||
|
|
|
@ -119,8 +119,13 @@ func (sg StatsGatherer) worker() {
|
|||
section = pattern[1]
|
||||
}
|
||||
|
||||
sg.instrument.Visit(section, id, search, fmt)
|
||||
sg.instrument.Duration(section, search, req.duration)
|
||||
sg.instrument.Request(instrument.RequestData{
|
||||
Section: section,
|
||||
ID: id,
|
||||
Search: search,
|
||||
Fmt: fmt,
|
||||
Duration: req.duration
|
||||
})
|
||||
switch section {
|
||||
case "download":
|
||||
err = sg.db.IncDownloads(id)
|
||||
|
|
Reference in a new issue