Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit da5e757

Browse files
committed
Cleans up status code variables in handlers and use exponential metrics
buckets for request duration Signed-off-by: JoshVanL <[email protected]>
1 parent 539a941 commit da5e757

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

pkg/metrics/metrics.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func New() *Metrics {
4444
prometheus.CounterOpts{
4545
Namespace: promNamespace,
4646
Name: "http_client_requests",
47-
Help: "The number of requests for incoming requests.",
47+
Help: "The number of incoming requests.",
4848
},
4949
[]string{"code", "path", "remote_address"},
5050
)
@@ -53,7 +53,7 @@ func New() *Metrics {
5353
Namespace: promNamespace,
5454
Name: "http_client_duration_seconds",
5555
Help: "The duration in seconds for incoming client requests to be responded to.",
56-
Buckets: prometheus.LinearBuckets(.000, .05, 30),
56+
Buckets: prometheus.ExponentialBuckets(0.005, 0.005, 10),
5757
},
5858
[]string{"remote_address"},
5959
)
@@ -62,7 +62,7 @@ func New() *Metrics {
6262
prometheus.CounterOpts{
6363
Namespace: promNamespace,
6464
Name: "http_server_requests",
65-
Help: "The requests for outgoing server requests.",
65+
Help: "The number of outgoing server requests.",
6666
},
6767
[]string{"code", "path", "remote_address"},
6868
)
@@ -71,7 +71,7 @@ func New() *Metrics {
7171
Namespace: promNamespace,
7272
Name: "http_server_duration_seconds",
7373
Help: "The duration in seconds for outgoing server requests to be responded to.",
74-
Buckets: prometheus.LinearBuckets(.000, .05, 30),
74+
Buckets: prometheus.ExponentialBuckets(0.005, 0.005, 10),
7575
},
7676
[]string{"remote_address"},
7777
)
@@ -90,7 +90,7 @@ func New() *Metrics {
9090
Namespace: promNamespace,
9191
Name: "token_review_duration_seconds",
9292
Help: "The duration in seconds for a token review lookup. Authenticated requests are 1, else 0.",
93-
Buckets: prometheus.LinearBuckets(.000, .05, 30),
93+
Buckets: prometheus.ExponentialBuckets(0.005, 0.005, 10),
9494
},
9595
[]string{"authenticated", "code", "remote_address", "user"},
9696
)
@@ -170,9 +170,8 @@ func (m *Metrics) Shutdown() error {
170170

171171
func (m *Metrics) ObserveClient(code int, path, remoteAddress string, duration time.Duration) {
172172
m.clientRequests.With(prometheus.Labels{
173-
"code": strconv.Itoa(code),
174-
"path": path,
175-
"remote_address": remoteAddress,
173+
"code": strconv.Itoa(code),
174+
"path": path,
176175
}).Inc()
177176

178177
m.clientDuration.With(prometheus.Labels{
@@ -182,9 +181,8 @@ func (m *Metrics) ObserveClient(code int, path, remoteAddress string, duration t
182181

183182
func (m *Metrics) ObserveServer(code int, path, remoteAddress string, duration time.Duration) {
184183
m.serverRequests.With(prometheus.Labels{
185-
"code": strconv.Itoa(code),
186-
"path": path,
187-
"remote_address": remoteAddress,
184+
"code": strconv.Itoa(code),
185+
"path": path,
188186
}).Inc()
189187

190188
m.serverDuration.With(prometheus.Labels{

pkg/proxy/context/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func WithClientRequestTimestamp(req *http.Request) *http.Request {
6767
return req.WithContext(request.WithValue(req.Context(), clientRequestTimestampKey, time.Now()))
6868
}
6969

70-
// ClientRequestTimestampKey will return thetimestamp that the client request was received.
70+
// ClientRequestTimestamp will return thetimestamp that the client request was received.
7171
func ClientRequestTimestamp(req *http.Request) time.Time {
7272
stamp, _ := req.Context().Value(clientRequestTimestampKey).(time.Time)
7373
return stamp

pkg/proxy/handlers.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ func (p *Proxy) withHandlers(handler http.Handler) http.Handler {
4141

4242
// Add the auditor backend as a shutdown hook
4343
p.hooks.AddPreShutdownHook("AuditBackend", p.auditor.Shutdown)
44-
// Add the metrics server as a shutdown hook
45-
p.hooks.AddPreShutdownHook("Metrics", p.metrics.Shutdown)
4644

4745
return handler
4846
}
@@ -186,7 +184,7 @@ func (p *Proxy) withImpersonateRequest(handler http.Handler) http.Handler {
186184
}
187185

188186
// withClientTimestamp adds the current timestamp for the client request to the
189-
// request contect.
187+
// request context.
190188
func (p *Proxy) withClientTimestamp(handler http.Handler) http.Handler {
191189
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
192190
req = context.WithClientRequestTimestamp(req)
@@ -233,28 +231,28 @@ func (p *Proxy) newErrorHandler() func(rw http.ResponseWriter, req *http.Request
233231
case errImpersonateHeader:
234232
statusCode = http.StatusForbidden
235233
klog.V(2).Infof("impersonation user request %s", remoteAddr)
236-
http.Error(rw, "Impersonation requests are disabled when using kube-oidc-proxy", http.StatusForbidden)
234+
http.Error(rw, "Impersonation requests are disabled when using kube-oidc-proxy", statusCode)
237235
return
238236

239237
// No name given or available in oidc request
240238
case errNoName:
241239
statusCode = http.StatusForbidden
242240
klog.V(2).Infof("no name available in oidc info %s", remoteAddr)
243-
http.Error(rw, "Username claim not available in OIDC Issuer response", http.StatusForbidden)
241+
http.Error(rw, "Username claim not available in OIDC Issuer response", statusCode)
244242
return
245243

246244
// No impersonation configuration found in context
247245
case errNoImpersonationConfig:
248246
statusCode = http.StatusInternalServerError
249247
klog.Errorf("if you are seeing this, there is likely a bug in the proxy (%s): %s", remoteAddr, err)
250-
http.Error(rw, "", http.StatusInternalServerError)
248+
http.Error(rw, "", statusCode)
251249
return
252250

253251
// Server or unknown error
254252
default:
255253
statusCode = http.StatusInternalServerError
256254
klog.Errorf("unknown error (%s): %s", remoteAddr, err)
257-
http.Error(rw, "", http.StatusInternalServerError)
255+
http.Error(rw, "", statusCode)
258256
}
259257
}
260258
}

pkg/proxy/proxy.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ func (p *Proxy) RoundTrip(req *http.Request) (*http.Response, error) {
198198
statusCode = resp.StatusCode
199199
}
200200

201-
p.metrics.ObserveClient(statusCode, req.URL.Path, remoteAddr, time.Since(clientDuration))
201+
// If we get an error here, then the client metrics observation will happen
202+
// at the proxy error handler.
203+
if err == nil {
204+
p.metrics.ObserveClient(statusCode, req.URL.Path, remoteAddr, time.Since(clientDuration))
205+
}
202206
p.metrics.ObserveServer(statusCode, req.URL.Path, remoteAddr, time.Since(serverDuration))
203207

204208
return resp, err

test/e2e/suite/cases/impersonation/impersonation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ var _ = framework.CasesDescribe("Impersonation", func() {
7272
By("Creating ClusterRole for system:anonymous to impersonate")
7373
roleImpersonate, err := f.Helper().KubeClient.RbacV1().ClusterRoles().Create(context.TODO(), &rbacv1.ClusterRole{
7474
ObjectMeta: metav1.ObjectMeta{
75-
GenerateName: fmt.Sprintf("test-user-role-impersonate-"),
75+
GenerateName: "test-user-role-impersonate-",
7676
},
7777
Rules: []rbacv1.PolicyRule{
7878
{APIGroups: []string{""}, Resources: []string{"users"}, Verbs: []string{"impersonate"}},
@@ -83,7 +83,7 @@ var _ = framework.CasesDescribe("Impersonation", func() {
8383
By("Creating Role for user foo to list Pods")
8484
rolePods, err := f.Helper().KubeClient.RbacV1().Roles(f.Namespace.Name).Create(context.TODO(), &rbacv1.Role{
8585
ObjectMeta: metav1.ObjectMeta{
86-
GenerateName: fmt.Sprintf("test-user-role-pods-"),
86+
GenerateName: "test-user-role-pods-",
8787
},
8888
Rules: []rbacv1.PolicyRule{
8989
{APIGroups: []string{""}, Resources: []string{"pods"}, Verbs: []string{"get", "list"}},

0 commit comments

Comments
 (0)