forked from aliyun/aliyun-log-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_store.go
461 lines (412 loc) · 13.1 KB
/
client_store.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package sls
import (
base64E "encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"time"
"github.com/go-kit/kit/log/level"
)
func convertLogstore(c *Client, project, logstore string) *LogStore {
c.accessKeyLock.RLock()
proj := convert(c, project)
c.accessKeyLock.RUnlock()
return &LogStore{
project: proj,
Name: logstore,
}
}
// ListShards returns shard id list of this logstore.
func (c *Client) ListShards(project, logstore string) (shardIDs []*Shard, err error) {
ls := convertLogstore(c, project, logstore)
return ls.ListShards()
}
// SplitShard https://help.aliyun.com/document_detail/29021.html
func (c *Client) SplitShard(project, logstore string, shardID int, splitKey string) (shards []*Shard, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
urlVal := url.Values{}
urlVal.Add("action", "split")
urlVal.Add("key", splitKey)
uri := fmt.Sprintf("/logstores/%v/shards/%v?%v", logstore, shardID, urlVal.Encode())
r, err := c.request(project, "POST", uri, h, nil)
if err != nil {
return
}
defer r.Body.Close()
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, NewClientError(err)
}
err = json.Unmarshal(buf, &shards)
return
}
// MergeShards https://help.aliyun.com/document_detail/29022.html
func (c *Client) MergeShards(project, logstore string, shardID int) (shards []*Shard, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
urlVal := url.Values{}
urlVal.Add("action", "merge")
uri := fmt.Sprintf("/logstores/%v/shards/%v?%v", logstore, shardID, urlVal.Encode())
r, err := c.request(project, "POST", uri, h, nil)
if err != nil {
return
}
defer r.Body.Close()
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, NewClientError(err)
}
err = json.Unmarshal(buf, &shards)
return
}
// PutLogs put logs into logstore.
// The callers should transform user logs into LogGroup.
func (c *Client) PutLogs(project, logstore string, lg *LogGroup) (err error) {
ls := convertLogstore(c, project, logstore)
return ls.PutLogs(lg)
}
// PostLogStoreLogs put logs into Shard logstore by hashKey.
// The callers should transform user logs into LogGroup.
func (c *Client) PostLogStoreLogs(project, logstore string, lg *LogGroup, hashKey *string) (err error) {
ls := convertLogstore(c, project, logstore)
return ls.PostLogStoreLogs(lg, hashKey)
}
// PutLogsWithCompressType put logs into logstore with specific compress type.
// The callers should transform user logs into LogGroup.
func (c *Client) PutLogsWithCompressType(project, logstore string, lg *LogGroup, compressType int) (err error) {
ls := convertLogstore(c, project, logstore)
if err := ls.SetPutLogCompressType(compressType); err != nil {
return err
}
return ls.PutLogs(lg)
}
// PutRawLogWithCompressType put raw log data to log service, no marshal
func (c *Client) PutRawLogWithCompressType(project, logstore string, rawLogData []byte, compressType int) (err error) {
ls := convertLogstore(c, project, logstore)
if err := ls.SetPutLogCompressType(compressType); err != nil {
return err
}
return ls.PutRawLog(rawLogData)
}
// GetCursor gets log cursor of one shard specified by shardId.
// The from can be in three form: a) unix timestamp in seccond, b) "begin", c) "end".
// For more detail please read: https://help.aliyun.com/document_detail/29024.html
func (c *Client) GetCursor(project, logstore string, shardID int, from string) (cursor string, err error) {
ls := convertLogstore(c, project, logstore)
return ls.GetCursor(shardID, from)
}
// GetCursorTime ...
func (c *Client) GetCursorTime(project, logstore string, shardID int, cursor string) (cursorTime time.Time, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
urlVal := url.Values{}
urlVal.Add("cursor", cursor)
urlVal.Add("type", "cursor_time")
uri := fmt.Sprintf("/logstores/%v/shards/%v?%v", logstore, shardID, urlVal.Encode())
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return
}
defer r.Body.Close()
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return cursorTime, NewClientError(err)
}
type getCursorResult struct {
CursorTime int `json:"cursor_time"`
}
var rst getCursorResult
err = json.Unmarshal(buf, &rst)
return time.Unix(int64(rst.CursorTime), 0), err
}
// GetPrevCursorTime ...
func (c *Client) GetPrevCursorTime(project, logstore string, shardID int, cursor string) (cursorTime time.Time, err error) {
realCursor, err := base64E.StdEncoding.DecodeString(cursor)
if err != nil {
return cursorTime, NewClientError(err)
}
cursorVal, err := strconv.Atoi(string(realCursor))
if err != nil {
return cursorTime, NewClientError(err)
}
cursorVal--
nextCursor := base64E.StdEncoding.EncodeToString([]byte(strconv.Itoa(cursorVal)))
return c.GetCursorTime(project, logstore, shardID, nextCursor)
}
// GetLogsBytes gets logs binary data from shard specified by shardId according cursor and endCursor.
// The logGroupMaxCount is the max number of logGroup could be returned.
// The nextCursor is the next curosr can be used to read logs at next time.
func (c *Client) GetLogsBytes(project, logstore string, shardID int, cursor, endCursor string,
logGroupMaxCount int) (out []byte, nextCursor string, err error) {
ls := convertLogstore(c, project, logstore)
return ls.GetLogsBytes(shardID, cursor, endCursor, logGroupMaxCount)
}
// PullLogs gets logs from shard specified by shardId according cursor and endCursor.
// The logGroupMaxCount is the max number of logGroup could be returned.
// The nextCursor is the next cursor can be used to read logs at next time.
// @note if you want to pull logs continuous, set endCursor = ""
func (c *Client) PullLogs(project, logstore string, shardID int, cursor, endCursor string,
logGroupMaxCount int) (gl *LogGroupList, nextCursor string, err error) {
ls := convertLogstore(c, project, logstore)
return ls.PullLogs(shardID, cursor, endCursor, logGroupMaxCount)
}
// GetHistograms query logs with [from, to) time range
func (c *Client) GetHistograms(project, logstore string, topic string, from int64, to int64, queryExp string) (*GetHistogramsResponse, error) {
ls := convertLogstore(c, project, logstore)
return ls.GetHistograms(topic, from, to, queryExp)
}
// GetLogs query logs with [from, to) time range
func (c *Client) GetLogs(project, logstore string, topic string, from int64, to int64, queryExp string,
maxLineNum int64, offset int64, reverse bool) (*GetLogsResponse, error) {
ls := convertLogstore(c, project, logstore)
return ls.GetLogs(topic, from, to, queryExp, maxLineNum, offset, reverse)
}
// GetLogLines ...
func (c *Client) GetLogLines(project, logstore string, topic string, from int64, to int64, queryExp string,
maxLineNum int64, offset int64, reverse bool) (*GetLogLinesResponse, error) {
ls := convertLogstore(c, project, logstore)
return ls.GetLogLines(topic, from, to, queryExp, maxLineNum, offset, reverse)
}
// CreateIndex ...
func (c *Client) CreateIndex(project, logstore string, index Index) error {
ls := convertLogstore(c, project, logstore)
return ls.CreateIndex(index)
}
// UpdateIndex ...
func (c *Client) UpdateIndex(project, logstore string, index Index) error {
ls := convertLogstore(c, project, logstore)
return ls.UpdateIndex(index)
}
// GetIndex ...
func (c *Client) GetIndex(project, logstore string) (*Index, error) {
ls := convertLogstore(c, project, logstore)
return ls.GetIndex()
}
// CreateIndexString ...
func (c *Client) CreateIndexString(project, logstore string, index string) error {
ls := convertLogstore(c, project, logstore)
return ls.CreateIndexString(index)
}
// UpdateIndexString ...
func (c *Client) UpdateIndexString(project, logstore string, index string) error {
ls := convertLogstore(c, project, logstore)
return ls.UpdateIndexString(index)
}
// GetIndexString ...
func (c *Client) GetIndexString(project, logstore string) (string, error) {
ls := convertLogstore(c, project, logstore)
return ls.GetIndexString()
}
// DeleteIndex ...
func (c *Client) DeleteIndex(project, logstore string) error {
ls := convertLogstore(c, project, logstore)
return ls.DeleteIndex()
}
// ListSubStore ...
func (c *Client) ListSubStore(project, logstore string) (sortedSubStores []string, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/logstores/%v/substores", logstore)
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return
}
defer r.Body.Close()
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
if r.StatusCode != http.StatusOK {
errMsg := &Error{}
err = json.Unmarshal(buf, errMsg)
if err != nil {
err = fmt.Errorf("failed to remove config from machine group")
if IsDebugLevelMatched(1) {
dump, _ := httputil.DumpResponse(r, true)
level.Error(Logger).Log("msg", string(dump))
}
return
}
err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
return
}
type sortedSubStoreList struct {
SubStores []string `json:"substores"`
}
body := &sortedSubStoreList{}
err = json.Unmarshal(buf, body)
if err != nil {
return
}
sortedSubStores = body.SubStores
return
}
// GetSubStore ...
func (c *Client) GetSubStore(project, logstore, name string) (sortedSubStore *SubStore, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/logstores/%s/substores/%s", logstore, name)
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return
}
defer r.Body.Close()
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
if r.StatusCode != http.StatusOK {
errMsg := &Error{}
err = json.Unmarshal(buf, errMsg)
if err != nil {
err = fmt.Errorf("failed to remove config from machine group")
if IsDebugLevelMatched(1) {
dump, _ := httputil.DumpResponse(r, true)
level.Error(Logger).Log("msg", string(dump))
}
return
}
err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
return
}
sortedSubStore = &SubStore{}
err = json.Unmarshal(buf, sortedSubStore)
if err != nil {
sortedSubStore = nil
return
}
return
}
// CreateSubStore ...
func (c *Client) CreateSubStore(project, logstore string, sss *SubStore) (err error) {
body, err := json.Marshal(sss)
if err != nil {
return NewClientError(err)
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate",
}
r, err := c.request(project, "POST", fmt.Sprintf("/logstores/%s/substores", logstore), h, body)
if err != nil {
return NewClientError(err)
}
defer r.Body.Close()
body, err = ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return
}
// UpdateSubStore ...
func (c *Client) UpdateSubStore(project, logstore string, sss *SubStore) (err error) {
body, err := json.Marshal(sss)
if err != nil {
return NewClientError(err)
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate",
}
r, err := c.request(project, "PUT", fmt.Sprintf("/logstores/%s/substores/%s", logstore, sss.Name), h, body)
if err != nil {
return NewClientError(err)
}
defer r.Body.Close()
body, err = ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return
}
// DeleteSubStore ...
func (c *Client) DeleteSubStore(project, logstore string, name string) (err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
r, err := c.request(project, "DELETE", fmt.Sprintf("/logstores/%s/substores/%s", logstore, name), h, nil)
if err != nil {
return NewClientError(err)
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return
}
// GetSubStoreTTL ...
func (c *Client) GetSubStoreTTL(project, logstore string) (ttl int, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/logstores/%s/substores/storage/ttl", logstore)
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return
}
defer r.Body.Close()
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return
}
if r.StatusCode != http.StatusOK {
errMsg := &Error{}
err = json.Unmarshal(buf, errMsg)
if err != nil {
err = fmt.Errorf("failed to remove config from machine group")
if IsDebugLevelMatched(1) {
dump, _ := httputil.DumpResponse(r, true)
level.Error(Logger).Log("msg", string(dump))
}
return
}
err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
return
}
type ttlDef struct {
TTL int `json:"ttl"`
}
var ttlIns ttlDef
err = json.Unmarshal(buf, &ttlIns)
if err != nil {
return
}
return ttlIns.TTL, err
}
// UpdateSubStoreTTL ...
func (c *Client) UpdateSubStoreTTL(project, logstore string, ttl int) (err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
r, err := c.request(project, "PUT", fmt.Sprintf("/logstores/%s/substores/storage/ttl?ttl=%d", logstore, ttl), h, nil)
if err != nil {
return NewClientError(err)
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return
}