Skip to content

Commit

Permalink
Add support for AWS Lambda entry spans triggered by S3 events
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Slotin committed Nov 13, 2020
1 parent f5bc0d3 commit b41749e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
48 changes: 48 additions & 0 deletions json_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,48 @@ func (tags AWSLambdaCloudWatchLogsTags) IsZero() bool {
return tags.Group == "" && tags.Stream == "" && tags.DecodingError == ""
}

// AWSS3EventTags represens metadata for an S3 event
type AWSS3EventTags struct {
Name string `json:"event"`
Bucket string `json:"bucket"`
Object string `json:"object,omitempty"`
}

// AWSLambdaS3SpanTags contains fields within the `data.lambda.s3` section of an OT span document
type AWSLambdaS3SpanTags struct {
Events []AWSS3EventTags `json:"events,omitempty"`
}

// NewAWSLambdaS3SpanTags extracts S3 Event tags for an AWS Lambda entry span. It truncates
// the events list to the first 3 items and limits each object names to the first 200 characters to reduce the payload.
func NewAWSLambdaS3SpanTags(span *spanS) AWSLambdaS3SpanTags {
var tags AWSLambdaS3SpanTags

if events, ok := span.Tags["s3.events"]; ok {
events, ok := events.([]AWSS3EventTags)
if ok {
tags.Events = events
}
}

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

for i := range tags.Events {
if len(tags.Events[i].Object) > 200 {
tags.Events[i].Object = tags.Events[i].Object[:200]
}
}

return tags
}

// IsZero returns true if an AWSLambdaS3SpanTags struct was populated with events data
func (tags AWSLambdaS3SpanTags) IsZero() bool {
return len(tags.Events) == 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 @@ -720,6 +762,8 @@ type AWSLambdaSpanTags struct {
Trigger string `json:"trigger,omitempty"`
// CloudWatch holds the details of a CloudWatch event associated with this lambda
CloudWatch *AWSLambdaCloudWatchSpanTags `json:"cw,omitempty"`
// S3 holds the details of a S3 events associated with this lambda
S3 *AWSLambdaS3SpanTags
}

// NewAWSLambdaSpanTags extracts AWS Lambda entry span tags from a tracer span
Expand All @@ -746,6 +790,10 @@ func NewAWSLambdaSpanTags(span *spanS) AWSLambdaSpanTags {
tags.CloudWatch = &cw
}

if st := NewAWSLambdaS3SpanTags(span); !st.IsZero() {
tags.S3 = &st
}

return tags
}

Expand Down
26 changes: 26 additions & 0 deletions json_span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,32 @@ func TestNewAWSLambdaSpanData(t *testing.T) {
},
},
},
"aws:s3": {
Tags: opentracing.Tags{
"s3.events": []instana.AWSS3EventTags{
{Name: "event1", Bucket: "bucket1", Object: "object1"},
{Name: "event2", Bucket: "bucket2", Object: strings.Repeat("long ", 40) + "object2"},
{Name: "event3", Bucket: "bucket3"},
{Name: "event4", Bucket: "bucket4"},
},
},
Expected: instana.AWSLambdaSpanData{
Snapshot: instana.AWSLambdaSpanTags{
ARN: "lambda-arn-1",
Runtime: "go",
Name: "test-lambda",
Version: "42",
Trigger: "aws:s3",
S3: &instana.AWSLambdaS3SpanTags{
Events: []instana.AWSS3EventTags{
{Name: "event1", Bucket: "bucket1", Object: "object1"},
{Name: "event2", Bucket: "bucket2", Object: strings.Repeat("long ", 40)},
{Name: "event3", Bucket: "bucket3"},
},
},
},
},
},
}

for trigger, example := range examples {
Expand Down

0 comments on commit b41749e

Please sign in to comment.