/
client.go
218 lines (186 loc) · 5.76 KB
/
client.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package prometheus
import (
"context"
"net"
"net/http"
"strings"
"time"
"github.com/pingcap/errors"
"github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/romberli/go-util/constant"
client "github.com/prometheus/client_golang/api"
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
)
const (
DefaultStep = time.Minute
DefaultDialTimeout = 30 * time.Second
DefaultKeepAlive = 30 * time.Second
DefaultTLSHandshakeTimeout = 10 * time.Second
defaultHTTPPrefix = "http://"
defaultHTTPSPrefix = "https://"
)
type Config struct {
client.Config
}
var (
DefaultDialer = &net.Dialer{
Timeout: DefaultDialTimeout,
KeepAlive: DefaultKeepAlive,
}
// DefaultRoundTripper is used if no RoundTripper is set in Config,
DefaultRoundTripper http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: DefaultDialer.DialContext,
TLSHandshakeTimeout: DefaultTLSHandshakeTimeout,
}
)
// NewConfig returns a new client.Config with given address and round tripper
func NewConfig(addr string, rt http.RoundTripper) Config {
address := strings.ToLower(addr)
if !strings.HasPrefix(address, defaultHTTPPrefix) && !strings.HasPrefix(address, defaultHTTPSPrefix) {
addr = defaultHTTPPrefix + addr
}
if rt == nil {
rt = DefaultRoundTripper
}
return Config{
client.Config{
Address: addr,
RoundTripper: rt,
},
}
}
// NewConfigWithDefaultRoundTripper returns a new client.Config with given address and default round tripper
func NewConfigWithDefaultRoundTripper(addr string) Config {
address := strings.ToLower(addr)
if !strings.HasPrefix(address, defaultHTTPPrefix) && !strings.HasPrefix(address, defaultHTTPSPrefix) {
addr = defaultHTTPSPrefix + addr
}
return Config{
client.Config{
Address: addr,
RoundTripper: DefaultRoundTripper,
},
}
}
// NewConfigWithBasicAuth returns a new client.Config with given address, user and password
func NewConfigWithBasicAuth(addr, user, pass string) Config {
address := strings.ToLower(addr)
if !strings.HasPrefix(address, defaultHTTPPrefix) && !strings.HasPrefix(address, defaultHTTPSPrefix) {
addr = defaultHTTPPrefix + addr
}
return Config{
client.Config{
Address: addr,
RoundTripper: config.NewBasicAuthRoundTripper(user, config.Secret(pass), constant.EmptyString, constant.EmptyString, DefaultRoundTripper),
},
}
}
type Conn struct {
apiv1.API
}
// NewConn returns a new *Conn with given address and round tripper
func NewConn(addr string, rt http.RoundTripper) (*Conn, error) {
return NewConnWithConfig(NewConfig(addr, rt))
}
// NewConnWithConfig returns a new *Conn with given config
func NewConnWithConfig(config Config) (*Conn, error) {
cli, err := client.NewClient(config.Config)
if err != nil {
return nil, errors.Trace(err)
}
return &Conn{apiv1.NewAPI(cli)}, nil
}
// CheckInstanceStatus checks prometheus instance status
func (conn *Conn) CheckInstanceStatus() bool {
query := "1"
result, err := conn.Execute(query)
if err != nil {
return false
}
status, err := result.GetInt(constant.ZeroInt, constant.ZeroInt)
if err != nil {
return false
}
return status == 1
}
// Execute executes given command with arguments and returns a result
func (conn *Conn) Execute(command string, args ...interface{}) (*Result, error) {
return conn.executeContext(context.Background(), command, args...)
}
// ExecuteContext executes given command with arguments and returns a result
func (conn *Conn) ExecuteContext(ctx context.Context, command string, args ...interface{}) (*Result, error) {
return conn.executeContext(ctx, command, args...)
}
// executeContext executes given command with arguments and returns a result.
// if args length is 0:
//
// it uses time.Now() as the time series
//
// if args length is 1:
//
// argument type must be either time.Time, TimeRange or apiv1.Range
//
// if args length is 2:
//
// argument types must be time.Time and time.Time, represent start time and end time, it uses 1 minute as step
//
// if args length is 3:
//
// argument types muse be in order of time.Time, time.Time and time.Duration, represent start time, end time and step
//
// if args length is larger than 3:
//
// it returns error
func (conn *Conn) executeContext(ctx context.Context, command string, args ...interface{}) (*Result, error) {
var (
arg interface{}
value model.Value
warnings apiv1.Warnings
err error
)
switch len(args) {
case 0:
arg = time.Now()
case 1:
arg = args[constant.ZeroInt]
case 2:
start, startOK := args[0].(time.Time)
end, endOK := args[1].(time.Time)
if !(startOK && endOK) {
return nil, errors.New("args length is 2, both of them should be time.Time, represent start time, end time")
}
arg = NewTimeRange(start, end, DefaultStep)
case 3:
start, startOK := args[0].(time.Time)
end, endOK := args[1].(time.Time)
step, stepOK := args[2].(time.Duration)
if !(startOK && endOK && stepOK) {
return nil, errors.New("args length is 3, should be in order of time.Time, time.Time and time.Duration, represent start time, end time and step")
}
arg = NewTimeRange(start, end, step)
default:
return nil, errors.Errorf("args length should be less or equal to 3, %d is not valid", len(args))
}
switch in := arg.(type) {
case time.Time:
value, warnings, err = conn.Query(ctx, command, in)
if err != nil {
return nil, errors.Trace(err)
}
case TimeRange:
value, warnings, err = conn.QueryRange(ctx, command, in.GetRange())
if err != nil {
return nil, errors.Trace(err)
}
case apiv1.Range:
value, warnings, err = conn.QueryRange(ctx, command, in)
if err != nil {
return nil, errors.Trace(err)
}
default:
return nil, errors.Errorf("unsupported argument data type: %T", arg)
}
return NewResult(value, warnings), nil
}