github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/chunkcompat/compat.go (about) 1 package chunkcompat 2 3 import ( 4 "bytes" 5 6 "github.com/prometheus/common/model" 7 "github.com/prometheus/prometheus/model/labels" 8 9 "github.com/grafana/loki/pkg/ingester/client" 10 "github.com/grafana/loki/pkg/storage/chunk" 11 ) 12 13 // FromChunks converts []client.Chunk to []chunk.Chunk. 14 func FromChunks(userID string, metric labels.Labels, in []client.Chunk) ([]chunk.Chunk, error) { 15 out := make([]chunk.Chunk, 0, len(in)) 16 for _, i := range in { 17 o, err := chunk.NewForEncoding(chunk.Encoding(byte(i.Encoding))) 18 if err != nil { 19 return nil, err 20 } 21 22 if err := o.UnmarshalFromBuf(i.Data); err != nil { 23 return nil, err 24 } 25 26 firstTime, lastTime := model.Time(i.StartTimestampMs), model.Time(i.EndTimestampMs) 27 // As the lifetime of this chunk is scopes to this request, we don't need 28 // to supply a fingerprint. 29 out = append(out, chunk.NewChunk(userID, 0, metric, o, firstTime, lastTime)) 30 } 31 return out, nil 32 } 33 34 // ToChunks converts []chunk.Chunk to []client.Chunk. 35 func ToChunks(in []chunk.Chunk) ([]client.Chunk, error) { 36 out := make([]client.Chunk, 0, len(in)) 37 for _, i := range in { 38 wireChunk := client.Chunk{ 39 StartTimestampMs: int64(i.From), 40 EndTimestampMs: int64(i.Through), 41 Encoding: int32(i.Data.Encoding()), 42 } 43 44 buf := bytes.NewBuffer(make([]byte, 0, 1024)) 45 if err := i.Data.Marshal(buf); err != nil { 46 return nil, err 47 } 48 49 wireChunk.Data = buf.Bytes() 50 out = append(out, wireChunk) 51 } 52 return out, nil 53 }