-
Notifications
You must be signed in to change notification settings - Fork 0
/
mark.go
170 lines (157 loc) · 4.06 KB
/
mark.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Package gomark runs an HTTP server for markable variables.
//
// A variable is a gmi.Marker that provides a single kind of monitor and expose their markings through HTTP
// request: http://ip:port/vars.
//
// After variable is created, user may call Mark() to push a number. The definition of number depends
// what kind of variable you create.
//
// If you want to destroy variable, call Cancel(), after which variable can not be used.
//
// Both Mark() and Cancel() are sync call and are safe cross routines.
//
// gomark is actually a go verison of bvar, see:
// https://github.com/apache/incubator-brpc
// for more information.
package gomark
import (
"github.com/forrestjgq/glog"
"github.com/forrestjgq/gomark/gmi"
"github.com/forrestjgq/gomark/internal/gm"
"github.com/forrestjgq/gomark/internal/httpsrv"
)
// StartHTTPServer will create an http server for gomark.
func StartHTTPServer(port int) {
if port == 0 {
panic("gomark does not take a zero port")
}
httpsrv.Start(port)
}
// Request will docker gomark to your own http server
// See http_server.go for detailed information, there Start() will start an http
// server based on mux, and it will call the same API as this calls.
func Request(req *gmi.Request) *gmi.Response {
return httpsrv.RequestHTTP(req)
}
// EnableServer will make server accept all marks
func EnableServer() {
gm.EnableServer()
}
// DisableServer will make server reject all marks
func DisableServer() {
gm.DisableServer()
}
// NewLatencyRecorder create a latency recorder.
func NewLatencyRecorder(name string) gmi.Marker {
var ret gmi.Marker
gm.RemoteCall(func() {
lr, err := gm.NewLatencyRecorder(name)
if err == nil {
ret = lr.VarBase().Marker()
} else {
glog.Errorf("create latency recorder(%s) fails, err: %v", name, err)
}
})
return ret
}
// NewAdder create an adder with series.
func NewAdder(name string) gmi.Marker {
var ret gmi.Marker
gm.RemoteCall(func() {
add, err := gm.NewAdder(name)
if err == nil {
ret = add.VarBase().Marker()
} else {
glog.Errorf("create adder(%s) fails, err: %v", name, err)
}
})
return ret
}
func NewAdderPerSecond(name string) gmi.Marker {
var ret gmi.Marker
gm.RemoteCall(func() {
add, err := gm.NewAdderPersecond(name)
if err == nil {
ret = add.VarBase().Marker()
} else {
glog.Errorf("create adder per second(%s) fails, err: %v", name, err)
}
})
return ret
}
func NewStatus(name string) gmi.Marker {
var ret gmi.Marker
gm.RemoteCall(func() {
add, err := gm.NewStatus(name)
if err == nil {
ret = add.VarBase().Marker()
} else {
glog.Errorf("create status(%s) fails, err: %v", name, err)
}
})
return ret
}
// NewCounter provide a passive status for counter.
// I prefer you use NewAdder instead.
func NewCounter(name string) gmi.Marker {
var ret gmi.Marker
gm.RemoteCall(func() {
c, err := gm.NewCounterWithName(name)
if err == nil {
ret = c.VarBase().Marker()
} else {
glog.Errorf("create counter(%s) fails, err: %v", name, err)
}
})
return ret
}
// NewQPS provide QPS statistics.
func NewQPS(name string) gmi.Marker {
var ret gmi.Marker
gm.RemoteCall(func() {
q, err := gm.NewQPS(name)
if err == nil {
ret = q.VarBase().Marker()
} else {
glog.Errorf("create qps(%s) fails, err: %v", name, err)
}
})
return ret
}
// NewMaxer saves max value(no series)
func NewMaxer(name string) gmi.Marker {
var ret gmi.Marker
gm.RemoteCall(func() {
w, err := gm.NewMaxer(name)
if err == nil {
ret = w.VarBase().Marker()
} else {
glog.Errorf("create maxer(%s) fails, err: %v", name, err)
}
})
return ret
}
// NewWindowMaxer collects max values in each period.
func NewWindowMaxer(name string) gmi.Marker {
var ret gmi.Marker
gm.RemoteCall(func() {
w, err := gm.NewWindowMaxer(name)
if err == nil {
ret = w.VarBase().Marker()
} else {
glog.Errorf("create windown maxer(%s) fails, err: %v", name, err)
}
})
return ret
}
// NewPercentile create a percentile collector.
func NewPercentile() interface {
Push(v gm.Mark)
Dispose()
} {
return gm.NewPercentile()
}
func IntToMarker(id int) gmi.Marker {
t := gm.Identity(id)
return t
}