http

package
v1.0.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2024 License: MIT Imports: 14 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLabels = []Label{
	{
		Label: "service_name",
		GetValue: func(ctx *gin.Context) string {
			return app.Name()
		},
	},
	{
		Label: "node",
		GetValue: func(ctx *gin.Context) string {
			return app.LocalIP()
		},
	},
	{
		Label: "method",
		GetValue: func(c *gin.Context) string {
			return c.Request.Method
		},
	},
	{
		Label: "api",
		GetValue: func(c *gin.Context) string {
			return c.Request.URL.Path
		},
	},
	{
		Label: "error_number",
		GetValue: func(c *gin.Context) string {
			return strconv.Itoa(len(c.Errors))
		},
	},
	{
		Label: "http_status",
		GetValue: func(c *gin.Context) string {
			return strconv.Itoa(c.Writer.Status())
		},
	},
	{
		Label: "errno",
		GetValue: func(c *gin.Context) string {
			ctx := c.Request.Context()

			rw := lc.ValueResponseWriter(c.Request.Context())
			bw, ok := rw.(*mr.BodyWriter)
			if !ok {
				return "-1"
			}

			resp := &sr.Response{}
			if err := json.Unmarshal(bw.Body.Bytes(), resp); err != nil {
				return "-2"
			}

			code := cast.ToString(int(resp.Errno))

			logger.AddField(ctx, logger.Reflect(logger.Errno, code))

			c.Request = c.Request.WithContext(ctx)

			return code
		},
	},
}

Functions

func HTTPMetricsMiddleware

func HTTPMetricsMiddleware(opts ...OptionFunc) gin.HandlerFunc

HTTPMetricsMiddleware return a gin.HandlerFunc. Panic and response is executed after c.Next. The code after next takes effect in the reverse order. So must register after PanicMiddleware and LoggerMiddleware.

func NewHTTPMetrics

func NewHTTPMetrics(opts ...OptionFunc) *httpMetrics

opts ...OptionFunc)

func WithFilters

func WithFilters(filters []Filter) func(*Options)

func WithLabels

func WithLabels(labels []Label) func(*Options)

Types

type Filter

type Filter func(*gin.Context) bool

Filter if hit filter return false, don't incr metrics's statistics

type Label

type Label struct {
	Label    string
	GetValue func(*gin.Context) string
}

func (*Label) Name

func (l *Label) Name() string

func (*Label) Value

func (l *Label) Value(c *gin.Context) string

type OptionFunc

type OptionFunc func(*Options)

type Options

type Options struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL