github.com/wfusion/gofusion@v1.1.14/http/metrics.go (about) 1 package http 2 3 import ( 4 "context" 5 "net/http" 6 7 "github.com/iancoleman/strcase" 8 "github.com/spf13/cast" 9 10 "github.com/wfusion/gofusion/common/utils" 11 "github.com/wfusion/gofusion/config" 12 "github.com/wfusion/gofusion/metrics" 13 ) 14 15 var ( 16 metricsCodeTotalKey = []string{"http", "code", "total"} 17 ) 18 19 func metricsCode(ctx context.Context, appName, path, method string, headerLabels map[string]string, 20 code, status, rspSize int, reqSize int64) { 21 select { 22 case <-ctx.Done(): 23 return 24 default: 25 } 26 27 // skip health check logging 28 if path == "/health" && method == http.MethodGet { 29 return 30 } 31 32 _, _ = utils.Catch(func() { 33 app := config.Use(appName).AppName() 34 labels := []metrics.Label{ 35 {Key: "path", Value: path}, 36 {Key: "method", Value: method}, 37 {Key: "code", Value: cast.ToString(code)}, 38 {Key: "status", Value: cast.ToString(status)}, 39 {Key: "req_size", Value: cast.ToString(reqSize)}, 40 {Key: "rsp_size", Value: cast.ToString(rspSize)}, 41 } 42 for k, v := range headerLabels { 43 labels = append(labels, metrics.Label{Key: strcase.ToSnake(k), Value: v}) 44 } 45 46 totalKey := append([]string{app}, metricsCodeTotalKey...) 47 for _, m := range metrics.Internal(metrics.AppName(appName)) { 48 select { 49 case <-ctx.Done(): 50 return 51 default: 52 if m.IsEnableServiceLabel() { 53 m.IncrCounter(ctx, totalKey, 1, metrics.Labels(labels)) 54 } else { 55 m.IncrCounter(ctx, metricsCodeTotalKey, 1, metrics.Labels(labels)) 56 } 57 } 58 } 59 }) 60 }