lib/protoparser: handle unexpected EOF error when parsing lines in prometheus exposition format (#4851)

Previously only io.EOF was handled, and io.ErrUnexpectedEOF was ignored, but it may happen if the client interrupts the connection.

https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4817
(cherry picked from commit b7d07e5acf)
This commit is contained in:
Dmytro Kozlov 2023-08-18 08:55:42 +02:00 committed by hagen1778
parent 07a3030856
commit 1929d3bca9
No known key found for this signature in database
GPG key ID: 3BF75F3741CA9640
3 changed files with 29 additions and 1 deletions

View file

@ -25,6 +25,7 @@ The following `tip` changes can be tested by building VictoriaMetrics components
## tip ## tip
* FEATURE: [vmbackup](https://docs.victoriametrics.com/vmbackup.html): add support for server-side copy of existing backups. See [these docs](https://docs.victoriametrics.com/vmbackup.html#server-side-copy-of-the-existing-backup) for details. * FEATURE: [vmbackup](https://docs.victoriametrics.com/vmbackup.html): add support for server-side copy of existing backups. See [these docs](https://docs.victoriametrics.com/vmbackup.html#server-side-copy-of-the-existing-backup) for details.
* FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): properly handle `unexpected EOF` error when parsing metrics in Prometheus exposition format. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4817).
* BUGFIX: do not allow starting VictoriaMetrics components with improperly set boolean command-line flags in the form `-boolFlagName value`, since this leads to silent incomplete flags' parsing. This form should be replaced with `-boolFlagName=value`. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4845). * BUGFIX: do not allow starting VictoriaMetrics components with improperly set boolean command-line flags in the form `-boolFlagName value`, since this leads to silent incomplete flags' parsing. This form should be replaced with `-boolFlagName=value`. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4845).
* BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): properly set labels from `-remoteWrite.label` command-line flag just before sending samples to the configured `-remoteWrite.url` according to [these docs](https://docs.victoriametrics.com/vmagent.html#adding-labels-to-metrics). Previously these labels were incorrectly set before [the relabeling](https://docs.victoriametrics.com/vmagent.html#relabeling) configured via `-remoteWrite.urlRelabelConfigs` and [the stream aggregation](https://docs.victoriametrics.com/stream-aggregation.html) configured via `-remoteWrite.streamAggr.config`, so these labels could be lost or incorrectly transformed before sending the samples to remote storage. The fix allows using `-remoteWrite.label` for identifying `vmagent` instances in [cluster mode](https://docs.victoriametrics.com/vmagent.html#scraping-big-number-of-targets). See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4247) and [these docs](https://docs.victoriametrics.com/stream-aggregation.html#cluster-mode) for more details. * BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): properly set labels from `-remoteWrite.label` command-line flag just before sending samples to the configured `-remoteWrite.url` according to [these docs](https://docs.victoriametrics.com/vmagent.html#adding-labels-to-metrics). Previously these labels were incorrectly set before [the relabeling](https://docs.victoriametrics.com/vmagent.html#relabeling) configured via `-remoteWrite.urlRelabelConfigs` and [the stream aggregation](https://docs.victoriametrics.com/stream-aggregation.html) configured via `-remoteWrite.streamAggr.config`, so these labels could be lost or incorrectly transformed before sending the samples to remote storage. The fix allows using `-remoteWrite.label` for identifying `vmagent` instances in [cluster mode](https://docs.victoriametrics.com/vmagent.html#scraping-big-number-of-targets). See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4247) and [these docs](https://docs.victoriametrics.com/stream-aggregation.html#cluster-mode) for more details.

View file

@ -93,7 +93,7 @@ again:
} }
func isEOFLikeError(err error) bool { func isEOFLikeError(err error) bool {
if errors.Is(err, io.EOF) { if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
return true return true
} }
s := err.Error() s := err.Error()

View file

@ -2,6 +2,7 @@ package common
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"reflect" "reflect"
@ -25,6 +26,20 @@ func TestReadLinesBlockFailure(t *testing.T) {
if _, _, err := ReadLinesBlock(fr, nil, nil); err == nil { if _, _, err := ReadLinesBlock(fr, nil, nil); err == nil {
t.Fatalf("expecting non-nil error") t.Fatalf("expecting non-nil error")
} }
un := &unexpectedEOF{}
if _, _, err := ReadLinesBlock(un, nil, nil); err != nil {
if !errors.Is(err, io.EOF) {
t.Fatalf("get unexpected error, expecting io.EOF")
}
}
ef := eofErr{}
if _, _, err := ReadLinesBlock(ef, nil, nil); err != nil {
if !errors.Is(err, io.EOF) {
t.Fatalf("get unexpected error, expecting io.EOF")
}
}
} }
// empty string // empty string
@ -41,6 +56,18 @@ func (fr *failureReader) Read(p []byte) (int, error) {
return 0, fmt.Errorf("some error") return 0, fmt.Errorf("some error")
} }
type unexpectedEOF struct{}
func (un unexpectedEOF) Read(p []byte) (int, error) {
return 0, io.ErrUnexpectedEOF
}
type eofErr struct{}
func (eo eofErr) Read(p []byte) (int, error) {
return 0, io.EOF
}
func TestReadLinesBlockMultiLinesSingleByteReader(t *testing.T) { func TestReadLinesBlockMultiLinesSingleByteReader(t *testing.T) {
f := func(s string, linesExpected []string) { f := func(s string, linesExpected []string) {
t.Helper() t.Helper()