-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.go
104 lines (88 loc) · 2.72 KB
/
options.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
package statsd
import (
"strconv"
"time"
"github.com/gin-gonic/gin"
)
const (
DefaultResponseTimeEnabled = true
DefaultThroughputEnabled = true
DefaultStatusCodeEnabled = true
DefaultSuccessEnabled = true
DefaultErrorEnabled = true
DefaultResponseTimeBucket = "request.response_time"
DefaultThroughputBucket = "request.throughput"
DefaultStatusCodeBucket = "request.status_code"
DefaultSuccessBucket = "request.success"
DefaultErrorBucket = "request.error.default"
)
// Options represents the configuration of statsd
type Options struct {
Client Client
ResponseTimeEnabled,
ThroughputEnabled,
StatusCodeEnabled,
SuccessEnabled,
ErrorEnabled bool
// Bucket names for each metric.
ResponseTimeBucket,
ThroughputBucket,
StatusCodeBucket,
SuccessBucket,
ErrorBucket string
}
func newConfiguredClient(client Client) *Options {
return &Options{
Client: client,
ResponseTimeEnabled: DefaultResponseTimeEnabled,
ThroughputEnabled: DefaultThroughputEnabled,
StatusCodeEnabled: DefaultStatusCodeEnabled,
SuccessEnabled: DefaultSuccessEnabled,
ErrorEnabled: DefaultErrorEnabled,
ResponseTimeBucket: DefaultResponseTimeBucket,
ThroughputBucket: DefaultThroughputBucket,
StatusCodeBucket: DefaultStatusCodeBucket,
SuccessBucket: DefaultSuccessBucket,
ErrorBucket: DefaultErrorBucket,
}
}
func (c *Options) IncrError(errors []*gin.Error, handler string) {
if c.ErrorEnabled {
for _, err := range errors {
// If the gin.Error.Meta implements the Bucket interface,
// increment its specific bucket by its given increment amount.
// Otherwise, increment the default error bucket with the default amount.
b, ok := err.Meta.(Bucket)
if !ok {
c.Client.Incr(join(handler, c.ErrorBucket), 1)
continue
}
c.Client.Incr(join(handler, b.BucketName()), 1)
}
}
}
func (c *Options) Timing(start time.Time, handler string) {
if c.ResponseTimeEnabled {
c.Client.Timing(join(handler, c.ResponseTimeBucket),
// Convert to milliseconds.
time.Now().Sub(start).Nanoseconds()/time.Millisecond.Nanoseconds())
}
}
func (c *Options) IncrThroughput(handler string) {
if c.ThroughputEnabled {
c.Client.Incr(join(handler, c.ThroughputBucket), 1)
}
}
// IncrStatusCode increments the context's response status code bucket.
func (c *Options) IncrStatusCode(status int, handler string) {
if c.StatusCodeEnabled {
c.Client.Incr(join(handler, c.StatusCodeBucket, strconv.Itoa(status)), 1)
}
}
// IncrSuccess increments the success bucket
// if no errors were attached to the context.
func (c *Options) IncrSuccess(errors []*gin.Error, handler string) {
if c.SuccessEnabled && len(errors) == 0 {
c.Client.Incr(join(handler, c.SuccessBucket), 1)
}
}