Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NewHTTP: No allocations #12

Merged
merged 2 commits into from
Jan 18, 2024
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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module go.ajitem.com/zapdriver
go 1.17

require (
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.19.1
)
Expand Down
13 changes: 5 additions & 8 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ package zapdriver
// pipe-1&dateRangeUnbound=backwardInTime

import (
"bytes"
"io"
"net/http"
"strconv"
Expand All @@ -23,7 +22,7 @@ import (
// HTTP adds the correct Stackdriver "HTTP" field.
//
// see: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#HttpRequest
func HTTP(req *HTTPPayload) zap.Field {
func HTTP(req HTTPPayload) zap.Field {
return zap.Object("httpRequest", req)
}

Expand Down Expand Up @@ -101,7 +100,7 @@ type HTTPPayload struct {

// NewHTTP returns a new HTTPPayload struct, based on the passed
// in http.Request and http.Response objects.
func NewHTTP(req *http.Request, res *http.Response) *HTTPPayload {
func NewHTTP(req *http.Request, res *http.Response) HTTPPayload {
if req == nil {
req = &http.Request{}
}
Expand All @@ -110,7 +109,7 @@ func NewHTTP(req *http.Request, res *http.Response) *HTTPPayload {
res = &http.Response{}
}

sdreq := &HTTPPayload{
sdreq := HTTPPayload{
RequestMethod: req.Method,
Status: res.StatusCode,
UserAgent: req.UserAgent(),
Expand All @@ -134,9 +133,8 @@ func NewHTTP(req *http.Request, res *http.Response) *HTTPPayload {
}
}

buf := &bytes.Buffer{}
if req.Body != nil {
n, _ := io.Copy(buf, req.Body) // nolint: gas
n, _ := io.Copy(io.Discard, req.Body) // nolint: gas
requestSize += n
}

Expand All @@ -153,8 +151,7 @@ func NewHTTP(req *http.Request, res *http.Response) *HTTPPayload {
}

if res.Body != nil {
buf.Reset()
n, _ := io.Copy(buf, res.Body) // nolint: gas
n, _ := io.Copy(io.Discard, res.Body) // nolint: gas
responseSize += n
}

Expand Down
21 changes: 15 additions & 6 deletions http_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package zapdriver_test

import (
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"go.uber.org/zap"

"go.ajitem.com/zapdriver"
)

func BenchmarkHTTP(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(b *testing.PB) {
for b.Next() {
p := zapdriver.NewHTTP(nil, nil)
_ = zapdriver.HTTP(p)
}
})
}

func TestHTTP(t *testing.T) {
t.Parallel()

req := &zapdriver.HTTPPayload{}
var req zapdriver.HTTPPayload
field := zapdriver.HTTP(req)

assert.Equal(t, zap.Object("httpRequest", req), field)
Expand Down Expand Up @@ -81,14 +90,14 @@ func TestNewHTTP(t *testing.T) {
},

"RequestSize": {
&http.Request{Body: ioutil.NopCloser(strings.NewReader("12345"))},
&http.Request{Body: io.NopCloser(strings.NewReader("12345"))},
nil,
&zapdriver.HTTPPayload{RequestSize: "5", ResponseSize: "0"},
},

"ResponseSize": {
nil,
&http.Response{Body: ioutil.NopCloser(strings.NewReader("12345"))},
&http.Response{Body: io.NopCloser(strings.NewReader("12345"))},
&zapdriver.HTTPPayload{ResponseSize: "5", RequestSize: "0"},
},

Expand All @@ -108,7 +117,7 @@ func TestNewHTTP(t *testing.T) {

"simple response": {
nil,
&http.Response{Body: ioutil.NopCloser(strings.NewReader("12345")), StatusCode: 404},
&http.Response{Body: io.NopCloser(strings.NewReader("12345")), StatusCode: 404},
&zapdriver.HTTPPayload{RequestSize: "0", ResponseSize: "5", Status: 404},
},

Expand Down
2 changes: 1 addition & 1 deletion report.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type reportContext struct {

// MarshalLogObject implements zapcore.ObjectMarshaller interface.
func (context reportContext) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddObject("reportLocation", context.ReportLocation)
_ = enc.AddObject("reportLocation", context.ReportLocation)

return nil
}
Expand Down