Skip to content

Commit

Permalink
Merge pull request #309 from aws/aws_sdk_v2
Browse files Browse the repository at this point in the history
aws sdk v2 instrumentation support
  • Loading branch information
wangzlei authored Jul 1, 2021
2 parents e97bba3 + 4cda8c1 commit ec99b91
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 12 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ jobs:
- windows-latest
- macos-latest
go:
- '1.10'
- '1.11'
- '1.12'
- '1.13'
- '1.14'
- '1.15'
- '1.16'
- '1'

steps:
Expand Down
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func getExample(ctx context.Context) ([]byte, error) {
}
```

**AWS**
**AWS SDK Instrumentation**

```go
sess := session.Must(session.NewSession())
Expand All @@ -216,6 +216,43 @@ xray.AWS(dynamo.Client)
dynamo.ListTablesWithContext(ctx, &dynamodb.ListTablesInput{})
```

**AWS SDK V2 Instrumentation**

```go
package main

import (
"context"
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-xray-sdk-go/instrumentation/awsv2"
"github.com/aws/aws-xray-sdk-go/xray"
)

func main() {
ctx, root := xray.BeginSegment(context.TODO(), "AWSSDKV2_Dynamodb")
defer root.Close(nil)
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion("us-west-2"))
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
// Instrumenting AWS SDK v2
awsv2.AWSV2Instrumentor(&cfg.APIOptions)
// Using the Config value, create the DynamoDB client
svc := dynamodb.NewFromConfig(cfg)
// Build the request with its input parameters
_, err = svc.ListTables(ctx, &dynamodb.ListTablesInput{
Limit: aws.Int32(5),
})
if err != nil {
log.Fatalf("failed to list tables, %v", err)
}
}
```

**S3**

`aws-xray-sdk-go` does not currently support [`*Request.Presign()`](https://docs.aws.amazon.com/sdk-for-go/api/aws/request/#Request.Presign) operations and will panic if one is encountered. This results in an error similar to:
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ go 1.15

require (
github.com/DATA-DOG/go-sqlmock v1.4.1
github.com/aws/aws-sdk-go-v2 v1.6.0
github.com/aws/aws-sdk-go-v2/service/route53 v1.6.2
github.com/aws/aws-sdk-go v1.17.12
github.com/aws/smithy-go v1.4.0
github.com/golang/protobuf v1.4.3
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2
github.com/pkg/errors v0.9.1
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSUL
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/aws/aws-sdk-go v1.17.12 h1:jMFwRUaM0LcfdenfvbDLePNoWSoCdOHqF4RCvSB4xNQ=
github.com/aws/aws-sdk-go v1.17.12/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go-v2 v1.6.0 h1:r20hdhm8wZmKkClREfacXrKfX0Y7/s0aOoeraFbf/sY=
github.com/aws/aws-sdk-go-v2 v1.6.0/go.mod h1:tI4KhsR5VkzlUa2DZAdwx7wCAYGwkZZ1H31PYrBFx1w=
github.com/aws/aws-sdk-go-v2/service/route53 v1.6.2 h1:OsggywXCk9iFKdu2Aopg3e1oJITIuyW36hA/B0rqupE=
github.com/aws/aws-sdk-go-v2/service/route53 v1.6.2/go.mod h1:ZnAMilx42P7DgIrdjlWCkNIGSBLzeyk6T31uB8oGTwY=
github.com/aws/smithy-go v1.4.0 h1:3rsQpgRe+OoQgJhEwGNpIkosl0fJLdmQqF4gSFRjg+4=
github.com/aws/smithy-go v1.4.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
Expand Down Expand Up @@ -45,11 +51,15 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2 h1:FlFbCRLd5Jr4iYXZufAvgWN6Ao0JrI5chLINnUXDDr0=
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2/go.mod h1:EaizFBKfUKtMIF5iaDEhniwNedqGo9FuLFzppDr3uwI=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.11.8 h1:difgzQsp5mdAz9v8lm3P/I+EpDKMU/6uTMw1y1FObuo=
Expand Down Expand Up @@ -164,6 +174,7 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
84 changes: 84 additions & 0 deletions instrumentation/awsv2/awsv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

package awsv2

import (
"context"

v2Middleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
"github.com/aws/aws-xray-sdk-go/xray"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
)

type awsV2SubsegmentKey struct{}

func initializeMiddlewareAfter(stack *middleware.Stack) error {
return stack.Initialize.Add(middleware.InitializeMiddlewareFunc("XRayInitializeMiddlewareAfter", func(
ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler) (
out middleware.InitializeOutput, metadata middleware.Metadata, err error) {

serviceName := v2Middleware.GetServiceID(ctx)
// Start the subsegment
ctx, subseg := xray.BeginSubsegment(ctx, serviceName)
if subseg == nil {
return
}
subseg.Namespace = "aws"
subseg.GetAWS()["region"] = v2Middleware.GetRegion(ctx)
subseg.GetAWS()["operation"] = v2Middleware.GetOperationName(ctx)

// set the subsegment in the context
ctx = context.WithValue(ctx, awsV2SubsegmentKey{}, subseg)

out, metadata, err = next.HandleInitialize(ctx, in)

// End the subsegment when the response returns from this middleware
defer subseg.Close(err)

return out, metadata, err
}),
middleware.After)
}

func deserializeMiddleware(stack *middleware.Stack) error {
return stack.Deserialize.Add(middleware.DeserializeMiddlewareFunc("XRayDeserializeMiddleware", func(
ctx context.Context, in middleware.DeserializeInput, next middleware.DeserializeHandler) (
out middleware.DeserializeOutput, metadata middleware.Metadata, err error) {

subseg := ctx.Value(awsV2SubsegmentKey{}).(*xray.Segment)
in.Request.(*smithyhttp.Request).Header.Set(xray.TraceIDHeaderKey, subseg.DownstreamHeader().String())

out, metadata, err = next.HandleDeserialize(ctx, in)

resp, ok := out.RawResponse.(*smithyhttp.Response)
if !ok {
// No raw response to wrap with.
return out, metadata, err
}

subseg.GetHTTP().GetResponse().ContentLength = int(resp.ContentLength)
requestID, ok := v2Middleware.GetRequestIDMetadata(metadata)

if ok {
subseg.GetAWS()[xray.RequestIDKey] = requestID
}
if extendedRequestID := resp.Header.Get(xray.S3ExtendedRequestIDHeaderKey); extendedRequestID != "" {
subseg.GetAWS()[xray.ExtendedRequestIDKey] = extendedRequestID
}

xray.HttpCaptureResponse(subseg, resp.StatusCode)
return out, metadata, err
}),
middleware.Before)
}

func AWSV2Instrumentor(apiOptions *[]func(*middleware.Stack) error) {
*apiOptions = append(*apiOptions, initializeMiddlewareAfter, deserializeMiddleware)
}
153 changes: 153 additions & 0 deletions instrumentation/awsv2/awsv2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

package awsv2

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53"
"github.com/aws/aws-sdk-go-v2/service/route53/types"
"github.com/aws/aws-xray-sdk-go/xray"
)

func TestAWSV2(t *testing.T) {
cases := map[string]struct {
responseStatus int
responseBody []byte
expectedRegion string
expectedError string
expectedRequestID string
expectedStatusCode int
}{
"fault response": {
responseStatus: 500,
responseBody: []byte(`<?xml version="1.0" encoding="UTF-8"?>
<InvalidChangeBatch xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
<Messages>
<Message>Tried to create resource record set duplicate.example.com. type A, but it already exists</Message>
</Messages>
<RequestId>b25f48e8-84fd-11e6-80d9-574e0c4664cb</RequestId>
</InvalidChangeBatch>`),
expectedRegion: "us-east-1",
expectedError: "Error",
expectedRequestID: "b25f48e8-84fd-11e6-80d9-574e0c4664cb",
expectedStatusCode: 500,
},

"error response": {
responseStatus: 404,
responseBody: []byte(`<?xml version="1.0"?>
<ErrorResponse xmlns="http://route53.amazonaws.com/doc/2016-09-07/">
<Error>
<Type>Sender</Type>
<Code>MalformedXML</Code>
<Message>1 validation error detected: Value null at 'route53#ChangeSet' failed to satisfy constraint: Member must not be null</Message>
</Error>
<RequestId>1234567890A</RequestId>
</ErrorResponse>
`),
expectedRegion: "us-west-1",
expectedError: "Error",
expectedRequestID: "1234567890A",
expectedStatusCode: 404,
},

"success response": {
responseStatus: 200,
responseBody: []byte(`<?xml version="1.0" encoding="UTF-8"?>
<ChangeResourceRecordSetsResponse>
<ChangeInfo>
<Comment>mockComment</Comment>
<Id>mockID</Id>
</ChangeInfo>
</ChangeResourceRecordSetsResponse>`),
expectedRegion: "us-west-2",
expectedStatusCode: 200,
},
}

for name, c := range cases {
server := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(c.responseStatus)
_, err := w.Write(c.responseBody)
if err != nil {
t.Fatal(err)
}
}))
defer server.Close()

t.Run(name, func(t *testing.T) {
ctx, root := xray.BeginSegment(context.Background(), "AWSSDKV2_Route53")

svc := route53.NewFromConfig(aws.Config{
Region: c.expectedRegion,
EndpointResolver: aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
return aws.Endpoint{
URL: server.URL,
SigningName: "route53",
}, nil
}),
Retryer: func() aws.Retryer {
return aws.NopRetryer{}
},
})

_, _ = svc.ChangeResourceRecordSets(ctx, &route53.ChangeResourceRecordSetsInput{
ChangeBatch: &types.ChangeBatch{
Changes: []types.Change{},
Comment: aws.String("mock"),
},
HostedZoneId: aws.String("zone"),
}, func(options *route53.Options) {
AWSV2Instrumentor(&options.APIOptions)
})

root.Close(nil)
seg := xray.GetSegment(ctx)
var subseg *xray.Segment
_ = json.Unmarshal(seg.Subsegments[0], &subseg)

if e, a := "Route 53", subseg.Name; !strings.EqualFold(e, a) {
t.Errorf("expected segment name to be %s, got %s", e, a)
}

if e, a := c.expectedRegion, fmt.Sprintf("%v", subseg.GetAWS()["region"]); !strings.EqualFold(e, a) {
t.Errorf("expected subsegment name to be %s, got %s", e, a)
}

if e, a := "ChangeResourceRecordSets", fmt.Sprintf("%v", subseg.GetAWS()["operation"]); !strings.EqualFold(e, a) {
t.Errorf("expected operation to be %s, got %s", e, a)
}

if e, a := fmt.Sprint(c.expectedStatusCode), fmt.Sprintf("%v", subseg.GetHTTP().GetResponse().Status); !strings.EqualFold(e, a) {
t.Errorf("expected status code to be %s, got %s", e, a)
}

if e, a := "aws", subseg.Namespace; !strings.EqualFold(e, a) {
t.Errorf("expected namespace to be %s, got %s", e, a)
}

if subseg.GetAWS()[xray.RequestIDKey] != nil {
if e, a := c.expectedRequestID, fmt.Sprintf("%v", subseg.GetAWS()[xray.RequestIDKey]); !strings.EqualFold(e, a) {
t.Errorf("expected request id to be %s, got %s", e, a)
}
}
})
time.Sleep(1 * time.Second)
}
}
2 changes: 1 addition & 1 deletion xray/fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ func fasthttpTrace(seg *Segment, h fasthttp.RequestHandler, ctx *fasthttp.Reques
seg.Lock()
seg.GetHTTP().GetResponse().ContentLength = ctx.Response.Header.ContentLength()
seg.Unlock()
httpCaptureResponse(seg, ctx.Response.StatusCode())
HttpCaptureResponse(seg, ctx.Response.StatusCode())
}
6 changes: 3 additions & 3 deletions xray/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func httpTrace(seg *Segment, h http.Handler, w http.ResponseWriter, r *http.Requ
seg.Lock()
seg.GetHTTP().GetResponse().ContentLength, _ = strconv.Atoi(capturer.Header().Get("Content-Length"))
seg.Unlock()
httpCaptureResponse(seg, capturer.status)
HttpCaptureResponse(seg, capturer.status)
}

func clientIP(r *http.Request) (string, bool) {
Expand Down Expand Up @@ -160,8 +160,8 @@ func generateTraceIDHeaderValue(seg *Segment, traceHeader *header.Header) string
return respHeader.String()
}

// httpCaptureResponse fill response by http status code
func httpCaptureResponse(seg *Segment, statusCode int) {
// HttpCaptureResponse fill response by http status code
func HttpCaptureResponse(seg *Segment, statusCode int) {
seg.Lock()
defer seg.Unlock()

Expand Down
2 changes: 1 addition & 1 deletion xray/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func TestHTTPCaptureResponse(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tArgs := tt.args(t)

httpCaptureResponse(tArgs.seg, tArgs.statusCode)
HttpCaptureResponse(tArgs.seg, tArgs.statusCode)

if tt.inspect != nil {
tt.inspect(tArgs.seg, t)
Expand Down
2 changes: 1 addition & 1 deletion xray/segment_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type ResponseData struct {

// ServiceData provides the shape for unmarshalling service version.
type ServiceData struct {
Version string `json:"version,omitempty"`
Version string `json:"version,omitempty"`
RuntimeVersion string `json:"runtime_version,omitempty"`
Runtime string `json:"runtime,omitempty"`
}
Expand Down

0 comments on commit ec99b91

Please sign in to comment.