github.com/qlik-oss/gopherciser@v0.18.6/buildmetrics/buildmetrics.go (about) 1 //go:build linux || darwin || windows 2 // +build linux darwin windows 3 4 package buildmetrics 5 6 import ( 7 "context" 8 "strconv" 9 "time" 10 11 "github.com/qlik-oss/gopherciser/metrics" 12 ) 13 14 var ( 15 enabled bool 16 apiEnabled bool 17 ) 18 19 // MetricEnabled returns whether metrics are enabled 20 func metricEnabled() bool { 21 return enabled 22 } 23 24 // ApiMetricEnabled returns whether metrics are enabled for apis as well 25 func apiMetricEnabled() bool { 26 return apiEnabled 27 } 28 29 func getLabel(action string, label string) string { 30 if label != "" { 31 return label 32 } 33 return action 34 } 35 36 // ReportApiResult reports the duration for a specific API path and response code 37 func ReportApiResult(action, label, path, method string, responseCode int, duration time.Duration) { 38 if metricEnabled() && apiMetricEnabled() { 39 actionlabel := getLabel(action, label) 40 resultString := strconv.Itoa(responseCode) 41 metrics.ApiCallDuration.WithLabelValues(actionlabel, path, method, resultString).Observe(duration.Seconds()) 42 metrics.ApiCallDurationQuantile.WithLabelValues(actionlabel, path, method, resultString).Observe(duration.Seconds()) 43 } 44 } 45 46 // ReportSuccess is invoked when a simulated user action is successfully completed. 47 // This then updates Prometheus metrics correlating to this (ReponseTimes | Latency | success counter for an action) 48 func ReportSuccess(action string, label string, time float64) { 49 if metricEnabled() { 50 actionlabel := getLabel(action, label) 51 metrics.GopherResponseTimes.WithLabelValues(actionlabel).Observe(time) 52 metrics.GopherActionLatencyHist.WithLabelValues(actionlabel).Observe(time) 53 metrics.GopherActions.WithLabelValues("success", actionlabel).Inc() 54 } 55 } 56 57 // ReportFailure is invoked when a simulated user action fails. 58 // This then updates Prometheus metrics correlating to this (Failure counter for an action) 59 func ReportFailure(action string, label string) { 60 if metricEnabled() { 61 actionlabel := getLabel(action, label) 62 metrics.GopherActions.WithLabelValues("failure", actionlabel).Inc() 63 } 64 } 65 66 // ReportError is invoked when an error occurs in execution. A user action can in theory have many errors. 67 // This then updates Prometheus metrics correlating to this (Error counter) 68 func ReportError(action string, label string) { 69 if metricEnabled() { 70 actionlabel := getLabel(action, label) 71 metrics.GopherErrors.WithLabelValues(actionlabel).Inc() 72 } 73 } 74 75 // ReportWarning is invoked when an warning occurs in execution. A user action can in theory have many warnings. 76 // This then updates Prometheus metrics correlating to this (Warning counter) 77 func ReportWarning(action string, label string) { 78 if metricEnabled() { 79 actionlabel := getLabel(action, label) 80 metrics.GopherWarnings.WithLabelValues(actionlabel).Inc() 81 } 82 } 83 84 // AddUser is invoked when a new simulated user is added. 85 // This then updates Prometheus metrics correlating to this (Users total | Active users) 86 func AddUser() { 87 if metricEnabled() { 88 metrics.GopherUsersTotal.Inc() 89 metrics.GopherActiveUsers.Inc() 90 } 91 } 92 93 // RemoveUser is invoked when a new simulated user is added. 94 // This then updates Prometheus metrics correlating to this (Decrements Active users). 95 // Note Total users is for obvious reasons not decremented 96 func RemoveUser() { 97 if metricEnabled() { 98 metrics.GopherActiveUsers.Dec() 99 } 100 } 101 102 // PullMetrics is called once to setup and enable Prometheus pull metrics on a certain endpoint 103 func PullMetrics(ctx context.Context, metricsPort int, registeredActions []string) error { 104 enabled = true 105 err := metrics.PullMetrics(ctx, metricsPort, registeredActions) 106 if err != nil { 107 return err 108 } 109 return nil 110 } 111 112 // PushMetrics is called once to setup and enable Prometheus push metrics to specified address 113 func PushMetrics(ctx context.Context, metricsTarget, job string, groupingKeys, registeredActions []string, enableApiMetrics bool) error { 114 enabled = true 115 apiEnabled = enableApiMetrics 116 err := metrics.PushMetrics(ctx, metricsTarget, job, groupingKeys, registeredActions, enableApiMetrics) 117 if err != nil { 118 return err 119 } 120 return nil 121 }