Skip to content

Commit

Permalink
[query] Add latency metrics to remote reads (#2027)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnikola authored Oct 30, 2019
1 parent 4a114d4 commit 1461ce6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/query/api/v1/handler/prometheus/native/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ func NewPromReadHandler(
keepNans bool,
instrumentOpts instrument.Options,
) *PromReadHandler {
taggedScope := instrumentOpts.MetricsScope().
Tagged(map[string]string{"handler": "native-read"})
h := &PromReadHandler{
engine: engine,
fetchOptionsBuilder: fetchOptionsBuilder,
tagOpts: tagOpts,
limitsCfg: limitsCfg,
promReadMetrics: newPromReadMetrics(instrumentOpts.MetricsScope()),
promReadMetrics: newPromReadMetrics(taggedScope),
timeoutOps: timeoutOpts,
keepNans: keepNans,
instrumentOpts: instrumentOpts,
Expand All @@ -134,7 +136,6 @@ func NewPromReadHandler(

func (h *PromReadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
timer := h.promReadMetrics.fetchTimerSuccess.Start()

fetchOpts, rErr := h.fetchOptionsBuilder.NewFetchOptions(r)
if rErr != nil {
xhttp.Error(w, rErr.Inner(), rErr.Code())
Expand Down
8 changes: 7 additions & 1 deletion src/query/api/v1/handler/prometheus/remote/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ func NewPromReadHandler(
keepEmpty bool,
instrumentOpts instrument.Options,
) http.Handler {
taggedScope := instrumentOpts.MetricsScope().
Tagged(map[string]string{"handler": "remote-read"})
return &PromReadHandler{
engine: engine,
promReadMetrics: newPromReadMetrics(instrumentOpts.MetricsScope()),
promReadMetrics: newPromReadMetrics(taggedScope),
timeoutOpts: timeoutOpts,
fetchOptionsBuilder: fetchOptionsBuilder,
keepEmpty: keepEmpty,
Expand All @@ -84,6 +86,7 @@ type promReadMetrics struct {
fetchSuccess tally.Counter
fetchErrorsServer tally.Counter
fetchErrorsClient tally.Counter
fetchTimerSuccess tally.Timer
}

func newPromReadMetrics(scope tally.Scope) promReadMetrics {
Expand All @@ -94,10 +97,12 @@ func newPromReadMetrics(scope tally.Scope) promReadMetrics {
Counter("fetch.errors"),
fetchErrorsClient: scope.Tagged(map[string]string{"code": "4XX"}).
Counter("fetch.errors"),
fetchTimerSuccess: scope.Timer("fetch.success.latency"),
}
}

func (h *PromReadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
timer := h.promReadMetrics.fetchTimerSuccess.Start()
ctx := context.WithValue(r.Context(), handler.HeaderKey, r.Header)
logger := logging.WithContext(ctx, h.instrumentOpts)
req, rErr := h.parseRequest(r)
Expand Down Expand Up @@ -152,6 +157,7 @@ func (h *PromReadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

timer.Stop()
h.promReadMetrics.fetchSuccess.Inc(1)
}

Expand Down

0 comments on commit 1461ce6

Please sign in to comment.