package apis import ( "reflect" "runtime" "strconv" "strings" "time" "github.com/prometheus/client_golang/prometheus" ) var ( requestCounter = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "http_requests_total", Help: "Number of get requests.", }, []string{"handler", "method", "code"}, ) responseTimeDuration = prometheus.NewHistogramVec( prometheus.HistogramOpts{ Name: "http_request_duration_seconds", Help: "Histogram of response time for HTTP requests.", Buckets: []float64{0.1, 0.2, 0.5, 1, 2, 5, 10, 30, 60}, }, []string{"handler", "method", "code"}, ) ) // getHandleName 获取FuncHandle/StreamHandle函数名 func getHandleName(fn any) string { fullName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name() if fullName == "" { panic("get func name is empty") } parts := strings.Split(fullName, ".") lastPart := parts[len(parts)-1] name := strings.TrimSuffix(lastPart, "-fm") return name } // collectHandleMetrics api指标数据 func collectHandleMetrics(funcName, method string, st time.Time, err error) { code := 200 if err != nil { code = APIError(err).(*APIResponse).HTTPStatusCode } codeStr := strconv.Itoa(code) requestCounter.WithLabelValues(funcName, method, codeStr).Inc() duration := time.Since(st).Seconds() responseTimeDuration.WithLabelValues(funcName, method, codeStr).Observe(duration) } func init() { prometheus.MustRegister(requestCounter) prometheus.MustRegister(responseTimeDuration) }