Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions grpcserver/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ type LoggingOptions struct {

// MetricsOptions represents options for gRPC request metrics that used in GRPCServer.
type MetricsOptions struct {
Namespace string
DurationBuckets []float64
ConstLabels prometheus.Labels
UnaryUserAgentTypeProvider func(ctx context.Context, info *grpc.UnaryServerInfo) string
Namespace string
DurationBuckets []float64
ConstLabels prometheus.Labels
// Deprecated: UnaryUserAgentTypeProvider be removed in the next major version. Please use CustomLabels instead.
UnaryUserAgentTypeProvider func(ctx context.Context, info *grpc.UnaryServerInfo) string
// Deprecated: StreamUserAgentTypeProvider be removed in the next major version. Please use CustomLabels instead.
StreamUserAgentTypeProvider func(ctx context.Context, info *grpc.StreamServerInfo) string
}

Expand Down
3 changes: 2 additions & 1 deletion httpserver/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type HTTPRequestMetricsOpts struct {
ConstLabels prometheus.Labels

// Middleware opts.
// Deprecated: GetUserAgentType be removed in the next major version. Please use CustomLabels with Context instead.
GetUserAgentType middleware.UserAgentTypeGetterFunc
GetRoutePattern middleware.RoutePatternGetterFunc
}
Expand Down Expand Up @@ -138,7 +139,7 @@ func New(cfg *Config, logger log.FieldLogger, opts Opts) (*HTTPServer, error) {
// NewWithHandler creates a new HTTPServer receiving already created http.Handler.
// Unlike the New constructor, it doesn't add any middlewares.
// Typical use case: create a chi.Router using NewRouter and pass it into NewWithHandler.
// Depricated: Will be removed in the next major version. Please use New with Handler options instead.
// Deprecated: Will be removed in the next major version. Please use New with Handler options instead.
func NewWithHandler(cfg *Config, logger log.FieldLogger, handler http.Handler) *HTTPServer {
return newWithHandler(cfg, logger, handler, nil)
}
Expand Down
27 changes: 7 additions & 20 deletions httpserver/middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package middleware
import (
"net/http"
"strconv"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -24,11 +23,6 @@ const (
httpRequestMetricsLabelStatusCode = "status_code"
)

const (
userAgentTypeBrowser = "browser"
userAgentTypeHTTPClient = "http-client"
)

// HTTPRequestInfoMetrics represents a request info for collecting metrics.
type HTTPRequestInfoMetrics struct {
Method string
Expand Down Expand Up @@ -221,6 +215,7 @@ type UserAgentTypeGetterFunc func(r *http.Request) string

// HTTPRequestMetricsOpts represents an options for HTTPRequestMetrics middleware.
type HTTPRequestMetricsOpts struct {
// Deprecated: GetUserAgentType be removed in the next major version. Please use CustomLabels with Context instead.
GetUserAgentType UserAgentTypeGetterFunc
ExcludedEndpoints []string
}
Expand Down Expand Up @@ -248,9 +243,6 @@ func HTTPRequestMetricsWithOpts(
if getRoutePattern == nil {
panic("function for getting route pattern cannot be nil")
}
if opts.GetUserAgentType == nil {
opts.GetUserAgentType = determineUserAgentType
}
return func(next http.Handler) http.Handler {
return &httpRequestMetricsHandler{next: next, collector: collector, getRoutePattern: getRoutePattern, opts: opts}
}
Expand All @@ -277,10 +269,12 @@ func (h *httpRequestMetricsHandler) ServeHTTP(rw http.ResponseWriter, r *http.Re
}

reqInfo := HTTPRequestInfoMetrics{
Method: r.Method,
RoutePattern: h.getRoutePattern(r),
UserAgentType: h.opts.GetUserAgentType(r),
CustomValues: copyValues(mp.values), // we copy values here to avoid mutation during the InFlight metrics processing
Method: r.Method,
RoutePattern: h.getRoutePattern(r),
CustomValues: copyValues(mp.values), // we copy values here to avoid mutation during the InFlight metrics processing
}
if h.opts.GetUserAgentType != nil {
reqInfo.UserAgentType = h.opts.GetUserAgentType(r)
}

h.collector.IncInFlightRequests(reqInfo)
Expand Down Expand Up @@ -324,10 +318,3 @@ func copyValues(src map[string]string) map[string]string {
}
return dst
}

func determineUserAgentType(r *http.Request) string {
if strings.Contains(strings.ToLower(r.UserAgent()), "mozilla") {
return userAgentTypeBrowser
}
return userAgentTypeHTTPClient
}
23 changes: 5 additions & 18 deletions httpserver/middleware/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,25 @@ func TestHttpRequestMetricsHandler_ServeHTTP(t *testing.T) {
customLabels map[string]string
}{
{
name: "GET request, user agent is not browser",
name: "GET request",
method: http.MethodGet,
url: "/hello",
userAgent: "agent1",
statusCodeToReturn: http.StatusOK,
reqsNum: 10,
wantUserAgentType: userAgentTypeHTTPClient,
},
{
name: "POST request, user agent is not browser",
name: "POST request",
method: http.MethodPost,
url: "/world",
userAgent: "agent2",
statusCodeToReturn: http.StatusMethodNotAllowed,
reqsNum: 11,
wantUserAgentType: userAgentTypeHTTPClient,
},
{
name: "DELETE request, user agent is browser",
name: "DELETE request",
method: http.MethodDelete,
url: "/admin",
userAgent: "Mozilla/5.0",
statusCodeToReturn: http.StatusForbidden,
reqsNum: 12,
wantUserAgentType: userAgentTypeBrowser,
},
{
name: "PUT request, custom func to parse user agent",
Expand All @@ -124,37 +118,30 @@ func TestHttpRequestMetricsHandler_ServeHTTP(t *testing.T) {
userAgent: "k8s",
statusCodeToReturn: http.StatusOK,
reqsNum: 10,
wantUserAgentType: userAgentTypeHTTPClient,
excludedEndpoints: []string{"/healthz"},
},
{
name: "GET request, labels currying",
method: http.MethodGet,
url: "/hello-currying",
userAgent: "agent1",
statusCodeToReturn: http.StatusOK,
reqsNum: 10,
wantUserAgentType: userAgentTypeHTTPClient,
curriedLabels: prometheus.Labels{"extra1": "value1", "extra2": "value2"},
},
{
name: "GET request, custom labels",
method: http.MethodGet,
url: "/hello-currying",
userAgent: "agent1",
statusCodeToReturn: http.StatusOK,
reqsNum: 10,
wantUserAgentType: userAgentTypeHTTPClient,
customLabels: map[string]string{"custom1": "value1", "custom2": "value2"},
},
{
name: "GET request, custom labels, labels currying",
method: http.MethodGet,
url: "/hello-currying",
userAgent: "agent1",
statusCodeToReturn: http.StatusOK,
reqsNum: 10,
wantUserAgentType: userAgentTypeHTTPClient,
curriedLabels: prometheus.Labels{"extra1": "value1", "extra2": "value2"},
customLabels: map[string]string{"custom1": "value1", "custom2": "value2"},
},
Expand Down Expand Up @@ -214,7 +201,7 @@ func TestHttpRequestMetricsHandler_ServeHTTP(t *testing.T) {
h := HTTPRequestMetrics(promMetrics, getRoutePattern)(next)
if assert.Panics(t, func() { h.ServeHTTP(resp, req) }) {
assert.Equal(t, 1, next.called)
labels := makeLabels(http.MethodGet, "/internal-error", "http-client", "500", nil)
labels := makeLabels(http.MethodGet, "/internal-error", "", "500", nil)
hist := promMetrics.Durations.With(labels).(prometheus.Histogram)
testutil.AssertSamplesCountInHistogram(t, hist, 1)
}
Expand All @@ -229,7 +216,7 @@ func TestHttpRequestMetricsHandler_ServeHTTP(t *testing.T) {
h := HTTPRequestMetrics(promMetrics, getRoutePattern)(next)
h.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
labels := makeLabels(http.MethodGet, "/hello", "http-client", "200", nil)
labels := makeLabels(http.MethodGet, "/hello", "", "200", nil)
hist := promMetrics.Durations.With(labels).(prometheus.Histogram)
testutil.AssertSamplesCountInHistogram(t, hist, 0)
})
Expand Down