From b31c2d12199714f2c3945935e4c8a6e097922471 Mon Sep 17 00:00:00 2001 From: Ivan Punko Date: Tue, 29 Sep 2026 22:18:25 +0300 Subject: [PATCH 1/2] Fix: accept gzip Content-Encoding on the OTLP/HTTP receiver The OpenTelemetry Collector's otlphttp exporter gzips by default, and POST /v1/traces read the body as-is, so every export from a stock collector failed to decode and got a 400 -- which the exporter treats as permanent and drops. Undo Content-Encoding (gzip, identity) before the protobuf/JSON decode; answer 415 for an encoding it doesn't know and 400 for a body that isn't valid gzip. Co-Authored-By: Claude Opus 5.5 (1M context) --- internal/api/otlp_http_test.go | 117 +++++++++++++++++++++++++++++++++ internal/api/server.go | 33 +++++++++- 2 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 internal/api/otlp_http_test.go diff --git a/internal/api/otlp_http_test.go b/internal/api/otlp_http_test.go new file mode 100644 index 0000000..29f375b --- /dev/null +++ b/internal/api/otlp_http_test.go @@ -0,0 +1,117 @@ +package api + +import ( + "bytes" + "compress/gzip" + "encoding/hex" + "net/http" + "net/http/httptest" + "testing" + + coltracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1" + commonpb "go.opentelemetry.io/proto/otlp/common/v1" + resourcepb "go.opentelemetry.io/proto/otlp/resource/v1" + tracepb "go.opentelemetry.io/proto/otlp/trace/v1" + "google.golang.org/protobuf/proto" +) + +func gzipBytes(t *testing.T, b []byte) []byte { + t.Helper() + var buf bytes.Buffer + zw := gzip.NewWriter(&buf) + if _, err := zw.Write(b); err != nil { + t.Fatalf("gzip write: %v", err) + } + if err := zw.Close(); err != nil { + t.Fatalf("gzip close: %v", err) + } + return buf.Bytes() +} + +func otlpHTTPTestRequestProto(t *testing.T, session string) []byte { + t.Helper() + traceID, _ := hex.DecodeString("3e289017fe03ffd7c4145316d2eb3d0d") + spanID, _ := hex.DecodeString("e37fdd8f56146d31") + req := &coltracepb.ExportTraceServiceRequest{ + ResourceSpans: []*tracepb.ResourceSpans{{ + Resource: &resourcepb.Resource{Attributes: []*commonpb.KeyValue{ + {Key: agentevalsSessionName, Value: &commonpb.AnyValue{Value: &commonpb.AnyValue_StringValue{StringValue: session}}}, + }}, + ScopeSpans: []*tracepb.ScopeSpans{{ + Scope: &commonpb.InstrumentationScope{Name: "gcp.vertex.agent"}, + Spans: []*tracepb.Span{{ + TraceId: traceID, + SpanId: spanID, + Name: "invoke_agent test", + StartTimeUnixNano: 1_000_000_000, + EndTimeUnixNano: 1_002_000_000, + }}, + }}, + }}, + } + b, err := proto.Marshal(req) + if err != nil { + t.Fatalf("marshal: %v", err) + } + return b +} + +func otlpHTTPTestRequestJSON(session string) []byte { + return []byte(`{"resourceSpans":[{"resource":{"attributes":[{"key":"` + agentevalsSessionName + + `","value":{"stringValue":"` + session + `"}}]},"scopeSpans":[{"scope":{"name":"gcp.vertex.agent"},` + + `"spans":[{"traceId":"3e289017fe03ffd7c4145316d2eb3d0d","spanId":"e37fdd8f56146d31",` + + `"name":"invoke_agent test","startTimeUnixNano":"1000000000","endTimeUnixNano":"1002000000"}]}]}]}`) +} + +// TestOTLPTracesHandler_ContentEncoding covers the encodings a stock +// OpenTelemetry Collector sends: its otlphttp exporter gzips by default, so +// a receiver that only reads raw bodies rejects every export with a 400. +func TestOTLPTracesHandler_ContentEncoding(t *testing.T) { + cases := []struct { + name string + contentType string + encoding string + body func(t *testing.T) []byte + wantStatus int + wantSession bool + }{ + {"protobuf identity", "application/x-protobuf", "", + func(t *testing.T) []byte { return otlpHTTPTestRequestProto(t, "s") }, http.StatusOK, true}, + {"protobuf gzip", "application/x-protobuf", "gzip", + func(t *testing.T) []byte { return gzipBytes(t, otlpHTTPTestRequestProto(t, "s")) }, http.StatusOK, true}, + {"json gzip", "application/json", "gzip", + func(t *testing.T) []byte { return gzipBytes(t, otlpHTTPTestRequestJSON("s")) }, http.StatusOK, true}, + {"gzip header, uppercase", "application/x-protobuf", "GZIP", + func(t *testing.T) []byte { return gzipBytes(t, otlpHTTPTestRequestProto(t, "s")) }, http.StatusOK, true}, + {"unsupported encoding", "application/x-protobuf", "br", + func(t *testing.T) []byte { return otlpHTTPTestRequestProto(t, "s") }, http.StatusUnsupportedMediaType, false}, + {"gzip header, body not gzip", "application/x-protobuf", "gzip", + func(t *testing.T) []byte { return otlpHTTPTestRequestProto(t, "s") }, http.StatusBadRequest, false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + store := NewSessionStore(nil) + req := httptest.NewRequest(http.MethodPost, "/v1/traces", bytes.NewReader(tc.body(t))) + req.Header.Set("Content-Type", tc.contentType) + if tc.encoding != "" { + req.Header.Set("Content-Encoding", tc.encoding) + } + rec := httptest.NewRecorder() + otlpTracesHandler(store)(rec, req) + + if rec.Code != tc.wantStatus { + t.Fatalf("status = %d, want %d (body %q)", rec.Code, tc.wantStatus, rec.Body.String()) + } + sessions := store.List() + if !tc.wantSession { + if len(sessions) != 0 { + t.Fatalf("got %d sessions, want 0", len(sessions)) + } + return + } + if len(sessions) != 1 || sessions[0].SpanCount != 1 || sessions[0].SessionID != "s" { + t.Fatalf("sessions = %+v, want one session \"s\" with 1 span", sessions) + } + }) + } +} diff --git a/internal/api/server.go b/internal/api/server.go index ac37332..d731bdb 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -13,6 +13,7 @@ package api import ( + "compress/gzip" "encoding/json" "fmt" "io" @@ -225,11 +226,18 @@ func otlpTracesHandler(store *SessionStore) http.HandlerFunc { return } + reqBody, status, err := otlpRequestBody(r) + if err != nil { + http.Error(w, err.Error(), status) + return + } + defer reqBody.Close() + contentType := r.Header.Get("Content-Type") var body map[string]any if strings.Contains(contentType, "application/x-protobuf") { - raw, err := io.ReadAll(r.Body) + raw, err := io.ReadAll(reqBody) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return @@ -240,7 +248,7 @@ func otlpTracesHandler(store *SessionStore) http.HandlerFunc { return } } else { - if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + if err := json.NewDecoder(reqBody).Decode(&body); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -254,6 +262,27 @@ func otlpTracesHandler(store *SessionStore) http.HandlerFunc { } } +// otlpRequestBody returns r's body with its Content-Encoding undone. OTLP/HTTP +// receivers are expected to accept gzip, and gzip is the default of the +// OpenTelemetry Collector's otlphttp exporter: without this, every export +// from a stock collector failed to decode and got a 400, which the exporter +// treats as permanent and drops. The returned status is only meaningful +// when err is non-nil. +func otlpRequestBody(r *http.Request) (io.ReadCloser, int, error) { + switch enc := strings.ToLower(strings.TrimSpace(r.Header.Get("Content-Encoding"))); enc { + case "", "identity": + return r.Body, 0, nil + case "gzip": + zr, err := gzip.NewReader(r.Body) + if err != nil { + return nil, http.StatusBadRequest, fmt.Errorf("invalid gzip body: %w", err) + } + return zr, 0, nil + default: + return nil, http.StatusUnsupportedMediaType, fmt.Errorf("unsupported Content-Encoding %q", enc) + } +} + // sessionsHandler implements GET /api/streaming/sessions, returning the // {"data": [...], "error": null} envelope the UI's fetchExistingSessions // expects (LiveStreamingView.tsx reads response.data; "error" is FastAPI's From cf18ee8284460b8032e629b1a07b130408dea239 Mon Sep 17 00:00:00 2001 From: Ivan Punko Date: Tue, 29 Sep 2026 22:31:01 +0300 Subject: [PATCH 2/2] Docs: note gzip on the OTLP/HTTP receiver as additive over Python Co-Authored-By: Claude Opus 5.5 (1M context) --- docs/STATUS.md | 5 +++++ internal/api/server.go | 13 +++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/STATUS.md b/docs/STATUS.md index 4bbf047..99417d6 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -36,6 +36,11 @@ (`ws_server.py`'s `get_or_create_otlp_session`) are all ported. Read back via `GET /api/streaming/sessions` (the `{"data": [...]}` envelope + camelCase `SessionInfo` field shape the UI's `LiveStreamingView.tsx` actually expects, not a guessed shape) and `GET /api/streaming/get-trace?session_id=`. +- **`Content-Encoding: gzip` on the OTLP/HTTP receiver** (`internal/api/server.go`'s `otlpRequestBody`) - + additive over Python, whose `api/otlp_routes.py` `receive_traces` reads the raw body with no + decompression. gzip is the OpenTelemetry Collector `otlphttp` exporter's default, so without it every export + from a stock collector got a 400 (dropped by the exporter as a permanent error); an unknown encoding now + gets 415 and a body that isn't valid gzip 400. Covered by `internal/api/otlp_http_test.go`. - **OTLP/gRPC trace ingestion** (`internal/api/grpc.go`) - `:4317` by default, a `TraceServiceServer` implementation (`go.opentelemetry.io/proto/otlp/collector/trace/v1`) sharing the exact same `SessionStore` and conversion path (`otlp.ExportRequestToMap` → `ParseExportRequest`) as the HTTP receiver - grpc hands diff --git a/internal/api/server.go b/internal/api/server.go index d731bdb..214ff13 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -262,12 +262,13 @@ func otlpTracesHandler(store *SessionStore) http.HandlerFunc { } } -// otlpRequestBody returns r's body with its Content-Encoding undone. OTLP/HTTP -// receivers are expected to accept gzip, and gzip is the default of the -// OpenTelemetry Collector's otlphttp exporter: without this, every export -// from a stock collector failed to decode and got a 400, which the exporter -// treats as permanent and drops. The returned status is only meaningful -// when err is non-nil. +// otlpRequestBody returns r's body with its Content-Encoding undone. +// Additive over Python: api/otlp_routes.py's receive_traces reads the raw +// body (request.body()/request.json()) with no decompression, so both +// implementations rejected gzip. gzip is the default of the OpenTelemetry +// Collector's otlphttp exporter, so every export from a stock collector +// failed to decode and got a 400, which the exporter treats as permanent and +// drops. The returned status is only meaningful when err is non-nil. func otlpRequestBody(r *http.Request) (io.ReadCloser, int, error) { switch enc := strings.ToLower(strings.TrimSpace(r.Header.Get("Content-Encoding"))); enc { case "", "identity":