Skip to content

[confighttp] snappy compress, Encode and NewReader inconsistent behavior #10584

Closed
@tomatopunk

Description

@tomatopunk

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:

"snappy": func(body io.ReadCloser) (io.ReadCloser, error) {

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.

code: https://github.com/prometheus/prometheus/blob/c173cd57c921f582586fc725ad51124728757533/cmd/promtool/metrics.go#L104

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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions