Skip to content

Commit

Permalink
Move nrawssdk/internal => internal/awssupport
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Nov 26, 2019
1 parent 44086c2 commit 11b016f
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ matrix:
- go: "1.12"
env: DIRS=v3/newrelic,v3/internal,v3/examples
- go: "1.13"
env: DIRS=v3/newrelic,v3/internal,v3/examples,v3/integrations/logcontext,v3/integrations/nrawssdk/internal
env: DIRS=v3/newrelic,v3/internal,v3/examples,v3/integrations/logcontext
- go: "1.13"
env: DIRS=v3/integrations/nrawssdk/v1
- go: "1.13"
Expand Down
12 changes: 6 additions & 6 deletions v3/integrations/nrawssdk/v1/nrawssdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ package nrawssdk

import (
"github.com/aws/aws-sdk-go/aws/request"
internal "github.com/newrelic/go-agent/v3/integrations/nrawssdk/internal"
agentinternal "github.com/newrelic/go-agent/v3/internal"
"github.com/newrelic/go-agent/v3/internal"
"github.com/newrelic/go-agent/v3/internal/awssupport"
)

func init() { agentinternal.TrackUsage("integration", "library", "aws-sdk-go") }
func init() { internal.TrackUsage("integration", "library", "aws-sdk-go") }

