Description
Hello team, I hope you are having a good day.
Describe the bug
I am coding on the collect-contrib, about on a new component to receive prometheus_remote_write.
I have encountered a potential bug related to inconsistent behavior in Snappy compression when using encode -> NewReader and NewBufferedWriter -> decode.
When we compress or decompress from a stream, or read all of it and then compress or decompress it, we get different results
Steps to reproduce
What did you expect to see?
package main
import (
"bytes"
"fmt"
"github.com/golang/snappy"
"io"
)
func main() {
test1()
test2()
}
func test1() {
data := []byte("Hello, World!")
//encode
compressed := snappy.Encode(nil, data)
//decode
reader := bytes.NewReader(compressed)
sr := snappy.NewReader(reader)
sb := new(bytes.Buffer)
_, err := io.Copy(sb, sr)
if err != nil {
println("test1 decode error:", err.Error())
return
}
fmt.Printf("test1 string1: %s, 2: %s", string(data), string(sb.Bytes()))
}
func test2() {
data := []byte("Hello, World!")
//encode
var buf bytes.Buffer
sw := snappy.NewBufferedWriter(&buf)
_, err := sw.Write(data)
if err != nil {
println("encode error")
return
}
//decode
byteSlice := buf.Bytes()
reqBuf, err := snappy.Decode(nil, byteSlice)
if err != nil {
println("test2 decode error:", err.Error())
return
}
fmt.Printf("test1 string1: %s, 2: %s", string(data), string(reqBuf))
}
What did you see instead?
maybe should be correctly code/decode by snappy
What version did you use?
main branch
What config did you use?
code:
Environment
go 1.22
Additional context
In the case of Prometheus and OpenTelemetry collection, Prometheus uses encode and decode, while OpenTelemetry uses NewReader and NewBufferedWriter. However, I believe that whether you decode/encode in stream or read all and then encode/decode, the same protocol should yield the same results, and there should be no parsing failures.
I would like to know how to address this bug. Should we consider skipping Snappy? If any fixes are required, I am willing to help resolve this issue.