Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/vtagent/opentelemetry/otlpgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func otlpExportTracesHandler(r *http.Request, w http.ResponseWriter) {
err = protoparserutil.ReadUncompressedData(bytes.NewReader(message), encoding, maxRequestSize, pushExportTraceServiceRequest)
if err != nil {
errorsGRPCTotal.Inc()
grpc.Errorf(w, r, grpc.StatusCodeInternal, fmt.Sprintf("cannot read OpenTelemetry protocol data: %s", err))
grpc.Errorf(w, r, grpc.StatusCodeFromError(err), fmt.Sprintf("cannot read OpenTelemetry protocol data: %s", err))
return
}

Expand Down
50 changes: 50 additions & 0 deletions app/vtagent/opentelemetry/otlpgrpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package opentelemetry

import (
"bytes"
"encoding/binary"
"flag"
"net/http"
"net/http/httptest"
"testing"

"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"

"github.com/VictoriaMetrics/VictoriaTraces/lib/grpc"
)

func newGRPCFrame(payload []byte) []byte {
frame := make([]byte, 5+len(payload))
binary.BigEndian.PutUint32(frame[1:5], uint32(len(payload)))
copy(frame[5:], payload)
return frame
}

func TestOTLPGRPCExportStatusCodes(t *testing.T) {
t.Run("busy write concurrency limit returns UNAVAILABLE", func(t *testing.T) {
_ = flag.Set("maxConcurrentInserts", "1")
_ = flag.Set("insert.maxQueueDuration", "10ms")
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
if err := writeconcurrencylimiter.IncConcurrency(); err != nil {
t.Fatalf("IncConcurrency setup failed: %v", err)
}
defer writeconcurrencylimiter.DecConcurrency()

r := httptest.NewRequest(http.MethodPost, otlpExportTracesPath, bytes.NewReader(newGRPCFrame([]byte{0x0a, 0x00})))
w := httptest.NewRecorder()
OTLPGRPCRequestHandler(w, r)

if got := w.Header().Get("grpc-status"); got != grpc.StatusCodeUnavailable {
t.Fatalf("grpc-status = %q, want %q", got, grpc.StatusCodeUnavailable)
}
})

t.Run("invalid protobuf returns INTERNAL", func(t *testing.T) {
r := httptest.NewRequest(http.MethodPost, otlpExportTracesPath, bytes.NewReader(newGRPCFrame([]byte{0xff, 0xff})))
w := httptest.NewRecorder()
OTLPGRPCRequestHandler(w, r)

if got := w.Header().Get("grpc-status"); got != grpc.StatusCodeInternal {
t.Fatalf("grpc-status = %q, want %q", got, grpc.StatusCodeInternal)
}
})
}
4 changes: 2 additions & 2 deletions app/vtinsert/opentelemetry/otlpgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func otlpExportTracesHandler(r *http.Request, w http.ResponseWriter) {

if err := insertutil.CanWriteData(); err != nil {
errorsGRPCTotal.Inc()
grpc.Errorf(w, r, grpc.StatusCodeInternal, err.Error())
grpc.Errorf(w, r, grpc.StatusCodeFromError(err), err.Error())
return
}

Expand Down Expand Up @@ -91,7 +91,7 @@ func otlpExportTracesHandler(r *http.Request, w http.ResponseWriter) {
})
if err != nil {
errorsGRPCTotal.Inc()
grpc.Errorf(w, r, grpc.StatusCodeInternal, fmt.Sprintf("cannot read OpenTelemetry protocol data: %s", err))
grpc.Errorf(w, r, grpc.StatusCodeFromError(err), fmt.Sprintf("cannot read OpenTelemetry protocol data: %s", err))
return
}

Expand Down
92 changes: 92 additions & 0 deletions app/vtinsert/opentelemetry/otlpgrpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package opentelemetry

import (
"bytes"
"encoding/binary"
"errors"
"flag"
"net/http"
"net/http/httptest"
"testing"

"github.com/VictoriaMetrics/VictoriaLogs/lib/logstorage"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/writeconcurrencylimiter"

"github.com/VictoriaMetrics/VictoriaTraces/app/vtinsert/insertutil"
"github.com/VictoriaMetrics/VictoriaTraces/lib/grpc"
)

type stubLogRowsStorage struct {
canWriteErr error
}

func (s *stubLogRowsStorage) MustAddRows(*logstorage.LogRows) {}

func (s *stubLogRowsStorage) CanWriteData() error {
return s.canWriteErr
}

func (s *stubLogRowsStorage) IsLocalStorage() bool {
return true
}

func newGRPCFrame(payload []byte) []byte {
frame := make([]byte, 5+len(payload))
binary.BigEndian.PutUint32(frame[1:5], uint32(len(payload)))
copy(frame[5:], payload)
return frame
}

func TestOTLPGRPCExportStatusCodes(t *testing.T) {
t.Run("read-only storage returns UNAVAILABLE", func(t *testing.T) {
insertutil.SetLogRowsStorage(&stubLogRowsStorage{
canWriteErr: &httpserver.ErrorWithStatusCode{
Err: errors.New("storage is in read-only mode"),
StatusCode: http.StatusTooManyRequests,
},
})
t.Cleanup(func() { insertutil.SetLogRowsStorage(nil) })

r := httptest.NewRequest(http.MethodPost, otlpExportTracesPath, bytes.NewReader(newGRPCFrame([]byte{0x0a, 0x00})))
w := httptest.NewRecorder()
OTLPGRPCRequestHandler(r, w)

if got := w.Header().Get("grpc-status"); got != grpc.StatusCodeUnavailable {
t.Fatalf("grpc-status = %q, want %q", got, grpc.StatusCodeUnavailable)
}
})

t.Run("busy write concurrency limit returns UNAVAILABLE", func(t *testing.T) {
insertutil.SetLogRowsStorage(&stubLogRowsStorage{})
t.Cleanup(func() { insertutil.SetLogRowsStorage(nil) })

_ = flag.Set("maxConcurrentInserts", "1")
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
_ = flag.Set("insert.maxQueueDuration", "10ms")
if err := writeconcurrencylimiter.IncConcurrency(); err != nil {
t.Fatalf("IncConcurrency setup failed: %v", err)
}
defer writeconcurrencylimiter.DecConcurrency()

r := httptest.NewRequest(http.MethodPost, otlpExportTracesPath, bytes.NewReader(newGRPCFrame([]byte{0x0a, 0x00})))
w := httptest.NewRecorder()
OTLPGRPCRequestHandler(r, w)

if got := w.Header().Get("grpc-status"); got != grpc.StatusCodeUnavailable {
t.Fatalf("grpc-status = %q, want %q", got, grpc.StatusCodeUnavailable)
}
})

t.Run("invalid protobuf returns INTERNAL", func(t *testing.T) {
insertutil.SetLogRowsStorage(&stubLogRowsStorage{})
t.Cleanup(func() { insertutil.SetLogRowsStorage(nil) })

r := httptest.NewRequest(http.MethodPost, otlpExportTracesPath, bytes.NewReader(newGRPCFrame([]byte{0xff, 0xff})))
w := httptest.NewRecorder()
OTLPGRPCRequestHandler(r, w)

if got := w.Header().Get("grpc-status"); got != grpc.StatusCodeInternal {
t.Fatalf("grpc-status = %q, want %q", got, grpc.StatusCodeInternal)
}
})
}
19 changes: 19 additions & 0 deletions lib/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package grpc

import (
"encoding/binary"
"errors"
"fmt"
"io"
"net/http"

"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/ioutil"

"github.com/VictoriaMetrics/VictoriaTraces/lib/http2server"
Expand Down Expand Up @@ -88,6 +90,23 @@ func CheckDataFrame(req []byte) error {
return nil
}

// StatusCodeFromError returns the gRPC status code corresponding to err.
//
// Transient overload and rate-limit errors (HTTP 429, 502, 503, 504) map to
// StatusCodeUnavailable so OTLP/gRPC clients retry them instead of dropping spans
// on StatusCodeInternal.
func StatusCodeFromError(err error) string {
var esc *httpserver.ErrorWithStatusCode
if errors.As(err, &esc) {
switch esc.StatusCode {
case http.StatusTooManyRequests, http.StatusServiceUnavailable,
http.StatusBadGateway, http.StatusGatewayTimeout:
return StatusCodeUnavailable
}
}
return StatusCodeInternal
}

// Errorf writes formatted error code and message in gRPC protocol over HTTP to w, and to logger.
func Errorf(w http.ResponseWriter, r *http.Request, grpcErrorCode, grpcErrorMessage string) {
http2server.LogHTTPError(r, grpcErrorMessage)
Expand Down
19 changes: 19 additions & 0 deletions lib/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package grpc
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
)

// TestGRPCErrorf verifies the gRPC headers in the error response.
Expand Down Expand Up @@ -91,3 +94,19 @@ func TestReadDataFrame_Failure(t *testing.T) {
f(append(newDataFrame("foo"), 'x'), 10, "invalid gRPC message length")
f([]byte{2, 0, 0, 0, 0}, 10, "invalid gRPC compressed flag 2")
}

func TestStatusCodeFromError(t *testing.T) {
f := func(err error, want string) {
t.Helper()
if got := StatusCodeFromError(err); got != want {
t.Fatalf("StatusCodeFromError(%v) = %q, want %q", err, got, want)
}
}

f(nil, StatusCodeInternal)
f(errors.New("plain error"), StatusCodeInternal)
f(&httpserver.ErrorWithStatusCode{Err: errors.New("bad"), StatusCode: http.StatusBadRequest}, StatusCodeInternal)
f(&httpserver.ErrorWithStatusCode{Err: errors.New("readonly"), StatusCode: http.StatusTooManyRequests}, StatusCodeUnavailable)
f(&httpserver.ErrorWithStatusCode{Err: errors.New("busy"), StatusCode: http.StatusServiceUnavailable}, StatusCodeUnavailable)
f(fmt.Errorf("wrapped: %w", &httpserver.ErrorWithStatusCode{Err: errors.New("busy"), StatusCode: http.StatusServiceUnavailable}), StatusCodeUnavailable)
}