log_store.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package alils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httputil"
  8. "strconv"
  9. lz4 "github.com/cloudflare/golz4"
  10. "github.com/gogo/protobuf/proto"
  11. )
  12. type LogStore struct {
  13. Name string `json:"logstoreName"`
  14. TTL int
  15. ShardCount int
  16. CreateTime uint32
  17. LastModifyTime uint32
  18. project *LogProject
  19. }
  20. type Shard struct {
  21. ShardID int `json:"shardID"`
  22. }
  23. // ListShards returns shard id list of this logstore.
  24. func (s *LogStore) ListShards() (shardIDs []int, err error) {
  25. h := map[string]string{
  26. "x-sls-bodyrawsize": "0",
  27. }
  28. uri := fmt.Sprintf("/logstores/%v/shards", s.Name)
  29. r, err := request(s.project, "GET", uri, h, nil)
  30. if err != nil {
  31. return
  32. }
  33. buf, err := ioutil.ReadAll(r.Body)
  34. if err != nil {
  35. return
  36. }
  37. if r.StatusCode != http.StatusOK {
  38. errMsg := &errorMessage{}
  39. err = json.Unmarshal(buf, errMsg)
  40. if err != nil {
  41. err = fmt.Errorf("failed to list logstore")
  42. dump, _ := httputil.DumpResponse(r, true)
  43. fmt.Println(dump)
  44. return
  45. }
  46. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  47. return
  48. }
  49. var shards []*Shard
  50. err = json.Unmarshal(buf, &shards)
  51. if err != nil {
  52. return
  53. }
  54. for _, v := range shards {
  55. shardIDs = append(shardIDs, v.ShardID)
  56. }
  57. return
  58. }
  59. // PutLogs put logs into logstore.
  60. // The callers should transform user logs into LogGroup.
  61. func (s *LogStore) PutLogs(lg *LogGroup) (err error) {
  62. body, err := proto.Marshal(lg)
  63. if err != nil {
  64. return
  65. }
  66. // Compresse body with lz4
  67. out := make([]byte, lz4.CompressBound(body))
  68. n, err := lz4.Compress(body, out)
  69. if err != nil {
  70. return
  71. }
  72. h := map[string]string{
  73. "x-sls-compresstype": "lz4",
  74. "x-sls-bodyrawsize": fmt.Sprintf("%v", len(body)),
  75. "Content-Type": "application/x-protobuf",
  76. }
  77. uri := fmt.Sprintf("/logstores/%v", s.Name)
  78. r, err := request(s.project, "POST", uri, h, out[:n])
  79. if err != nil {
  80. return
  81. }
  82. buf, err := ioutil.ReadAll(r.Body)
  83. if err != nil {
  84. return
  85. }
  86. if r.StatusCode != http.StatusOK {
  87. errMsg := &errorMessage{}
  88. err = json.Unmarshal(buf, errMsg)
  89. if err != nil {
  90. err = fmt.Errorf("failed to put logs")
  91. dump, _ := httputil.DumpResponse(r, true)
  92. fmt.Println(dump)
  93. return
  94. }
  95. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  96. return
  97. }
  98. return
  99. }
  100. // GetCursor gets log cursor of one shard specified by shardId.
  101. // The from can be in three form: a) unix timestamp in seccond, b) "begin", c) "end".
  102. // For more detail please read: http://gitlab.alibaba-inc.com/sls/doc/blob/master/api/shard.md#logstore
  103. func (s *LogStore) GetCursor(shardId int, from string) (cursor string, err error) {
  104. h := map[string]string{
  105. "x-sls-bodyrawsize": "0",
  106. }
  107. uri := fmt.Sprintf("/logstores/%v/shards/%v?type=cursor&from=%v",
  108. s.Name, shardId, from)
  109. r, err := request(s.project, "GET", uri, h, nil)
  110. if err != nil {
  111. return
  112. }
  113. buf, err := ioutil.ReadAll(r.Body)
  114. if err != nil {
  115. return
  116. }
  117. if r.StatusCode != http.StatusOK {
  118. errMsg := &errorMessage{}
  119. err = json.Unmarshal(buf, errMsg)
  120. if err != nil {
  121. err = fmt.Errorf("failed to get cursor")
  122. dump, _ := httputil.DumpResponse(r, true)
  123. fmt.Println(dump)
  124. return
  125. }
  126. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  127. return
  128. }
  129. type Body struct {
  130. Cursor string
  131. }
  132. body := &Body{}
  133. err = json.Unmarshal(buf, body)
  134. if err != nil {
  135. return
  136. }
  137. cursor = body.Cursor
  138. return
  139. }
  140. // GetLogsBytes gets logs binary data from shard specified by shardId according cursor.
  141. // The logGroupMaxCount is the max number of logGroup could be returned.
  142. // The nextCursor is the next curosr can be used to read logs at next time.
  143. func (s *LogStore) GetLogsBytes(shardId int, cursor string,
  144. logGroupMaxCount int) (out []byte, nextCursor string, err error) {
  145. h := map[string]string{
  146. "x-sls-bodyrawsize": "0",
  147. "Accept": "application/x-protobuf",
  148. "Accept-Encoding": "lz4",
  149. }
  150. uri := fmt.Sprintf("/logstores/%v/shards/%v?type=logs&cursor=%v&count=%v",
  151. s.Name, shardId, cursor, logGroupMaxCount)
  152. r, err := request(s.project, "GET", uri, h, nil)
  153. if err != nil {
  154. return
  155. }
  156. buf, err := ioutil.ReadAll(r.Body)
  157. if err != nil {
  158. return
  159. }
  160. if r.StatusCode != http.StatusOK {
  161. errMsg := &errorMessage{}
  162. err = json.Unmarshal(buf, errMsg)
  163. if err != nil {
  164. err = fmt.Errorf("failed to get cursor")
  165. dump, _ := httputil.DumpResponse(r, true)
  166. fmt.Println(dump)
  167. return
  168. }
  169. err = fmt.Errorf("%v:%v", errMsg.Code, errMsg.Message)
  170. return
  171. }
  172. v, ok := r.Header["X-Sls-Compresstype"]
  173. if !ok || len(v) == 0 {
  174. err = fmt.Errorf("can't find 'x-sls-compresstype' header")
  175. return
  176. }
  177. if v[0] != "lz4" {
  178. err = fmt.Errorf("unexpected compress type:%v", v[0])
  179. return
  180. }
  181. v, ok = r.Header["X-Sls-Cursor"]
  182. if !ok || len(v) == 0 {
  183. err = fmt.Errorf("can't find 'x-sls-cursor' header")
  184. return
  185. }
  186. nextCursor = v[0]
  187. v, ok = r.Header["X-Sls-Bodyrawsize"]
  188. if !ok || len(v) == 0 {
  189. err = fmt.Errorf("can't find 'x-sls-bodyrawsize' header")
  190. return
  191. }
  192. bodyRawSize, err := strconv.Atoi(v[0])
  193. if err != nil {
  194. return
  195. }
  196. out = make([]byte, bodyRawSize)
  197. err = lz4.Uncompress(buf, out)
  198. if err != nil {
  199. return
  200. }
  201. return
  202. }
  203. // LogsBytesDecode decodes logs binary data retruned by GetLogsBytes API
  204. func LogsBytesDecode(data []byte) (gl *LogGroupList, err error) {
  205. gl = &LogGroupList{}
  206. err = proto.Unmarshal(data, gl)
  207. if err != nil {
  208. return
  209. }
  210. return
  211. }
  212. // GetLogs gets logs from shard specified by shardId according cursor.
  213. // The logGroupMaxCount is the max number of logGroup could be returned.
  214. // The nextCursor is the next curosr can be used to read logs at next time.
  215. func (s *LogStore) GetLogs(shardId int, cursor string,
  216. logGroupMaxCount int) (gl *LogGroupList, nextCursor string, err error) {
  217. out, nextCursor, err := s.GetLogsBytes(shardId, cursor, logGroupMaxCount)
  218. if err != nil {
  219. return
  220. }
  221. gl, err = LogsBytesDecode(out)
  222. if err != nil {
  223. return
  224. }
  225. return
  226. }