-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for instrumenting AWS SNS (#938)
- Loading branch information
1 parent
3c5fa4e
commit 36ee3f6
Showing
8 changed files
with
325 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License 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 apmawssdkgo // import "go.elastic.co/apm/module/apmawssdkgo" | ||
|
||
import ( | ||
"strings" | ||
|
||
"go.elastic.co/apm" | ||
"go.elastic.co/apm/module/apmhttp" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/request" | ||
"github.com/aws/aws-sdk-go/service/sns" | ||
) | ||
|
||
type apmSNS struct { | ||
name, opName, resourceName string | ||
} | ||
|
||
func newSNS(req *request.Request) (*apmSNS, error) { | ||
if req.Operation.Name != "Publish" { | ||
return nil, errMethodNotSupported | ||
} | ||
name := req.ClientInfo.ServiceID + " PUBLISH" | ||
resourceName := serviceSNS | ||
|
||
topicName := getTopicName(req) | ||
if topicName != "" { | ||
name += " " + topicName | ||
resourceName += "/" + topicName | ||
} | ||
|
||
s := &apmSNS{ | ||
name: name, | ||
opName: "publish", | ||
resourceName: resourceName, | ||
} | ||
|
||
return s, nil | ||
} | ||
|
||
func (s *apmSNS) spanName() string { return s.name } | ||
|
||
func (s *apmSNS) resource() string { return s.resourceName } | ||
|
||
func (s *apmSNS) setAdditional(span *apm.Span) { | ||
span.Action = s.opName | ||
} | ||
|
||
func getTopicName(req *request.Request) string { | ||
// TODO: PhoneNumber is the third possibility, but I'm guessing we | ||
// don't want to store that for customers? | ||
arn := req.HTTPRequest.FormValue("TopicArn") | ||
if arn == "" { | ||
arn = req.HTTPRequest.FormValue("TargetArn") | ||
} | ||
|
||
// SNS ARN can be in the following formats: | ||
// - arn:aws:sns:us-east-2:123456789012:MyTopic | ||
// - arn:aws:sns:us-east-2:123456789012/MyTopic | ||
parts := strings.Split(arn, "/") | ||
if len(parts) == 1 { | ||
parts = strings.Split(arn, ":") | ||
} | ||
return parts[len(parts)-1] | ||
} | ||
|
||
// addMessageAttributesSNS adds message attributes to `Publish` RPC calls. | ||
// Other SNS RPC calls are ignored. | ||
func addMessageAttributesSNS(req *request.Request, span *apm.Span, propagateLegacyHeader bool) { | ||
if req.Operation.Name != "Publish" { | ||
return | ||
} | ||
|
||
traceContext := span.TraceContext() | ||
msgAttr := &sns.MessageAttributeValue{ | ||
DataType: aws.String("String"), | ||
StringValue: aws.String(apmhttp.FormatTraceparentHeader(traceContext)), | ||
} | ||
tracestate := traceContext.State.String() | ||
|
||
input, ok := req.Params.(*sns.PublishInput) | ||
if !ok { | ||
return | ||
} | ||
|
||
if input.MessageAttributes == nil { | ||
input.MessageAttributes = make(map[string]*sns.MessageAttributeValue) | ||
} | ||
input.MessageAttributes[apmhttp.W3CTraceparentHeader] = msgAttr | ||
if propagateLegacyHeader { | ||
input.MessageAttributes[apmhttp.ElasticTraceparentHeader] = msgAttr | ||
} | ||
if tracestate != "" { | ||
input.MessageAttributes[apmhttp.TracestateHeader] = &sns.MessageAttributeValue{ | ||
DataType: aws.String("String"), | ||
StringValue: aws.String(tracestate), | ||
} | ||
} | ||
} | ||
|
||
func supportedSNSMethod(req *request.Request) bool { | ||
return req.Operation.Name == "Publish" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License 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. | ||
|
||
// +build go1.13 | ||
|
||
package apmawssdkgo // import "go.elastic.co/apm/module/apmawssdkgo" | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"strconv" | ||
"testing" | ||
|
||
"go.elastic.co/apm/apmtest" | ||
"go.elastic.co/apm/module/apmhttp" | ||
|
||
"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/service/sns" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSNS(t *testing.T) { | ||
for _, tc := range []struct { | ||
fn func(context.Context, *sns.SNS) | ||
name, action, resource string | ||
ignored, hasTraceContext, hasError bool | ||
}{ | ||
{ | ||
name: "SNS PUBLISH myTopic", | ||
action: "publish", | ||
resource: "sns/myTopic", | ||
hasError: true, | ||
hasTraceContext: true, | ||
fn: func(ctx context.Context, svc *sns.SNS) { | ||
svc.PublishWithContext(ctx, &sns.PublishInput{ | ||
Message: aws.String("my message"), | ||
TopicArn: aws.String("arn:aws:sns:us-east-2:123456789012/myTopic"), | ||
}) | ||
}, | ||
}, | ||
{ | ||
name: "SNS PUBLISH myTopic", | ||
action: "publish", | ||
resource: "sns/myTopic", | ||
hasTraceContext: true, | ||
fn: func(ctx context.Context, svc *sns.SNS) { | ||
svc.PublishWithContext(ctx, &sns.PublishInput{ | ||
Message: aws.String("my message"), | ||
TopicArn: aws.String("arn:aws:sns:us-east-2:123456789012:myTopic"), | ||
}) | ||
}, | ||
}, | ||
{ | ||
ignored: true, | ||
fn: func(ctx context.Context, svc *sns.SNS) { | ||
svc.SubscribeWithContext(ctx, &sns.SubscribeInput{ | ||
Endpoint: aws.String("endpoint"), | ||
Protocol: aws.String("email"), | ||
ReturnSubscriptionArn: aws.Bool(true), | ||
TopicArn: aws.String("arn:aws:sns:us-east-2:123456789012:myTopic"), | ||
}) | ||
}, | ||
}, | ||
} { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if tc.hasError { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
})) | ||
defer ts.Close() | ||
|
||
region := "us-west-2" | ||
cfg := aws.NewConfig(). | ||
WithEndpoint(ts.URL). | ||
WithRegion(region). | ||
WithDisableSSL(true). | ||
WithCredentials(credentials.AnonymousCredentials) | ||
|
||
session := session.Must(session.NewSession(cfg)) | ||
wrapped := WrapSession(session) | ||
if tc.hasTraceContext { | ||
wrapped.Handlers.Build.PushBackNamed(request.NamedHandler{ | ||
Name: "spy_message_attrs_added", | ||
Fn: testTracingAttributesSNS(t), | ||
}) | ||
} | ||
|
||
svc := sns.New(wrapped) | ||
|
||
tx, spans, errors := apmtest.WithTransaction(func(ctx context.Context) { | ||
tc.fn(ctx, svc) | ||
}) | ||
|
||
if tc.ignored { | ||
require.Len(t, spans, 0) | ||
require.Len(t, errors, 0) | ||
return | ||
} | ||
|
||
require.Len(t, spans, 1) | ||
span := spans[0] | ||
|
||
if tc.hasError { | ||
require.Len(t, errors, 1) | ||
err := errors[0] | ||
assert.Equal(t, tx.ID, err.TransactionID) | ||
assert.Equal(t, span.ID, err.ParentID) | ||
} | ||
|
||
assert.Equal(t, tc.name, span.Name) | ||
assert.Equal(t, "messaging", span.Type) | ||
assert.Equal(t, "sns", span.Subtype) | ||
assert.Equal(t, tc.action, span.Action) | ||
|
||
service := span.Context.Destination.Service | ||
assert.Equal(t, "sns", service.Name) | ||
assert.Equal(t, "messaging", service.Type) | ||
assert.Equal(t, tc.resource, service.Resource) | ||
|
||
host, port, err := net.SplitHostPort(ts.URL[7:]) | ||
require.NoError(t, err) | ||
assert.Equal(t, host, span.Context.Destination.Address) | ||
assert.Equal(t, port, strconv.Itoa(span.Context.Destination.Port)) | ||
|
||
assert.Equal(t, region, span.Context.Destination.Cloud.Region) | ||
|
||
assert.Equal(t, tx.ID, span.ParentID) | ||
} | ||
} | ||
|
||
func testTracingAttributesSNS(t *testing.T) func(*request.Request) { | ||
return func(req *request.Request) { | ||
if req.Operation.Name != "Publish" { | ||
t.Fail() | ||
} | ||
|
||
input, ok := req.Params.(*sns.PublishInput) | ||
require.True(t, ok) | ||
attrs := input.MessageAttributes | ||
assert.Contains(t, attrs, apmhttp.W3CTraceparentHeader) | ||
assert.Contains(t, attrs, apmhttp.ElasticTraceparentHeader) | ||
assert.Contains(t, attrs, apmhttp.TracestateHeader) | ||
} | ||
} |
Oops, something went wrong.