Skip to content

Commit

Permalink
Merge branch 'lytics-no-alloc-for-count'
Browse files Browse the repository at this point in the history
  • Loading branch information
asahasrabuddhe committed Jan 18, 2024
2 parents 5423f22 + 1861acc commit 82e1389
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module go.ajitem.com/zapdriver

go 1.21
go 1.17

require (
github.com/pkg/errors v0.9.1
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
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

0 comments on commit 82e1389

Please sign in to comment.