Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let metrics libs handle the atomicity #5738

Merged
merged 6 commits into from Feb 11, 2020
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 11 additions & 13 deletions pkg/middlewares/metrics/metrics.go
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"strconv"
"strings"
"sync/atomic"
"time"
"unicode/utf8"

Expand All @@ -27,9 +26,6 @@ const (
)

type metricsMiddleware struct {
// Important: Since this int64 field is using sync/atomic, it has to be at the top of the struct due to a bug on 32-bit platform
// See: https://golang.org/pkg/sync/atomic/ for more information
openConns int64
next http.Handler
reqsCounter gokitmetrics.Counter
reqDurationHistogram gokitmetrics.Histogram
Expand Down Expand Up @@ -78,23 +74,25 @@ func WrapServiceHandler(ctx context.Context, registry metrics.Registry, serviceN
}

func (m *metricsMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
labels := []string{"method", getMethod(req), "protocol", getRequestProtocol(req)}
// Initialize labels slice with correct size
labels := make([]string, 0, 2*len([]string{"method", "protocol", "code"})+len(m.baseLabels))
labels = append(labels, m.baseLabels...)
// Adding 4 entries to labels
labels = append(labels, []string{"method", getMethod(req), "protocol", getRequestProtocol(req)}...)

openConns := atomic.AddInt64(&m.openConns, 1)
m.openConnsGauge.With(labels...).Set(float64(openConns))
defer func(labelValues []string) {
openConns := atomic.AddInt64(&m.openConns, -1)
m.openConnsGauge.With(labelValues...).Set(float64(openConns))
}(labels)
m.openConnsGauge.With(labels...).Add(1)
defer m.openConnsGauge.With(labels...).Add(-1)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I restored the defer call instead of moving m.openConnsGauge.With(labels...).Add(-1) after m.next.ServeHTTP(recorder, req) because I've a scenario where, when doing a load testing of an SSE endpoint with rate limiting activated, we never go past m.next.ServeHTTP(recorder, req) and the Gauge is never decremented.

@mpl if you have an idea why I would be really interested.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, that suggests m.next.ServeHTTP never returns, either because of a deadlock or a blocked channel, does it not?


start := time.Now()
recorder := newResponseRecorder(rw)
start := time.Now()
m.next.ServeHTTP(recorder, req)
duration := time.Since(start).Seconds()

// Adding 2 entries to labels
labels = append(labels, "code", strconv.Itoa(recorder.getCode()))

m.reqsCounter.With(labels...).Add(1)
m.reqDurationHistogram.With(labels...).Observe(time.Since(start).Seconds())
m.reqDurationHistogram.With(labels...).Observe(duration)
}

func getRequestProtocol(req *http.Request) string {
Expand Down