-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmark.go
143 lines (132 loc) · 3.26 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
// 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/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)
}
// 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()
}
})
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()
}
})
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()
}
})
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()
}
})
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()
}
})
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()
}
})
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()
}
})
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()
}
})
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
}