Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Require a request context in the request factory.
Browse files Browse the repository at this point in the history
  • Loading branch information
a-feld committed Jul 1, 2021
1 parent fdddaef commit 870b8c4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
4 changes: 3 additions & 1 deletion telemetry/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package telemetry

import (
"context"
"fmt"
"net/http"
)
Expand Down Expand Up @@ -31,7 +32,8 @@ func BuildSplitRequests(batches []Batch, factory RequestFactory) ([]*http.Reques
}

func newRequestsInternal(batches []Batch, factory RequestFactory, needsSplit func(*http.Request) bool) ([]*http.Request, error) {
r, err := factory.BuildRequest(batches)
// Context will be defined in the harvester when the request is actually submitted to the client
r, err := factory.BuildRequest(context.TODO(), batches)
if nil != err {
return nil, err
}
Expand Down
18 changes: 10 additions & 8 deletions telemetry/request_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package telemetry
import (
"bytes"
"compress/gzip"
"context"
"errors"
"io"
"io/ioutil"
Expand Down Expand Up @@ -42,7 +43,7 @@ type RequestFactory interface {
// BuildRequest converts the telemetry payload slice into an http.Request.
// Do not mix telemetry data types in a single call to build request. Each
// telemetry data type has its own RequestFactory.
BuildRequest([]Batch, ...ClientOption) (*http.Request, error)
BuildRequest(context.Context, []Batch, ...ClientOption) (*http.Request, error)
}

type requestFactory struct {
Expand Down Expand Up @@ -82,17 +83,17 @@ func configure(f *requestFactory, options []ClientOption) error {

}

func (f *hashRequestFactory) BuildRequest(batches []Batch, options ...ClientOption) (*http.Request, error) {
return f.buildRequest(batches, bufferRequestBytes, options)
func (f *hashRequestFactory) BuildRequest(ctx context.Context, batches []Batch, options ...ClientOption) (*http.Request, error) {
return f.buildRequest(ctx, batches, bufferRequestBytes, options)
}

func (f *eventRequestFactory) BuildRequest(batches []Batch, options ...ClientOption) (*http.Request, error) {
return f.buildRequest(batches, bufferEventRequestBytes, options)
func (f *eventRequestFactory) BuildRequest(ctx context.Context, batches []Batch, options ...ClientOption) (*http.Request, error) {
return f.buildRequest(ctx, batches, bufferEventRequestBytes, options)
}

type writer func(buf *bytes.Buffer, batches []Batch)

func (f *requestFactory) buildRequest(batches []Batch, bufferRequestBytes writer, options []ClientOption) (*http.Request, error) {
func (f *requestFactory) buildRequest(ctx context.Context, batches []Batch, bufferRequestBytes writer, options []ClientOption) (*http.Request, error) {
configuredFactory := f
if len(options) > 0 {
configuredFactory = &requestFactory{
Expand Down Expand Up @@ -149,7 +150,7 @@ func (f *requestFactory) buildRequest(batches []Batch, bufferRequestBytes writer
endpoint := configuredFactory.endpoint
headers := configuredFactory.getHeaders()

return &http.Request{
request := &http.Request{
Method: "POST",
URL: &url.URL{
Scheme: configuredFactory.scheme,
Expand All @@ -162,7 +163,8 @@ func (f *requestFactory) buildRequest(batches []Batch, bufferRequestBytes writer
ContentLength: contentLength,
Close: false,
Host: endpoint,
}, nil
}
return request.WithContext(ctx), nil
}

func (f *requestFactory) getHeaders() http.Header {
Expand Down
6 changes: 4 additions & 2 deletions telemetry/request_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package telemetry
import (
"bytes"
"compress/gzip"
"github.com/newrelic/newrelic-telemetry-sdk-go/internal"
"context"
"io/ioutil"
"testing"

"github.com/newrelic/newrelic-telemetry-sdk-go/internal"
)

func TestNewRequestFactoryNoInsertKeyConfigSuccess(t *testing.T) {
Expand Down Expand Up @@ -74,7 +76,7 @@ func (m *MockPayloadEntry) WriteDataEntry(buf *bytes.Buffer) *bytes.Buffer {

func TestSpanFactoryRequest(t *testing.T) {
f, _ := NewSpanRequestFactory(WithInsertKey("key!"))
request, _ := f.BuildRequest([]Batch{{&MockPayloadEntry{}}})
request, _ := f.BuildRequest(context.Background(), []Batch{{&MockPayloadEntry{}}})
if request.Method != "POST" {
t.Error("Method was not POST")
}
Expand Down

0 comments on commit 870b8c4

Please sign in to comment.