sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/jira/metrics.go (about) 1 /* 2 Copyright 2020 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 package jira 18 19 import ( 20 "net/http" 21 "strconv" 22 "time" 23 24 "github.com/prometheus/client_golang/prometheus" 25 26 "sigs.k8s.io/prow/pkg/simplifypath" 27 ) 28 29 func init() { 30 prometheus.MustRegister(requestResults) 31 } 32 33 var requestResults = prometheus.NewHistogramVec(prometheus.HistogramOpts{ 34 Name: "jira_request_duration_seconds", 35 Buckets: []float64{0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 20}, 36 }, []string{methodField, pathField, statusField}) 37 38 func pathSimplifier() simplifypath.Simplifier { 39 return simplifypath.NewSimplifier(simplifypath.L("", // shadow element mimicing the root 40 simplifypath.L("rest", 41 simplifypath.L("api", 42 simplifypath.L("2", 43 simplifypath.L("project"), 44 simplifypath.L("issue", 45 simplifypath.V("issueID", 46 simplifypath.L("remotelink"), 47 ), 48 ), 49 ), 50 ), 51 ), 52 )) 53 } 54 55 const ( 56 methodField = "method" 57 pathField = "path" 58 statusField = "status" 59 ) 60 61 type metricsTransport struct { 62 upstream http.RoundTripper 63 pathSimplifier func(string) string 64 recorder *prometheus.HistogramVec 65 } 66 67 func (m *metricsTransport) RoundTrip(r *http.Request) (*http.Response, error) { 68 start := time.Now() 69 result, err := m.upstream.RoundTrip(r) 70 status := "error" 71 if err == nil && result != nil { 72 status = strconv.Itoa(result.StatusCode) 73 } 74 var path string 75 if r.URL != nil { 76 path = r.URL.Path 77 } 78 m.recorder.WithLabelValues(r.Method, m.pathSimplifier(path), status).Observe(float64(time.Since(start).Milliseconds()) / 1000) 79 return result, err 80 }