diff --git a/client_interface.go b/client_interface.go index 9f6a710b..80556dbf 100644 --- a/client_interface.go +++ b/client_interface.go @@ -189,6 +189,9 @@ type ClientInterface interface { GetLogLines(project, logstore string, topic string, from int64, to int64, queryExp string, maxLineNum int64, offset int64, reverse bool) (*GetLogLinesResponse, error) + GetLogsV2(project, logstore string, req *GetLogRequest) (*GetLogsResponse, error) + GetLogLinesV2(project, logstore string, req *GetLogRequest) (*GetLogLinesResponse, error) + // #################### Index Operations ##################### // CreateIndex ... CreateIndex(project, logstore string, index Index) error diff --git a/client_store.go b/client_store.go index df008b65..ebd1181f 100644 --- a/client_store.go +++ b/client_store.go @@ -197,6 +197,18 @@ func (c *Client) GetLogLines(project, logstore string, topic string, from int64, return ls.GetLogLines(topic, from, to, queryExp, maxLineNum, offset, reverse) } +// GetLogsV2 ... +func (c *Client) GetLogsV2(project, logstore string, req *GetLogRequest) (*GetLogsResponse, error) { + ls := convertLogstore(c, project, logstore) + return ls.GetLogsV2(req) +} + +// GetLogLinesV2 ... +func (c *Client) GetLogLinesV2(project, logstore string, req *GetLogRequest) (*GetLogLinesResponse, error) { + ls := convertLogstore(c, project, logstore) + return ls.GetLogLinesV2(req) +} + // CreateIndex ... func (c *Client) CreateIndex(project, logstore string, index Index) error { ls := convertLogstore(c, project, logstore) diff --git a/log_store.go b/log_store.go index 69d5d5c5..039578b1 100644 --- a/log_store.go +++ b/log_store.go @@ -534,23 +534,14 @@ func (s *LogStore) GetHistograms(topic string, from int64, to int64, queryExp st } // 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) (*http.Response, []byte, *GetLogsResponse, error) { +func (s *LogStore) getLogs(req *GetLogRequest) (*http.Response, []byte, *GetLogsResponse, error) { h := map[string]string{ "x-log-bodyrawsize": "0", "Accept": "application/json", } - urlVal := url.Values{} - urlVal.Add("type", "log") - urlVal.Add("from", strconv.Itoa(int(from))) - urlVal.Add("to", strconv.Itoa(int(to))) - urlVal.Add("topic", topic) - urlVal.Add("line", strconv.Itoa(int(maxLineNum))) - urlVal.Add("offset", strconv.Itoa(int(offset))) - urlVal.Add("reverse", strconv.FormatBool(reverse)) - urlVal.Add("query", queryExp) + urlVal := req.ToURLParams() uri := fmt.Sprintf("/logstores/%s?%s", s.Name, urlVal.Encode()) r, err := request(s.project, "GET", uri, h, nil) @@ -591,11 +582,24 @@ func (s *LogStore) getLogs(topic string, from int64, to int64, queryExp string, }, nil } -// GetJsonLogs query logs with [from, to) time range +// GetLogLines query logs with [from, to) time range func (s *LogStore) GetLogLines(topic string, from int64, to int64, queryExp string, maxLineNum int64, offset int64, reverse bool) (*GetLogLinesResponse, error) { - rsp, b, logRsp, err := s.getLogs(topic, from, to, queryExp, maxLineNum, offset, reverse) + var req GetLogRequest + req.Topic = topic + req.From = from + req.To = to + req.Query = queryExp + req.Lines = maxLineNum + req.Offset = offset + req.Reverse = reverse + return s.GetLogLinesV2(&req) +} + +// GetLogLinesV2 query logs with [from, to) time range +func (s *LogStore) GetLogLinesV2(req *GetLogRequest) (*GetLogLinesResponse, error) { + rsp, b, logRsp, err := s.getLogs(req) if err != nil { return nil, err } @@ -616,8 +620,20 @@ func (s *LogStore) GetLogLines(topic string, from int64, to int64, queryExp stri // 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) { + var req GetLogRequest + req.Topic = topic + req.From = from + req.To = to + req.Query = queryExp + req.Lines = maxLineNum + req.Offset = offset + req.Reverse = reverse + return s.GetLogsV2(&req) +} - rsp, b, logRsp, err := s.getLogs(topic, from, to, queryExp, maxLineNum, offset, reverse) +// GetLogsV2 query logs with [from, to) time range +func (s *LogStore) GetLogsV2(req *GetLogRequest) (*GetLogsResponse, error) { + rsp, b, logRsp, err := s.getLogs(req) if err == nil && len(b) != 0 { logs := []map[string]string{} err = json.Unmarshal(b, &logs) @@ -626,7 +642,6 @@ func (s *LogStore) GetLogs(topic string, from int64, to int64, queryExp string, } logRsp.Logs = logs } - return logRsp, err } diff --git a/logstore_test.go b/logstore_test.go index 07035b30..d3167b49 100644 --- a/logstore_test.go +++ b/logstore_test.go @@ -256,6 +256,7 @@ func (s *LogstoreTestSuite) TestGetLogs() { lResp, lErr := s.Logstore.GetLogs("", int64(beginTime), int64(endTime), "InternalServerError", 100, 0, false) s.Nil(lErr) s.Equal(lResp.Count, int64(logCount)) + fmt.Println(*lResp) } func (s *LogstoreTestSuite) TestLogstore() { diff --git a/model.go b/model.go index 243460fe..e67f383b 100644 --- a/model.go +++ b/model.go @@ -2,9 +2,37 @@ package sls import ( "encoding/json" + "net/url" + "strconv" "strings" ) +// GetLogRequest for GetLogsV2 +type GetLogRequest struct { + From int64 // unix time, eg time.Now().Unix() - 900 + To int64 // unix time, eg time.Now().Unix() + Topic string // @note topic is not used anymore, use __topic__ : xxx in query instead + Lines int64 // max 100; offset, lines and reverse is ignored when use SQL in query + Offset int64 + Reverse bool + Query string + PowerSQL bool +} + +func (glr *GetLogRequest) ToURLParams() url.Values { + urlVal := url.Values{} + urlVal.Add("type", "log") + urlVal.Add("from", strconv.Itoa(int(glr.From))) + urlVal.Add("to", strconv.Itoa(int(glr.To))) + urlVal.Add("topic", glr.Topic) + urlVal.Add("line", strconv.Itoa(int(glr.Lines))) + urlVal.Add("offset", strconv.Itoa(int(glr.Offset))) + urlVal.Add("reverse", strconv.FormatBool(glr.Reverse)) + urlVal.Add("powerSql", strconv.FormatBool(glr.PowerSQL)) + urlVal.Add("query", glr.Query) + return urlVal +} + // GetHistogramsResponse defines response from GetHistograms call type SingleHistogram struct { Progress string `json:"progress"` @@ -36,7 +64,7 @@ type GetLogsResponse struct { // note: GetLogLinesResponse.Logs is nil when use GetLogLinesResponse type GetLogLinesResponse struct { GetLogsResponse - Lines []json.RawMessage + Lines []json.RawMessage } func (resp *GetLogsResponse) IsComplete() bool { diff --git a/token_auto_update_client.go b/token_auto_update_client.go index 9dc5eaad..b8c2bf75 100644 --- a/token_auto_update_client.go +++ b/token_auto_update_client.go @@ -669,6 +669,26 @@ func (c *TokenAutoUpdateClient) GetHistograms(project, logstore string, topic st return } +func (c *TokenAutoUpdateClient) GetLogsV2(project, logstore string, req *GetLogRequest) (r *GetLogsResponse, err error) { + for i := 0; i < c.maxTryTimes; i++ { + r, err = c.logClient.GetLogsV2(project, logstore, req) + if !c.processError(err) { + return + } + } + return +} + +func (c *TokenAutoUpdateClient) GetLogLinesV2(project, logstore string, req *GetLogRequest) (r *GetLogLinesResponse, err error) { + for i := 0; i < c.maxTryTimes; i++ { + r, err = c.logClient.GetLogLinesV2(project, logstore, req) + if !c.processError(err) { + return + } + } + return +} + func (c *TokenAutoUpdateClient) GetLogs(project, logstore string, topic string, from int64, to int64, queryExp string, maxLineNum int64, offset int64, reverse bool) (r *GetLogsResponse, err error) { for i := 0; i < c.maxTryTimes; i++ {