Skip to content

Commit

Permalink
fix metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kavehmz committed Feb 4, 2019
1 parent 140b1ce commit 4016e6b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
16 changes: 14 additions & 2 deletions serve/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,22 @@ import (
var errTimeout = errors.New(`Operation timedout`)

func (m *MathHandler) calculate(f formula, b []byte) ([]byte, error) {
f.metrics.accesses.Inc()
timer := prometheus.NewTimer(f.metrics.calcTime)
defer timer.ObserveDuration()

var res []byte
var err error
if f.opt.Immutable() {
return m.calculateImmutable(f, b)
res, err = m.calculateImmutable(f, b)
} else {
res, err = m.calculatemutable(f, b)

}
if err != nil {
f.metrics.errors.Inc()
}
return m.calculatemutable(f, b)
return res, err
}

func (m *MathHandler) calculateImmutable(f formula, b []byte) ([]byte, error) {
Expand Down
4 changes: 0 additions & 4 deletions serve/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,12 @@ func (m *MathHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

if f, ok := m.formulas[path.Clean(r.URL.Path)]; ok {
f.metrics.accesses.Inc()

result, err := m.calculate(f, b)
if err == errTimeout {
f.metrics.errors.Inc()
httpError(w, err.Error(), http.StatusRequestTimeout)
return
}
if err != nil {
f.metrics.errors.Inc()
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
Expand Down
1 change: 1 addition & 0 deletions serve/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestMathHandler_ServeHTTP_app_err(t *testing.T) {
t.Error("Expected error", resp.StatusCode)
}
}

func TestMathHandler_ServeHTTP_timeout(t *testing.T) {
m := MathHandler{timeout: time.Nanosecond}
m.formulas = make(map[string]formula)
Expand Down
6 changes: 6 additions & 0 deletions serve/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type metrics struct {
errors prometheus.Counter
cacheMisses prometheus.Counter
cacheSize prometheus.Gauge
cacheTime prometheus.Histogram
calcTime prometheus.Histogram
}

Expand All @@ -26,6 +27,10 @@ func register(name string) *metrics {
cacheSize := promauto.NewGauge(prometheus.GaugeOpts{
Name: name + "_cache_size",
})
cacheTime := promauto.NewHistogram(prometheus.HistogramOpts{
Name: name + "_cache_time",
Buckets: prometheus.ExponentialBuckets(0.5e-3, 2, 14),
})
calcTime := promauto.NewHistogram(prometheus.HistogramOpts{
Name: name + "_calculation_time",
Buckets: prometheus.ExponentialBuckets(0.5e-3, 2, 14),
Expand All @@ -36,6 +41,7 @@ func register(name string) *metrics {
errors: errors,
cacheMisses: cacheMisses,
cacheSize: cacheSize,
cacheTime: cacheTime,
calcTime: calcTime,
}
}

0 comments on commit 4016e6b

Please sign in to comment.