This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
114 lines (103 loc) · 2.66 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2018 tree xie
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package stats
import (
"errors"
"net/http"
"net/url"
"sync/atomic"
"time"
"github.com/vicanso/elton"
"github.com/vicanso/hes"
)
var (
errNoStatsFunction = errors.New("require on stats function")
)
type (
// OnStats on stats function
OnStats func(*Info, *elton.Context)
// Config stats config
Config struct {
OnStats OnStats
Skipper elton.Skipper
}
// Info stats's info
Info struct {
CID string `json:"cid,omitempty"`
IP string `json:"ip,omitempty"`
Method string `json:"method,omitempty"`
Route string `json:"route,omitempty"`
URI string `json:"uri,omitempty"`
Status int `json:"status,omitempty"`
Consuming time.Duration `json:"consuming,omitempty"`
Type int `json:"type,omitempty"`
Size int `json:"size,omitempty"`
Connecting uint32 `json:"connecting,omitempty"`
}
)
// New create a new stats middleware
func New(config Config) elton.Handler {
if config.OnStats == nil {
panic(errNoStatsFunction)
}
var connectingCount uint32
skipper := config.Skipper
if skipper == nil {
skipper = elton.DefaultSkipper
}
return func(c *elton.Context) (err error) {
if skipper(c) {
return c.Next()
}
atomic.AddUint32(&connectingCount, 1)
defer atomic.AddUint32(&connectingCount, ^uint32(0))
startedAt := time.Now()
req := c.Request
uri, _ := url.QueryUnescape(req.RequestURI)
if uri == "" {
uri = req.RequestURI
}
info := &Info{
CID: c.ID,
Method: req.Method,
Route: c.Route,
URI: uri,
Connecting: connectingCount,
IP: c.RealIP(),
}
err = c.Next()
info.Consuming = time.Since(startedAt)
status := c.StatusCode
if err != nil {
he, ok := err.(*hes.Error)
if ok {
status = he.StatusCode
} else {
status = http.StatusInternalServerError
}
}
if status == 0 {
status = http.StatusOK
}
info.Status = status
info.Type = status / 100
size := 0
if c.BodyBuffer != nil {
size = c.BodyBuffer.Len()
}
info.Size = size
config.OnStats(info, c)
return
}
}