|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// ref: https://github.com/linki/instrumented_http/blob/master/client.go |
| 18 | + |
| 19 | +package http |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + "strings" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/prometheus/client_golang/prometheus" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + requestDuration = prometheus.NewSummaryVec( |
| 32 | + prometheus.SummaryOpts{ |
| 33 | + Name: "request_duration_seconds", |
| 34 | + Help: "The HTTP request latencies in seconds.", |
| 35 | + Subsystem: "http", |
| 36 | + ConstLabels: prometheus.Labels{"handler": "instrumented_http"}, |
| 37 | + Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, |
| 38 | + }, |
| 39 | + []string{"scheme", "host", "path", "method", "status"}, |
| 40 | + ) |
| 41 | +) |
| 42 | + |
| 43 | +func init() { |
| 44 | + prometheus.MustRegister(requestDuration) |
| 45 | +} |
| 46 | + |
| 47 | +type CustomRoundTripper struct { |
| 48 | + next http.RoundTripper |
| 49 | +} |
| 50 | + |
| 51 | +// CancelRequest is a no-op to satisfy interfaces that require it. |
| 52 | +// https://github.com/kubernetes/client-go/blob/34f52c14eaed7e50c845cc14e85e1c4c91e77470/transport/transport.go#L346 |
| 53 | +func (r *CustomRoundTripper) CancelRequest(_ *http.Request) { |
| 54 | +} |
| 55 | + |
| 56 | +func (r *CustomRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { |
| 57 | + start := time.Now() |
| 58 | + resp, err := r.next.RoundTrip(req) |
| 59 | + |
| 60 | + status := "" |
| 61 | + if resp != nil { |
| 62 | + status = fmt.Sprintf("%d", resp.StatusCode) |
| 63 | + } |
| 64 | + |
| 65 | + labels := prometheus.Labels{ |
| 66 | + "scheme": req.URL.Scheme, |
| 67 | + "host": req.URL.Host, |
| 68 | + "path": pathProcessor(req.URL.Path), |
| 69 | + "method": req.Method, |
| 70 | + "status": status, |
| 71 | + } |
| 72 | + requestDuration.With(labels).Observe(time.Since(start).Seconds()) |
| 73 | + return resp, err |
| 74 | +} |
| 75 | + |
| 76 | +func NewInstrumentedClient(next *http.Client) *http.Client { |
| 77 | + if next == nil { |
| 78 | + next = http.DefaultClient |
| 79 | + } |
| 80 | + |
| 81 | + next.Transport = NewInstrumentedTransport(next.Transport) |
| 82 | + |
| 83 | + return next |
| 84 | +} |
| 85 | + |
| 86 | +func NewInstrumentedTransport(next http.RoundTripper) http.RoundTripper { |
| 87 | + if next == nil { |
| 88 | + next = http.DefaultTransport |
| 89 | + } |
| 90 | + |
| 91 | + return &CustomRoundTripper{next: next} |
| 92 | +} |
| 93 | + |
| 94 | +func pathProcessor(path string) string { |
| 95 | + parts := strings.Split(path, "/") |
| 96 | + return parts[len(parts)-1] |
| 97 | +} |
0 commit comments