Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Return spec-compliant `TraceIdRatioBased` description. This is a breaking behavioral change, but it is necessary to
make the implementation [spec-compliant](https://opentelemetry.io/docs/specs/otel/trace/sdk/#traceidratiobased). (#8027)
- Fix a race condition in `go.opentelemetry.io/otel/sdk/metric` where the lastvalue aggregation could collect the value 0 even when no zero-value measurements were recorded. (#8056)
- Fix missing `request.GetBody` in `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp` to correctly handle HTTP2 GOAWAY frame. (#8096)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down
9 changes: 9 additions & 0 deletions exporters/otlp/otlplog/otlploghttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ func (c *httpClient) newRequest(ctx context.Context, body []byte) (request, erro
case NoCompression:
r.ContentLength = int64(len(body))
req.bodyReader = bodyReader(body)
req.GetBody = bodyReaderErr(body)
case GzipCompression:
// Ensure the content length is not used.
r.ContentLength = -1
Expand All @@ -283,6 +284,7 @@ func (c *httpClient) newRequest(ctx context.Context, body []byte) (request, erro
}

req.bodyReader = bodyReader(b.Bytes())
req.GetBody = bodyReaderErr(body)
}

return req, nil
Expand All @@ -295,6 +297,13 @@ func bodyReader(buf []byte) func() io.ReadCloser {
}
}

// bodyReaderErr returns a closure returning a new reader for buf.
func bodyReaderErr(buf []byte) func() (io.ReadCloser, error) {
return func() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(buf)), nil
}
}

// request wraps an http.Request with a resettable body reader.
type request struct {
*http.Request
Expand Down
53 changes: 53 additions & 0 deletions exporters/otlp/otlplog/otlploghttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"math/big"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync"
Expand Down Expand Up @@ -833,6 +834,58 @@ func TestConfig(t *testing.T) {
})
}

func TestGetBodyCalledOnRedirect(t *testing.T) {
// Test that req.GetBody is set correctly, allowing the HTTP transport
// to re-send the body on 307 redirects.
var mu sync.Mutex
var requestBodies [][]byte

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

mu.Lock()
requestBodies = append(requestBodies, body)
isFirstRequest := len(requestBodies) == 1
mu.Unlock()

if isFirstRequest {
w.Header().Set("Location", "/v1/logs/final")
w.WriteHeader(http.StatusTemporaryRedirect)
return
}

w.Header().Set("Content-Type", "application/x-protobuf")
w.WriteHeader(http.StatusOK)
})

server := httptest.NewServer(handler)
defer server.Close()

opts := []Option{WithEndpoint(server.Listener.Addr().String()), WithInsecure()}
cfg := newConfig(opts)
client, err := newHTTPClient(t.Context(), cfg)
require.NoError(t, err)

exporter, err := newExporter(client, cfg)
require.NoError(t, err)
ctx := t.Context()
defer func() { _ = exporter.Shutdown(ctx) }()

err = exporter.Export(ctx, make([]log.Record, 1))
require.NoError(t, err)

mu.Lock()
defer mu.Unlock()

require.Len(t, requestBodies, 2, "expected 2 requests (original + redirect)")
assert.NotEmpty(t, requestBodies[0], "original request body should not be empty")
assert.Equal(t, requestBodies[0], requestBodies[1], "redirect body should match original")
}

// SetExporterID sets the exporter ID counter to v and returns the previous
// value.
//
Expand Down
Loading