Skip to content

Commit

Permalink
Adding SQSMessageHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
atshaw43 committed Oct 13, 2022
1 parent 34628cb commit 110eed9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 19 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ go 1.18

require (
github.com/DATA-DOG/go-sqlmock v1.4.1
github.com/aws/aws-lambda-go v1.34.1
github.com/aws/aws-sdk-go v1.17.12
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/smithy-go v1.4.0
github.com/golang/protobuf v1.4.3
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.7.2
github.com/valyala/fasthttp v1.34.0
golang.org/x/net v0.0.0-20220225172249-27dd8689420f
google.golang.org/grpc v1.35.0
Expand All @@ -28,5 +29,5 @@ require (
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20210114201628-6edceaf6022f // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/DATA-DOG/go-sqlmock v1.4.1 h1:ThlnYciV1iM/V0OSF/dtkqWb6xo5qITT1TJBG1M
github.com/DATA-DOG/go-sqlmock v1.4.1/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/aws/aws-lambda-go v1.34.1 h1:M3a/uFYBjii+tDcOJ0wL/WyFi2550FHoECdPf27zvOs=
github.com/aws/aws-lambda-go v1.34.1/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM=
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=
Expand Down Expand Up @@ -76,8 +78,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.34.0 h1:d3AAQJ2DRcxJYHm7OXNXtXt2as1vMDfxeIcFvhmGGm4=
Expand Down Expand Up @@ -177,8 +179,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
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=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
16 changes: 16 additions & 0 deletions lambda/sqs_message_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package lambda

import (
"github.com/aws/aws-lambda-go/events"
"strings"
)

func IsSampled(sqsMessge *events.SQSMessage) bool {
value, ok := sqsMessge.Attributes["AWSTraceHeader"]

if !ok {
return false
} else {
return strings.Contains(value, "Sampled=1")
}
}
32 changes: 32 additions & 0 deletions lambda/sqs_message_helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package lambda

import (
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/assert"
"testing"
)

func TestSQSMessageHelper(t *testing.T) {
testTrue(t, "Root=1-632BB806-bd862e3fe1be46a994272793;Sampled=1")
testTrue(t, "Root=1-5759e988-bd862e3fe1be46a994272793;Sampled=1")
testTrue(t, "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1")

testFalse(t, "Root=1-632BB806-bd862e3fe1be46a994272793")
testFalse(t, "Root=1-632BB806-bd862e3fe1be46a994272793;Sampled=0")
testFalse(t, "Root=1-5759e988-bd862e3fe1be46a994272793;Sampled=0")
testFalse(t, "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=0")
}

func testTrue(t *testing.T, header string) {
var sqsMessage events.SQSMessage
sqsMessage.Attributes = make(map[string]string)
sqsMessage.Attributes["AWSTraceHeader"] = header
assert.True(t, IsSampled(&sqsMessage))
}

func testFalse(t *testing.T, header string) {
var sqsMessage events.SQSMessage
sqsMessage.Attributes = make(map[string]string)
sqsMessage.Attributes["AWSTraceHeader"] = header
assert.False(t, IsSampled(&sqsMessage))
}
28 changes: 16 additions & 12 deletions xray/lambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,7 @@ func testHelper(t *testing.T, ctx context.Context) *http.Response {

req, _ := http.NewRequestWithContext(ctx, http.MethodPost, ts.URL, strings.NewReader(""))

var samplingDecision = header.Sampled
if !subseg.Sampled {
samplingDecision = header.NotSampled
}

req.Header.Add(TraceIDHeaderKey, header.Header{
TraceID: subseg.TraceID,
ParentID: subseg.ID,
SamplingDecision: samplingDecision,

AdditionalData: make(map[string]string),
}.String())
req.Header.Add(TraceIDHeaderKey, generateHeader(subseg).String())

resp, _ := http.DefaultClient.Do(req)
assert.Equal(t, http.StatusOK, resp.StatusCode)
Expand All @@ -129,3 +118,18 @@ func testHelper(t *testing.T, ctx context.Context) *http.Response {

return resp
}

func generateHeader(seg *Segment) header.Header {
var samplingDecision = header.Sampled
if !seg.Sampled {
samplingDecision = header.NotSampled
}

return header.Header{
TraceID: seg.TraceID,
ParentID: seg.ID,
SamplingDecision: samplingDecision,

AdditionalData: make(map[string]string),
}
}

0 comments on commit 110eed9

Please sign in to comment.