-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathlog_store.go
436 lines (368 loc) · 10.1 KB
/
log_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
package sls
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strconv"
lz4 "github.com/cloudflare/golz4"
"github.com/gogo/protobuf/proto"
"github.com/golang/glog"
)
// LogStore defines LogStore struct
type LogStore struct {
Name string `json:"logstoreName"`
TTL int
ShardCount int
CreateTime uint32
LastModifyTime uint32
project *LogProject
}
// Shard defines shard struct
type Shard struct {
ShardID int `json:"shardID"`
}
// ListShards returns shard id list of this logstore.
func (s *LogStore) ListShards() (shardIDs []int, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/logstores/%v/shards", s.Name)
r, err := request(s.project, "GET", uri, h, nil)
if err != nil {
return nil, NewClientError(err.Error())
}
buf, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := &Error{}
json.Unmarshal(buf, err)
return nil, err
}
var shards []*Shard
json.Unmarshal(buf, &shards)
for _, v := range shards {
shardIDs = append(shardIDs, v.ShardID)
}
return shardIDs, nil
}
// PutLogs put logs into logstore.
// The callers should transform user logs into LogGroup.
func (s *LogStore) PutLogs(lg *LogGroup) (err error) {
if len(lg.Logs) == 0 {
// empty log group
return nil
}
body, err := proto.Marshal(lg)
if err != nil {
return NewClientError(err.Error())
}
// Compresse body with lz4
out := make([]byte, lz4.CompressBound(body))
n, err := lz4.Compress(body, out)
if err != nil {
return NewClientError(err.Error())
}
h := map[string]string{
"x-log-compresstype": "lz4",
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/x-protobuf",
}
uri := fmt.Sprintf("/logstores/%v", s.Name)
r, err := request(s.project, "POST", uri, h, out[:n])
if err != nil {
return NewClientError(err.Error())
}
body, _ = ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return err
}
return nil
}
// 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: http://gitlab.alibaba-inc.com/sls/doc/blob/master/api/shard.md#logstore
func (s *LogStore) GetCursor(shardID int, from string) (cursor string, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/logstores/%v/shards/%v?type=cursor&from=%v",
s.Name, shardID, from)
r, err := request(s.project, "GET", uri, h, nil)
if err != nil {
return
}
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 get cursor")
dump, _ := httputil.DumpResponse(r, true)
if glog.V(1) {
glog.Error(string(dump))
}
return
}
err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
return
}
type Body struct {
Cursor string
}
body := &Body{}
err = json.Unmarshal(buf, body)
if err != nil {
return
}
cursor = body.Cursor
return
}
// 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 (s *LogStore) GetLogsBytes(shardID int, cursor, endCursor string,
logGroupMaxCount int) (out []byte, nextCursor string, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Accept": "application/x-protobuf",
"Accept-Encoding": "lz4",
}
uri := ""
if endCursor == "" {
uri = fmt.Sprintf("/logstores/%v/shards/%v?type=logs&cursor=%v&count=%v",
s.Name, shardID, cursor, logGroupMaxCount)
} else {
uri = fmt.Sprintf("/logstores/%v/shards/%v?type=logs&cursor=%v&end_cursor=%v&count=%v",
s.Name, shardID, cursor, endCursor, logGroupMaxCount)
}
r, err := request(s.project, "GET", uri, h, nil)
if err != nil {
return
}
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 get cursor")
dump, _ := httputil.DumpResponse(r, true)
if glog.V(1) {
glog.Error(string(dump))
}
return
}
err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
return
}
v, ok := r.Header["X-Log-Compresstype"]
if !ok || len(v) == 0 {
err = fmt.Errorf("can't find 'x-log-compresstype' header")
return
}
if v[0] != "lz4" {
err = fmt.Errorf("unexpected compress type:%v", v[0])
return
}
v, ok = r.Header["X-Log-Cursor"]
if !ok || len(v) == 0 {
err = fmt.Errorf("can't find 'x-log-cursor' header")
return
}
nextCursor = v[0]
v, ok = r.Header["X-Log-Bodyrawsize"]
if !ok || len(v) == 0 {
err = fmt.Errorf("can't find 'x-log-bodyrawsize' header")
return
}
bodyRawSize, err := strconv.Atoi(v[0])
if err != nil {
return
}
out = make([]byte, bodyRawSize)
err = lz4.Uncompress(buf, out)
if err != nil {
return
}
return
}
// LogsBytesDecode decodes logs binary data returned by GetLogsBytes API
func LogsBytesDecode(data []byte) (gl *LogGroupList, err error) {
gl = &LogGroupList{}
err = proto.Unmarshal(data, gl)
if err != nil {
return nil, err
}
return gl, nil
}
// 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.
func (s *LogStore) PullLogs(shardID int, cursor, endCursor string,
logGroupMaxCount int) (gl *LogGroupList, nextCursor string, err error) {
out, nextCursor, err := s.GetLogsBytes(shardID, cursor, endCursor, logGroupMaxCount)
if err != nil {
return nil, "", err
}
gl, err = LogsBytesDecode(out)
if err != nil {
return nil, "", err
}
return gl, nextCursor, nil
}
// GetHistograms query logs with [from, to) time range
func (s *LogStore) GetHistograms(topic string, from int64, to int64, queryExp string) (*GetHistogramsResponse, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Accept": "application/json",
}
uri := fmt.Sprintf("/logstores/%v?type=histogram&topic=%v&from=%v&to=%v&query=%v", s.Name, topic, from, to, queryExp)
r, err := request(s.project, "GET", uri, h, nil)
if err != nil {
return nil, NewClientError(err.Error())
}
body, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return nil, err
}
histograms := []SingleHistogram{}
err = json.Unmarshal(body, &histograms)
if err != nil {
return nil, err
}
count, err := strconv.ParseInt(r.Header[GetLogsCountHeader][0], 10, 32)
if err != nil {
return nil, err
}
getHistogramsResponse := GetHistogramsResponse{
Progress: r.Header[ProgressHeader][0],
Count: count,
Histograms: histograms,
}
return &getHistogramsResponse, nil
}
// GetLogs query logs with [from, to) time range
func (s *LogStore) GetLogs(topic string, from int64, to int64, queryExp string,
maxLineNum int64, offset int64, reverse bool) (*GetLogsResponse, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Accept": "application/json",
}
uri := fmt.Sprintf("/logstores/%v?type=log&topic=%v&from=%v&to=%v&query=%v&line=%v&offset=%v&reverse=%v", s.Name, topic, from, to, queryExp, maxLineNum, offset, reverse)
r, err := request(s.project, "GET", uri, h, nil)
if err != nil {
return nil, NewClientError(err.Error())
}
body, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
json.Unmarshal(body, err)
return nil, err
}
logs := []map[string]string{}
err = json.Unmarshal(body, &logs)
if err != nil {
return nil, err
}
count, err := strconv.ParseInt(r.Header[GetLogsCountHeader][0], 10, 32)
if err != nil {
return nil, err
}
getLogsResponse := GetLogsResponse{
Progress: r.Header[ProgressHeader][0],
Count: count,
Logs: logs,
}
return &getLogsResponse, nil
}
// CreateIndex ...
func (s *LogStore) CreateIndex(index Index) error {
body, err := json.Marshal(index)
if err != nil {
return err
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
uri := fmt.Sprintf("/logstores/%s/index", s.Name)
_, err = request(s.project, "POST", uri, h, body)
return err
}
// UpdateIndex ...
func (s *LogStore) UpdateIndex(index Index) error {
body, err := json.Marshal(index)
if err != nil {
return err
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
uri := fmt.Sprintf("/logstores/%s/index", s.Name)
_, err = request(s.project, "PUT", uri, h, body)
return err
}
// DeleteIndex ...
func (s *LogStore) DeleteIndex() error {
type Body struct {
project string `json:"projectName"`
store string `json:"logstoreName"`
}
body, err := json.Marshal(Body{
project: s.project.Name,
store: s.Name,
})
if err != nil {
return err
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
uri := fmt.Sprintf("/logstores/%s/index", s.Name)
_, err = request(s.project, "DELETE", uri, h, body)
return err
}
func (s *LogStore) GetIndex() (*Index, error) {
type Body struct {
project string `json:"projectName"`
store string `json:"logstoreName"`
}
body, err := json.Marshal(Body{
project: s.project.Name,
store: s.Name,
})
if err != nil {
return nil, err
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
uri := fmt.Sprintf("/logstores/%s/index", s.Name)
resp, err := request(s.project, "GET", uri, h, body)
if err != nil {
}
index := &Index{}
data, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(data, index)
if err != nil {
return nil, err
}
return index, err
}