Skip to content

Commit

Permalink
Add support for AWS Lambda entry spans triggered by SQS events
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Slotin committed Nov 13, 2020
1 parent b41749e commit 9de1ee0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
41 changes: 41 additions & 0 deletions json_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,41 @@ func (tags AWSLambdaS3SpanTags) IsZero() bool {
return len(tags.Events) == 0
}

// AWSSQSMessageTags represents span tags for an SQS message delivery
type AWSSQSMessageTags struct {
Queue string `json:"queue"`
}

// AWSLambdaSQSSpanTags contains fields within the `data.lambda.sqs` section of an OT span document
type AWSLambdaSQSSpanTags struct {
// Messages are message tags for an SQS event
Messages []AWSSQSMessageTags `json:"messages"`
}

// NewAWSLambdaSQSSpanTags extracts SQS event tags for an AWS Lambda entry span. It truncates
// the events list to the first 3 items to reduce the payload.
func NewAWSLambdaSQSSpanTags(span *spanS) AWSLambdaSQSSpanTags {
var tags AWSLambdaSQSSpanTags

if msgs, ok := span.Tags["sqs.messages"]; ok {
msgs, ok := msgs.([]AWSSQSMessageTags)
if ok {
tags.Messages = msgs
}
}

if len(tags.Messages) > 3 {
tags.Messages = tags.Messages[:3]
}

return tags
}

// IsZero returns true if an AWSLambdaSQSSpanTags struct was populated with messages data
func (tags AWSLambdaSQSSpanTags) IsZero() bool {
return len(tags.Messages) == 0
}

// AWSLambdaSpanTags contains fields within the `data.lambda` section of an OT span document
type AWSLambdaSpanTags struct {
// ARN is the ARN of invoked AWS Lambda function with the version attached
Expand All @@ -764,6 +799,8 @@ type AWSLambdaSpanTags struct {
CloudWatch *AWSLambdaCloudWatchSpanTags `json:"cw,omitempty"`
// S3 holds the details of a S3 events associated with this lambda
S3 *AWSLambdaS3SpanTags
// SQS holds the details of a SQS events associated with this lambda
SQS *AWSLambdaSQSSpanTags
}

// NewAWSLambdaSpanTags extracts AWS Lambda entry span tags from a tracer span
Expand Down Expand Up @@ -794,6 +831,10 @@ func NewAWSLambdaSpanTags(span *spanS) AWSLambdaSpanTags {
tags.S3 = &st
}

if sqs := NewAWSLambdaSQSSpanTags(span); !sqs.IsZero() {
tags.SQS = &sqs
}

return tags
}

Expand Down
17 changes: 17 additions & 0 deletions json_span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ func TestNewAWSLambdaSpanData(t *testing.T) {
},
},
},
"aws:sqs": {
Tags: opentracing.Tags{
"sqs.messages": []instana.AWSSQSMessageTags{{Queue: "q1"}, {Queue: "q2"}, {Queue: "q3"}, {Queue: "q4"}},
},
Expected: instana.AWSLambdaSpanData{
Snapshot: instana.AWSLambdaSpanTags{
ARN: "lambda-arn-1",
Runtime: "go",
Name: "test-lambda",
Version: "42",
Trigger: "aws:sqs",
SQS: &instana.AWSLambdaSQSSpanTags{
Messages: []instana.AWSSQSMessageTags{{Queue: "q1"}, {Queue: "q2"}, {Queue: "q3"}},
},
},
},
},
}

for trigger, example := range examples {
Expand Down

0 comments on commit 9de1ee0

Please sign in to comment.