Skip to content

Commit

Permalink
Fix #1 middleware eats req body issue (#2)
Browse files Browse the repository at this point in the history
* - Fix req body restoring
- Add unittests for calc the body size.

* - Update CHANGELOG.md
  • Loading branch information
Cyprinus12138 authored Apr 6, 2024
1 parent 9e59c1b commit d84d434
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ N/A
- Implement basic http server metrics.
- Add example of initialization of `MeterProvider` in the example.
- Add README.md.

## [v1.0.1] - 2024-04-06

### Fixed
- Fix the bug when middleware calculating the size of the request body. (#1)
3 changes: 2 additions & 1 deletion gintrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package otelgin // import "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"

import (
"bytes"
"fmt"
otelmetric "go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/noop"
Expand Down Expand Up @@ -192,7 +193,7 @@ func calcReqSize(c *gin.Context) int {
}

// Restore the request body for further processing
c.Request.Body = io.NopCloser(c.Request.Body)
c.Request.Body = io.NopCloser(bytes.NewReader(body))

// Calculate the size of headers
headerSize := 0
Expand Down
69 changes: 69 additions & 0 deletions gintrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
package otelgin

import (
"bytes"
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -95,3 +97,70 @@ func TestPropagationWithCustomPropagators(t *testing.T) {

router.ServeHTTP(w, r)
}

// TestCalcReqSize tests the calcReqSize function.
func TestCalcReqSize(t *testing.T) {
// Create a sample request with a body and headers
body := []byte("sample body")
req, err := http.NewRequest("POST", "/test", bytes.NewReader(body))
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer token")

// Create a Gin context with the request
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = req

// Call the function to calculate the request size
size := calcReqSize(c)

// Calculate the expected size (body + headers + extra bytes for header formatting)
expectedSize := len(body) + len("Content-Type") + len("application/json") + len("Authorization") + len("Bearer token") + 4 // 4 extra bytes for ": " and "\r\n"

// Check if the calculated size matches the expected size
if size != expectedSize {
t.Errorf("Expected request size %d, got %d", expectedSize, size)
}
}

// TestCalcReqSizeWithBodyRead tests the calcReqSize function and ensures the request body can still be read afterward.
func TestCalcReqSizeWithBodyRead(t *testing.T) {
// Create a sample request with a body and headers
body := []byte("sample body")
req, err := http.NewRequest("POST", "/test", bytes.NewReader(body))
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer token")

// Create a Gin context with the request
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = req

// Call the function to calculate the request size
size := calcReqSize(c)

// Calculate the expected size (body + headers + extra bytes for header formatting)
expectedSize := len(body) + len("Content-Type") + len("application/json") + len("Authorization") + len("Bearer token") + 4 // 4 extra bytes for ": " and "\r\n"

// Check if the calculated size matches the expected size
if size != expectedSize {
t.Errorf("Expected request size %d, got %d", expectedSize, size)
}

// Read the request body again
newBody, err := io.ReadAll(c.Request.Body)
if err != nil {
t.Fatalf("Failed to read request body: %v", err)
}

// Check if the body is unchanged
if !bytes.Equal(newBody, body) {
t.Errorf("Expected request body %q, got %q", body, newBody)
}
}

0 comments on commit d84d434

Please sign in to comment.