github.com/oam-dev/cluster-gateway@v1.9.0/pkg/metrics/proxy.go (about)

     1  package metrics
     2  
     3  import (
     4  	"strconv"
     5  	"time"
     6  
     7  	compbasemetrics "k8s.io/component-base/metrics"
     8  )
     9  
    10  const (
    11  	namespace = "ocm"
    12  	subsystem = "proxy"
    13  )
    14  
    15  // labels
    16  const (
    17  	proxiedResource = "resource"
    18  	proxiedVerb     = "verb"
    19  	proxiedCluster  = "cluster"
    20  	success         = "success"
    21  	code            = "code"
    22  )
    23  
    24  var (
    25  	requestDurationSecondsBuckets = []float64{0, 0.005, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 30}
    26  )
    27  
    28  var (
    29  	ocmProxiedRequestsByResourceTotal = compbasemetrics.NewCounterVec(
    30  		&compbasemetrics.CounterOpts{
    31  			Namespace:      namespace,
    32  			Subsystem:      subsystem,
    33  			Name:           "proxied_resource_requests_by_resource_total",
    34  			Help:           "Number of requests proxied requests",
    35  			StabilityLevel: compbasemetrics.ALPHA,
    36  		},
    37  		[]string{proxiedResource, proxiedVerb, code},
    38  	)
    39  	ocmProxiedRequestsByClusterTotal = compbasemetrics.NewCounterVec(
    40  		&compbasemetrics.CounterOpts{
    41  			Namespace:      namespace,
    42  			Subsystem:      subsystem,
    43  			Name:           "proxied_requests_by_cluster_total",
    44  			Help:           "Number of requests proxied requests",
    45  			StabilityLevel: compbasemetrics.ALPHA,
    46  		},
    47  		[]string{proxiedCluster, code},
    48  	)
    49  	ocmProxiedRequestsDurationHistogram = compbasemetrics.NewHistogramVec(
    50  		&compbasemetrics.HistogramOpts{
    51  			Namespace:      namespace,
    52  			Subsystem:      subsystem,
    53  			Name:           "proxied_request_duration_seconds",
    54  			Help:           "Cluster proxy request time cost",
    55  			Buckets:        requestDurationSecondsBuckets,
    56  			StabilityLevel: compbasemetrics.ALPHA,
    57  		},
    58  		[]string{proxiedResource, proxiedVerb, proxiedCluster, code},
    59  	)
    60  	ocmProxiedClusterEscalationRequestDurationHistogram = compbasemetrics.NewHistogramVec(
    61  		&compbasemetrics.HistogramOpts{
    62  			Namespace:      namespace,
    63  			Subsystem:      subsystem,
    64  			Name:           "cluster_escalation_access_review_duration_seconds",
    65  			Help:           "Cluster escalation access review time cost",
    66  			Buckets:        requestDurationSecondsBuckets,
    67  			StabilityLevel: compbasemetrics.ALPHA,
    68  		},
    69  		[]string{success},
    70  	)
    71  )
    72  
    73  func RecordProxiedRequestsByResource(resource string, verb string, code int) {
    74  	ocmProxiedRequestsByResourceTotal.
    75  		WithLabelValues(resource, verb, strconv.Itoa(code)).
    76  		Inc()
    77  }
    78  
    79  func RecordProxiedRequestsByCluster(cluster string, code int) {
    80  	ocmProxiedRequestsByClusterTotal.
    81  		WithLabelValues(cluster, strconv.Itoa(code)).
    82  		Inc()
    83  }
    84  
    85  func RecordProxiedRequestsDuration(resource string, verb string, cluster string, code int, ts time.Duration) {
    86  	ocmProxiedRequestsDurationHistogram.
    87  		WithLabelValues(resource, verb, cluster, strconv.Itoa(code)).
    88  		Observe(ts.Seconds())
    89  }