func startSegment(req *request.Request) {
input := internal.StartSegmentInputs{
input := awssupport.StartSegmentInputs{
HTTPRequest: req.HTTPRequest,
ServiceName: req.ClientInfo.ServiceName,
Operation: req.Operation.Name,
Region: req.ClientInfo.SigningRegion,
Params: req.Params,
}
req.HTTPRequest = internal.StartSegment(input)
req.HTTPRequest = awssupport.StartSegment(input)
}

func endSegment(req *request.Request) {
ctx := req.HTTPRequest.Context()
internal.EndSegment(ctx, req.HTTPResponse.Header)
awssupport.EndSegment(ctx, req.HTTPResponse.Header)
}

// InstrumentHandlers will add instrumentation to the given *request.Handlers.
Expand Down
48 changes: 48 additions & 0 deletions v3/integrations/nrawssdk/v1/nrawssdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import (
"errors"
"io/ioutil"
"net/http"
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/private/protocol/rest"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/newrelic/go-agent/v3/internal"
"github.com/newrelic/go-agent/v3/internal/awssupport"
"github.com/newrelic/go-agent/v3/internal/integrationsupport"
newrelic "github.com/newrelic/go-agent/v3/newrelic"
)
Expand Down Expand Up @@ -584,3 +587,48 @@ func TestNoRequestIDFound(t *testing.T) {
app.ExpectSpanEvents(t, []internal.WantEvent{
genericSpan, externalSpanNoRequestID})
}

func TestGetRequestID(t *testing.T) {
primary := "X-Amzn-Requestid"
secondary := "X-Amz-Request-Id"

testcases := []struct {
hdr http.Header
expected string
}{
{hdr: http.Header{
"hello": []string{"world"},
}, expected: ""},

{hdr: http.Header{
strings.ToUpper(primary): []string{"hello"},
}, expected: ""},

{hdr: http.Header{
primary: []string{"hello"},
}, expected: "hello"},

{hdr: http.Header{
secondary: []string{"hello"},
}, expected: "hello"},

{hdr: http.Header{
primary: []string{"hello"},
secondary: []string{"world"},
}, expected: "hello"},
}

// Make sure our assumptions still hold against aws-sdk-go
for _, test := range testcases {
req := &request.Request{
HTTPResponse: &http.Response{
Header: test.hdr,
},
}
rest.UnmarshalMeta(req)
if out := awssupport.GetRequestID(test.hdr); req.RequestID != out {
t.Error("requestId assumptions incorrect", out, req.RequestID,
test.hdr, test.expected)
}
}
}
12 changes: 6 additions & 6 deletions v3/integrations/nrawssdk/v2/nrawssdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ package nrawssdk

import (
"github.com/aws/aws-sdk-go-v2/aws"
internal "github.com/newrelic/go-agent/v3/integrations/nrawssdk/internal"
agentinternal "github.com/newrelic/go-agent/v3/internal"
"github.com/newrelic/go-agent/v3/internal"
"github.com/newrelic/go-agent/v3/internal/awssupport"
)

func init() { agentinternal.TrackUsage("integration", "library", "aws-sdk-go-v2") }
func init() { internal.TrackUsage("integration", "library", "aws-sdk-go-v2") }

func startSegment(req *aws.Request) {
input := internal.StartSegmentInputs{
input := awssupport.StartSegmentInputs{
HTTPRequest: req.HTTPRequest,
ServiceName: req.Metadata.ServiceName,
Operation: req.Operation.Name,
Region: req.Metadata.SigningRegion,
Params: req.Params,
}
req.HTTPRequest = internal.StartSegment(input)
req.HTTPRequest = awssupport.StartSegment(input)
}

func endSegment(req *aws.Request) {
ctx := req.HTTPRequest.Context()
internal.EndSegment(ctx, req.HTTPResponse.Header)
awssupport.EndSegment(ctx, req.HTTPResponse.Header)
}

// InstrumentHandlers will add instrumentation to the given *aws.Handlers.
Expand Down
49 changes: 49 additions & 0 deletions v3/integrations/nrawssdk/v2/nrawssdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"errors"
"io/ioutil"
"net/http"
"strings"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/endpoints"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/private/protocol/rest"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/newrelic/go-agent/v3/internal"
"github.com/newrelic/go-agent/v3/internal/awssupport"
"github.com/newrelic/go-agent/v3/internal/integrationsupport"
newrelic "github.com/newrelic/go-agent/v3/newrelic"
)
Expand Down Expand Up @@ -563,3 +566,49 @@ func TestNoRequestIDFound(t *testing.T) {
app.ExpectSpanEvents(t, []internal.WantEvent{
genericSpan, externalSpanNoRequestID})
}

func TestGetRequestID(t *testing.T) {
primary := "X-Amzn-Requestid"
secondary := "X-Amz-Request-Id"

testcases := []struct {
hdr http.Header
expected string
}{
{hdr: http.Header{
"hello": []string{"world"},
}, expected: ""},

{hdr: http.Header{
strings.ToUpper(primary): []string{"hello"},
}, expected: ""},

{hdr: http.Header{
primary: []string{"hello"},
}, expected: "hello"},

{hdr: http.Header{
secondary: []string{"hello"},
}, expected: "hello"},

{hdr: http.Header{
primary: []string{"hello"},
secondary: []string{"world"},
}, expected: "hello"},
}

// Make sure our assumptions still hold against aws-sdk-go-v2
for _, test := range testcases {
req := &aws.Request{
HTTPResponse: &http.Response{
Header: test.hdr,
},
Data: &lambda.InvokeOutput{},
}
rest.UnmarshalMeta(req)
if out := awssupport.GetRequestID(test.hdr); req.RequestID != out {
t.Error("requestId assumptions incorrect", out, req.RequestID,
test.hdr, test.expected)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package internal
// +build go1.8

package awssupport

import (
"context"
Expand Down Expand Up @@ -36,7 +38,8 @@ func getTableName(params interface{}) string {
return tableName
}

func getRequestID(hdr http.Header) string {
// GetRequestID looks for the AWS request ID header.
func GetRequestID(hdr http.Header) string {
id := hdr.Get("X-Amzn-Requestid")
if id == "" {
// Alternative version of request id in the header
Expand Down Expand Up @@ -90,7 +93,7 @@ func StartSegment(input StartSegmentInputs) *http.Request {
// EndSegment will end any segment found in the given context.
func EndSegment(ctx context.Context, hdr http.Header) {
if segment, ok := ctx.Value(segmentContextKey).(endable); ok {
if id := getRequestID(hdr); "" != id {
if id := GetRequestID(hdr); "" != id {
txn := newrelic.FromContext(ctx)
integrationsupport.AddAgentSpanAttribute(txn, newrelic.SpanAttributeAWSRequestID, id)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package internal
// +build go1.8

package awssupport

import (
"net/http"
"strings"
"testing"

requestv2 "github.com/aws/aws-sdk-go-v2/aws"
restv2 "github.com/aws/aws-sdk-go-v2/private/protocol/rest"
"github.com/aws/aws-sdk-go-v2/service/lambda"
requestv1 "github.com/aws/aws-sdk-go/aws/request"
restv1 "github.com/aws/aws-sdk-go/private/protocol/rest"
)

func TestGetTableName(t *testing.T) {
Expand Down Expand Up @@ -77,37 +73,8 @@ func TestGetRequestID(t *testing.T) {
}

for i, test := range testcases {
if out := getRequestID(test.hdr); test.expected != out {
if out := GetRequestID(test.hdr); test.expected != out {
t.Error(i, out, test.hdr, test.expected)
}
}

// Make sure our assumptions still hold against aws-sdk-go
for _, test := range testcases {
req := &requestv1.Request{
HTTPResponse: &http.Response{
Header: test.hdr,
},
}
restv1.UnmarshalMeta(req)
if out := getRequestID(test.hdr); req.RequestID != out {
t.Error("requestId assumptions incorrect", out, req.RequestID,
test.hdr, test.expected)
}
}

// Make sure our assumptions still hold against aws-sdk-go-v2
for _, test := range testcases {
req := &requestv2.Request{
HTTPResponse: &http.Response{
Header: test.hdr,
},
Data: &lambda.InvokeOutput{},
}
restv2.UnmarshalMeta(req)
if out := getRequestID(test.hdr); req.RequestID != out {
t.Error("requestId assumptions incorrect", out, req.RequestID,
test.hdr, test.expected)
}
}
}

0 comments on commit 11b016f

Please sign in to comment.