Skip to content

Commit 249c6f7

Browse files
authored
feat: Get logs support zstd (#362)
1 parent 67ccac4 commit 249c6f7

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

log_store.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -599,32 +599,43 @@ func (s *LogStore) GetLogsBytesWithQuery(plr *PullLogRequest) ([]byte, *PullLogM
599599
}
600600

601601
// decompress data
602+
out, err := decompressResponse(rawSize, buf, r)
603+
if err != nil {
604+
return nil, nil, err
605+
}
606+
return out, pullMeta, nil
607+
}
608+
609+
func decompressResponse(rawSize int, compressedBody []byte, r *http.Response) ([]byte, error) {
610+
if rawSize == 0 {
611+
return make([]byte, 0), nil
612+
}
602613
out := make([]byte, rawSize)
603614
compressType, err := parseHeaderString(r.Header, "X-Log-Compresstype")
604615
if err != nil {
605-
return nil, nil, err
616+
return nil, err
606617
}
607618
switch compressType {
608619
case "lz4":
609620
uncompressedSize := 0
610-
if uncompressedSize, err = lz4.UncompressBlock(buf, out); err != nil {
611-
return nil, nil, err
621+
if uncompressedSize, err = lz4.UncompressBlock(compressedBody, out); err != nil {
622+
return nil, err
612623
}
613624
if uncompressedSize != rawSize {
614-
return nil, nil, fmt.Errorf("uncompressed size %d does not match 'x-log-bodyrawsize' %d", uncompressedSize, rawSize)
625+
return nil, fmt.Errorf("uncompressed size %d does not match 'x-log-bodyrawsize' %d", uncompressedSize, rawSize)
615626
}
616627
case "zstd":
617-
out, err = slsZstdCompressor.Decompress(buf, out)
628+
out, err = slsZstdCompressor.Decompress(compressedBody, out)
618629
if err != nil {
619-
return nil, nil, err
630+
return nil, err
620631
}
621632
if len(out) != rawSize {
622-
return nil, nil, fmt.Errorf("uncompressed size %d does not match 'x-log-bodyrawsize' %d", len(out), rawSize)
633+
return nil, fmt.Errorf("uncompressed size %d does not match 'x-log-bodyrawsize' %d", len(out), rawSize)
623634
}
624635
default:
625-
return nil, nil, fmt.Errorf("unexpected compress type: %s", compressType)
636+
return nil, fmt.Errorf("unexpected compress type: %s", compressType)
626637
}
627-
return out, pullMeta, nil
638+
return out, nil
628639
}
629640

630641
// LogsBytesDecode decodes logs binary data returned by GetLogsBytes API
@@ -977,7 +988,11 @@ func (s *LogStore) getLogsV3Internal(req *GetLogRequest) (*GetLogsV3Response, *h
977988
h := map[string]string{
978989
"x-log-bodyrawsize": fmt.Sprintf("%v", len(reqBody)),
979990
"Content-Type": "application/json",
980-
"Accept-Encoding": "lz4",
991+
}
992+
if req.CompressType == Compress_ZSTD {
993+
h["Accept-Encoding"] = "zstd"
994+
} else {
995+
h["Accept-Encoding"] = "lz4"
981996
}
982997
uri := fmt.Sprintf("/logstores/%s/logs", s.Name)
983998
r, err := request(s.project, "POST", uri, h, reqBody)
@@ -1003,12 +1018,9 @@ func (s *LogStore) getLogsV3Internal(req *GetLogRequest) (*GetLogsV3Response, *h
10031018
if err != nil {
10041019
return nil, nil, NewBadResponseError(string(respBody), r.Header, r.StatusCode)
10051020
}
1006-
out := make([]byte, bodyRawSize)
1007-
if bodyRawSize != 0 {
1008-
len, err := lz4.UncompressBlock(respBody, out)
1009-
if err != nil || int64(len) != bodyRawSize {
1010-
return nil, nil, NewBadResponseError(string(respBody), r.Header, r.StatusCode)
1011-
}
1021+
out, err := decompressResponse(int(bodyRawSize), respBody, r)
1022+
if err != nil {
1023+
return nil, nil, err
10121024
}
10131025
respBody = out
10141026
}

model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type GetLogRequest struct {
2424
ToNsPart int32 `json:"toNs"`
2525
NeedHighlight bool `json:"highlight"`
2626
IsAccurate bool `json:"accurate"`
27+
CompressType int `json:"-"` // Compress_ZSTD or Compress_LZ4, default is Compress_LZ4
2728
}
2829

2930
func (glr *GetLogRequest) ToURLParams() url.Values {

0 commit comments

Comments
 (0)