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 @@ -32,6 +32,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Fix bad log message when key-value pairs are dropped because of key duplication in `go.opentelemetry.io/otel/sdk/log`. (#7662)
- Fix `DroppedAttributes` on `Record` in `go.opentelemetry.io/otel/sdk/log` to not count the non-attribute key-value pairs dropped because of key duplication. (#7662)
- Fix `SetAttributes` on `Record` in `go.opentelemetry.io/otel/sdk/log` to not log that attributes are dropped when they are actually not dropped. (#7662)
- Fix missing `request.GetBody` in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp` to correctly handle HTTP2 GOAWAY frame. (#7794)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down
9 changes: 9 additions & 0 deletions exporters/otlp/otlptrace/otlptracehttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func (d *client) newRequest(body []byte) (request, error) {
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 @@ -290,6 +291,7 @@ func (d *client) newRequest(body []byte) (request, error) {
}

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

return req, nil
Expand All @@ -315,6 +317,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
56 changes: 56 additions & 0 deletions exporters/otlp/otlptrace/otlptracehttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ package otlptracehttp_test
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -582,6 +585,59 @@ func TestClientInstrumentation(t *testing.T) {
metricdatatest.AssertEqual(t, want, got.ScopeMetrics[0], opt...)
}

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/traces/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()

client := otlptracehttp.NewClient(
otlptracehttp.WithEndpoint(server.Listener.Addr().String()),
otlptracehttp.WithInsecure(),
)

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

err = exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan())
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")
}

func BenchmarkExporterExportSpans(b *testing.B) {
const n = 10

Expand Down