github.com/weaviate/weaviate@v1.24.6/adapters/handlers/rest/requests_total_metrics.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package rest 13 14 import ( 15 "github.com/prometheus/client_golang/prometheus" 16 "github.com/sirupsen/logrus" 17 "github.com/weaviate/weaviate/usecases/monitoring" 18 ) 19 20 type RequestStatus int 21 22 const ( 23 Ok RequestStatus = iota 24 UserError 25 ServerError 26 ) 27 28 func (s RequestStatus) String() string { 29 switch s { 30 case Ok: 31 return "ok" 32 case UserError: 33 return "user_error" 34 case ServerError: 35 return "server_error" 36 } 37 return "unknown" 38 } 39 40 type requestsTotalMetric struct { 41 requestsTotal *prometheus.GaugeVec 42 groupClasses bool 43 api string 44 } 45 46 func newRequestsTotalMetric(prom *monitoring.PrometheusMetrics, api string) *requestsTotalMetric { 47 if prom == nil { 48 return nil 49 } 50 return &requestsTotalMetric{ 51 requestsTotal: prom.RequestsTotal, 52 groupClasses: prom.Group, 53 api: api, 54 } 55 } 56 57 func (m *requestsTotalMetric) RequestsTotalInc(status RequestStatus, className, queryType string) { 58 if m == nil { 59 return 60 } 61 62 if m.groupClasses { 63 className = "n/a" 64 } 65 66 m.requestsTotal.With(prometheus.Labels{ 67 "status": status.String(), 68 "class_name": className, 69 "api": m.api, 70 "query_type": queryType, 71 }).Inc() 72 } 73 74 type restApiRequestsTotal interface { 75 logError(className string, err error) 76 logOk(className string) 77 logUserError(className string) 78 logServerError(className string, err error) 79 } 80 81 type restApiRequestsTotalImpl struct { 82 metrics *requestsTotalMetric 83 api, queryType string 84 logger logrus.FieldLogger 85 } 86 87 func (e *restApiRequestsTotalImpl) logOk(className string) { 88 if e.metrics != nil { 89 e.metrics.RequestsTotalInc(Ok, className, e.queryType) 90 } 91 } 92 93 func (e *restApiRequestsTotalImpl) logUserError(className string) { 94 if e.metrics != nil { 95 e.metrics.RequestsTotalInc(UserError, className, e.queryType) 96 } 97 } 98 99 func (e *restApiRequestsTotalImpl) logServerError(className string, err error) { 100 e.logger.WithFields(logrus.Fields{ 101 "action": "requests_total", 102 "api": e.api, 103 "query_type": e.queryType, 104 "class_name": className, 105 }).WithError(err).Error("unexpected error") 106 if e.metrics != nil { 107 e.metrics.RequestsTotalInc(ServerError, className, e.queryType) 108 } 109 } 110 111 type panicsRequestsTotal struct { 112 *restApiRequestsTotalImpl 113 } 114 115 func newPanicsRequestsTotal(metrics *monitoring.PrometheusMetrics, logger logrus.FieldLogger) restApiRequestsTotal { 116 return &panicsRequestsTotal{ 117 restApiRequestsTotalImpl: &restApiRequestsTotalImpl{newRequestsTotalMetric(metrics, "rest"), "rest", "", logger}, 118 } 119 } 120 121 func (e *panicsRequestsTotal) logError(className string, err error) { 122 e.logServerError(className, err) 123 }