/
client.go
261 lines (217 loc) · 7.73 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package client
import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"path"
"strings"
"time"
"github.com/gorilla/websocket"
json "github.com/json-iterator/go"
"github.com/prometheus/common/config"
"github.com/grafana/loki/pkg/build"
"github.com/grafana/loki/pkg/loghttp"
"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/util"
)
const (
queryPath = "/loki/api/v1/query"
queryRangePath = "/loki/api/v1/query_range"
labelsPath = "/loki/api/v1/labels"
labelValuesPath = "/loki/api/v1/label/%s/values"
seriesPath = "/loki/api/v1/series"
tailPath = "/loki/api/v1/tail"
)
var (
userAgent = fmt.Sprintf("loki-logcli/%s", build.Version)
)
// Client contains all the methods to query a Loki instance, it's an interface to allow multiple implementations.
type Client interface {
Query(queryStr string, limit int, time time.Time, direction logproto.Direction, quiet bool) (*loghttp.QueryResponse, error)
QueryRange(queryStr string, limit int, from, through time.Time, direction logproto.Direction, step, interval time.Duration, quiet bool) (*loghttp.QueryResponse, error)
ListLabelNames(quiet bool, from, through time.Time) (*loghttp.LabelResponse, error)
ListLabelValues(name string, quiet bool, from, through time.Time) (*loghttp.LabelResponse, error)
Series(matchers []string, from, through time.Time, quiet bool) (*loghttp.SeriesResponse, error)
LiveTailQueryConn(queryStr string, delayFor int, limit int, from int64, quiet bool) (*websocket.Conn, error)
GetOrgID() string
}
// Client contains fields necessary to query a Loki instance
type DefaultClient struct {
TLSConfig config.TLSConfig
Username string
Password string
Address string
OrgID string
}
// Query uses the /api/v1/query endpoint to execute an instant query
// excluding interfacer b/c it suggests taking the interface promql.Node instead of logproto.Direction b/c it happens to have a String() method
// nolint:interfacer
func (c *DefaultClient) Query(queryStr string, limit int, time time.Time, direction logproto.Direction, quiet bool) (*loghttp.QueryResponse, error) {
qsb := util.NewQueryStringBuilder()
qsb.SetString("query", queryStr)
qsb.SetInt("limit", int64(limit))
qsb.SetInt("start", time.UnixNano())
qsb.SetString("direction", direction.String())
return c.doQuery(queryPath, qsb.Encode(), quiet)
}
// QueryRange uses the /api/v1/query_range endpoint to execute a range query
// excluding interfacer b/c it suggests taking the interface promql.Node instead of logproto.Direction b/c it happens to have a String() method
// nolint:interfacer
func (c *DefaultClient) QueryRange(queryStr string, limit int, from, through time.Time, direction logproto.Direction, step, interval time.Duration, quiet bool) (*loghttp.QueryResponse, error) {
params := util.NewQueryStringBuilder()
params.SetString("query", queryStr)
params.SetInt32("limit", limit)
params.SetInt("start", from.UnixNano())
params.SetInt("end", through.UnixNano())
params.SetString("direction", direction.String())
// The step is optional, so we do set it only if provided,
// otherwise we do leverage on the API defaults
if step != 0 {
params.SetInt("step", int64(step.Seconds()))
}
if interval != 0 {
params.SetInt("interval", int64(interval.Seconds()))
}
return c.doQuery(queryRangePath, params.Encode(), quiet)
}
// ListLabelNames uses the /api/v1/label endpoint to list label names
func (c *DefaultClient) ListLabelNames(quiet bool, from, through time.Time) (*loghttp.LabelResponse, error) {
var labelResponse loghttp.LabelResponse
params := util.NewQueryStringBuilder()
params.SetInt("start", from.UnixNano())
params.SetInt("end", through.UnixNano())
if err := c.doRequest(labelsPath, params.Encode(), quiet, &labelResponse); err != nil {
return nil, err
}
return &labelResponse, nil
}
// ListLabelValues uses the /api/v1/label endpoint to list label values
func (c *DefaultClient) ListLabelValues(name string, quiet bool, from, through time.Time) (*loghttp.LabelResponse, error) {
path := fmt.Sprintf(labelValuesPath, url.PathEscape(name))
var labelResponse loghttp.LabelResponse
params := util.NewQueryStringBuilder()
params.SetInt("start", from.UnixNano())
params.SetInt("end", through.UnixNano())
if err := c.doRequest(path, params.Encode(), quiet, &labelResponse); err != nil {
return nil, err
}
return &labelResponse, nil
}
func (c *DefaultClient) Series(matchers []string, from, through time.Time, quiet bool) (*loghttp.SeriesResponse, error) {
params := util.NewQueryStringBuilder()
params.SetInt("start", from.UnixNano())
params.SetInt("end", through.UnixNano())
params.SetStringArray("match", matchers)
var seriesResponse loghttp.SeriesResponse
if err := c.doRequest(seriesPath, params.Encode(), quiet, &seriesResponse); err != nil {
return nil, err
}
return &seriesResponse, nil
}
// LiveTailQueryConn uses /api/prom/tail to set up a websocket connection and returns it
func (c *DefaultClient) LiveTailQueryConn(queryStr string, delayFor int, limit int, from int64, quiet bool) (*websocket.Conn, error) {
qsb := util.NewQueryStringBuilder()
qsb.SetString("query", queryStr)
qsb.SetInt("delay_for", int64(delayFor))
qsb.SetInt("limit", int64(limit))
qsb.SetInt("from", from)
return c.wsConnect(tailPath, qsb.Encode(), quiet)
}
func (c *DefaultClient) GetOrgID() string {
return c.OrgID
}
func (c *DefaultClient) doQuery(path string, query string, quiet bool) (*loghttp.QueryResponse, error) {
var err error
var r loghttp.QueryResponse
if err = c.doRequest(path, query, quiet, &r); err != nil {
return nil, err
}
return &r, nil
}
func (c *DefaultClient) doRequest(path, query string, quiet bool, out interface{}) error {
us, err := buildURL(c.Address, path, query)
if err != nil {
return err
}
if !quiet {
log.Print(us)
}
req, err := http.NewRequest("GET", us, nil)
if err != nil {
return err
}
req.SetBasicAuth(c.Username, c.Password)
req.Header.Set("User-Agent", userAgent)
if c.OrgID != "" {
req.Header.Set("X-Scope-OrgID", c.OrgID)
}
// Parse the URL to extract the host
clientConfig := config.HTTPClientConfig{
TLSConfig: c.TLSConfig,
}
client, err := config.NewClientFromConfig(clientConfig, "logcli", false)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Println("error closing body", err)
}
}()
if resp.StatusCode/100 != 2 {
buf, _ := ioutil.ReadAll(resp.Body) // nolint
return fmt.Errorf("Error response from server: %s (%v)", string(buf), err)
}
return json.NewDecoder(resp.Body).Decode(out)
}
func (c *DefaultClient) wsConnect(path, query string, quiet bool) (*websocket.Conn, error) {
us, err := buildURL(c.Address, path, query)
if err != nil {
return nil, err
}
tlsConfig, err := config.NewTLSConfig(&c.TLSConfig)
if err != nil {
return nil, err
}
if strings.HasPrefix(us, "https") {
us = strings.Replace(us, "https", "wss", 1)
} else if strings.HasPrefix(us, "http") {
us = strings.Replace(us, "http", "ws", 1)
}
if !quiet {
log.Println(us)
}
h := http.Header{"Authorization": {"Basic " + base64.StdEncoding.EncodeToString([]byte(c.Username+":"+c.Password))}}
if c.OrgID != "" {
h.Set("X-Scope-OrgID", c.OrgID)
}
ws := websocket.Dialer{
TLSClientConfig: tlsConfig,
}
conn, resp, err := ws.Dial(us, h)
if err != nil {
if resp == nil {
return nil, err
}
buf, _ := ioutil.ReadAll(resp.Body) // nolint
return nil, fmt.Errorf("Error response from server: %s (%v)", string(buf), err)
}
return conn, nil
}
// buildURL concats a url `http://foo/bar` with a path `/buzz`.
func buildURL(u, p, q string) (string, error) {
url, err := url.Parse(u)
if err != nil {
return "", err
}
url.Path = path.Join(url.Path, p)
url.RawQuery = q
return url.String(), nil
}