go.etcd.io/etcd@v3.3.27+incompatible/proxy/httpproxy/metrics.go (about)

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package httpproxy
    16  
    17  import (
    18  	"net/http"
    19  	"strconv"
    20  	"time"
    21  
    22  	"github.com/prometheus/client_golang/prometheus"
    23  )
    24  
    25  var (
    26  	requestsIncoming = prometheus.NewCounterVec(
    27  		prometheus.CounterOpts{
    28  			Namespace: "etcd",
    29  			Subsystem: "proxy",
    30  			Name:      "requests_total",
    31  			Help:      "Counter requests incoming by method.",
    32  		}, []string{"method"})
    33  
    34  	requestsHandled = prometheus.NewCounterVec(
    35  		prometheus.CounterOpts{
    36  			Namespace: "etcd",
    37  			Subsystem: "proxy",
    38  			Name:      "handled_total",
    39  			Help:      "Counter of requests fully handled (by authoratitave servers)",
    40  		}, []string{"method", "code"})
    41  
    42  	requestsDropped = prometheus.NewCounterVec(
    43  		prometheus.CounterOpts{
    44  			Namespace: "etcd",
    45  			Subsystem: "proxy",
    46  			Name:      "dropped_total",
    47  			Help:      "Counter of requests dropped on the proxy.",
    48  		}, []string{"method", "proxying_error"})
    49  
    50  	requestsHandlingTime = prometheus.NewHistogramVec(
    51  		prometheus.HistogramOpts{
    52  			Namespace: "etcd",
    53  			Subsystem: "proxy",
    54  			Name:      "handling_duration_seconds",
    55  			Help: "Bucketed histogram of handling time of successful events (non-watches), by method " +
    56  				"(GET/PUT etc.).",
    57  			Buckets: prometheus.ExponentialBuckets(0.0005, 2, 13),
    58  		}, []string{"method"})
    59  )
    60  
    61  type forwardingError string
    62  
    63  const (
    64  	zeroEndpoints         forwardingError = "zero_endpoints"
    65  	failedSendingRequest  forwardingError = "failed_sending_request"
    66  	failedGettingResponse forwardingError = "failed_getting_response"
    67  )
    68  
    69  func init() {
    70  	prometheus.MustRegister(requestsIncoming)
    71  	prometheus.MustRegister(requestsHandled)
    72  	prometheus.MustRegister(requestsDropped)
    73  	prometheus.MustRegister(requestsHandlingTime)
    74  }
    75  
    76  func reportIncomingRequest(request *http.Request) {
    77  	requestsIncoming.WithLabelValues(request.Method).Inc()
    78  }
    79  
    80  func reportRequestHandled(request *http.Request, response *http.Response, startTime time.Time) {
    81  	method := request.Method
    82  	requestsHandled.WithLabelValues(method, strconv.Itoa(response.StatusCode)).Inc()
    83  	requestsHandlingTime.WithLabelValues(method).Observe(time.Since(startTime).Seconds())
    84  }
    85  
    86  func reportRequestDropped(request *http.Request, err forwardingError) {
    87  	requestsDropped.WithLabelValues(request.Method, string(err)).Inc()
    88  }