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