Skip to content

nrlambda: add support for pointer to event #1071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion v3/integrations/nrlambda/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,52 @@ func getEventSourceARN(event interface{}) string {
switch v := event.(type) {
case events.KinesisFirehoseEvent:
return v.DeliveryStreamArn
case *events.KinesisFirehoseEvent:
return getEventSourceARN(safeDereference(v))

case events.KinesisEvent:
if len(v.Records) > 0 {
return v.Records[0].EventSourceArn
}
case *events.KinesisEvent:
return getEventSourceARN(safeDereference(v))

case events.CodeCommitEvent:
if len(v.Records) > 0 {
return v.Records[0].EventSourceARN
}
case *events.CodeCommitEvent:
return getEventSourceARN(safeDereference(v))

case events.DynamoDBEvent:
if len(v.Records) > 0 {
return v.Records[0].EventSourceArn
}
case *events.DynamoDBEvent:
return getEventSourceARN(safeDereference(v))

case events.SQSEvent:
if len(v.Records) > 0 {
return v.Records[0].EventSourceARN
}
case *events.SQSEvent:
return getEventSourceARN(safeDereference(v))

case events.S3Event:
if len(v.Records) > 0 {
return v.Records[0].S3.Bucket.Arn
}
case *events.S3Event:
return getEventSourceARN(safeDereference(v))

case events.SNSEvent:
if len(v.Records) > 0 {
return v.Records[0].EventSubscriptionArn
}
case *events.SNSEvent:
return getEventSourceARN(safeDereference(v))
}

return ""
}

Expand All @@ -54,11 +75,16 @@ func eventWebRequest(event interface{}) *newrelic.WebRequest {
request.Method = r.HTTPMethod
path = r.Path
headers = r.Headers
case *events.APIGatewayProxyRequest:
return eventWebRequest(safeDereference(r))

case events.ALBTargetGroupRequest:
// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#receive-event-from-load-balancer
request.Method = r.HTTPMethod
path = r.Path
headers = r.Headers
case *events.ALBTargetGroupRequest:
return eventWebRequest(safeDereference(r))

default:
return nil
}
Expand Down Expand Up @@ -98,9 +124,15 @@ func eventResponse(event interface{}) *response {
case events.APIGatewayProxyResponse:
code = r.StatusCode
headers = r.Headers
case *events.APIGatewayProxyResponse:
return eventResponse(safeDereference(r))

case events.ALBTargetGroupResponse:
code = r.StatusCode
headers = r.Headers
case *events.ALBTargetGroupResponse:
return eventResponse(safeDereference(r))

default:
return nil
}
Expand All @@ -113,3 +145,11 @@ func eventResponse(event interface{}) *response {
header: hdr,
}
}

func safeDereference[T any](p *T) T {
if p == nil {
var z T
return z
}
return *p
}
150 changes: 142 additions & 8 deletions v3/integrations/nrlambda/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,33 @@ func TestGetEventAttributes(t *testing.T) {
Arn string
}{
{Name: "nil", Input: nil, Arn: ""},

{Name: "SQSEvent empty", Input: events.SQSEvent{}, Arn: ""},
{Name: "SQSEvent", Input: events.SQSEvent{
Records: []events.SQSMessage{{
EventSourceARN: "ARN",
}},
}, Arn: "ARN"},
{Name: "*SQSEvent nil", Input: (*events.SQSEvent)(nil), Arn: ""},
{Name: "*SQSEvent", Input: &events.SQSEvent{
Records: []events.SQSMessage{{
EventSourceARN: "ARN",
}},
}, Arn: "ARN"},

{Name: "SNSEvent empty", Input: events.SNSEvent{}, Arn: ""},
{Name: "SNSEvent", Input: events.SNSEvent{
Records: []events.SNSEventRecord{{
EventSubscriptionArn: "ARN",
}},
}, Arn: "ARN"},
{Name: "*SNSEvent nil", Input: (*events.SNSEvent)(nil), Arn: ""},
{Name: "*SNSEvent", Input: &events.SNSEvent{
Records: []events.SNSEventRecord{{
EventSubscriptionArn: "ARN",
}},
}, Arn: "ARN"},

{Name: "S3Event empty", Input: events.S3Event{}, Arn: ""},
{Name: "S3Event", Input: events.S3Event{
Records: []events.S3EventRecord{{
Expand All @@ -39,27 +54,64 @@ func TestGetEventAttributes(t *testing.T) {
},
}},
}, Arn: "ARN"},
{Name: "*S3Event nil", Input: (*events.S3Event)(nil), Arn: ""},
{Name: "*S3Event", Input: &events.S3Event{
Records: []events.S3EventRecord{{
S3: events.S3Entity{
Bucket: events.S3Bucket{
Arn: "ARN",
},
},
}},
}, Arn: "ARN"},

{Name: "DynamoDBEvent empty", Input: events.DynamoDBEvent{}, Arn: ""},
{Name: "DynamoDBEvent", Input: events.DynamoDBEvent{
Records: []events.DynamoDBEventRecord{{
EventSourceArn: "ARN",
}},
}, Arn: "ARN"},
{Name: "*DynamoDBEvent nil", Input: (*events.DynamoDBEvent)(nil), Arn: ""},
{Name: "*DynamoDBEvent", Input: &events.DynamoDBEvent{
Records: []events.DynamoDBEventRecord{{
EventSourceArn: "ARN",
}},
}, Arn: "ARN"},

{Name: "CodeCommitEvent empty", Input: events.CodeCommitEvent{}, Arn: ""},
{Name: "CodeCommitEvent", Input: events.CodeCommitEvent{
Records: []events.CodeCommitRecord{{
EventSourceARN: "ARN",
}},
}, Arn: "ARN"},
{Name: "*CodeCommitEvent nil", Input: (*events.CodeCommitEvent)(nil), Arn: ""},
{Name: "*CodeCommitEvent", Input: &events.CodeCommitEvent{
Records: []events.CodeCommitRecord{{
EventSourceARN: "ARN",
}},
}, Arn: "ARN"},

{Name: "KinesisEvent empty", Input: events.KinesisEvent{}, Arn: ""},
{Name: "KinesisEvent", Input: events.KinesisEvent{
Records: []events.KinesisEventRecord{{
EventSourceArn: "ARN",
}},
}, Arn: "ARN"},
{Name: "*KinesisEvent nil", Input: (*events.KinesisEvent)(nil), Arn: ""},
{Name: "*KinesisEvent", Input: &events.KinesisEvent{
Records: []events.KinesisEventRecord{{
EventSourceArn: "ARN",
}},
}, Arn: "ARN"},

{Name: "KinesisFirehoseEvent empty", Input: events.KinesisFirehoseEvent{}, Arn: ""},
{Name: "KinesisFirehoseEvent", Input: events.KinesisFirehoseEvent{
DeliveryStreamArn: "ARN",
}, Arn: "ARN"},
{Name: "*KinesisFirehoseEvent nil", Input: (*events.KinesisFirehoseEvent)(nil), Arn: ""},
{Name: "*KinesisFirehoseEvent", Input: &events.KinesisFirehoseEvent{
DeliveryStreamArn: "ARN",
}, Arn: "ARN"},
}

for _, testcase := range testcases {
Expand All @@ -86,15 +138,15 @@ func TestEventWebRequest(t *testing.T) {
transport newrelic.TransportType
}{
{
testname: "empty proxy request",
testname: "empty APIGatewayProxyRequest",
input: events.APIGatewayProxyRequest{},
numHeaders: 0,
method: "",
urlString: "",
transport: newrelic.TransportUnknown,
},
{
testname: "populated proxy request",
testname: "populated APIGatewayProxyRequest",
input: events.APIGatewayProxyRequest{
Headers: map[string]string{
"x-forwarded-port": "4000",
Expand All @@ -109,15 +161,39 @@ func TestEventWebRequest(t *testing.T) {
transport: newrelic.TransportHTTPS,
},
{
testname: "empty alb request",
testname: "nil *APIGatewayProxyRequest",
input: (*events.APIGatewayProxyRequest)(nil),
numHeaders: 0,
method: "",
urlString: "",
transport: newrelic.TransportUnknown,
},
{
testname: "populated *APIGatewayProxyRequest",
input: &events.APIGatewayProxyRequest{
Headers: map[string]string{
"x-forwarded-port": "4000",
"x-forwarded-proto": "HTTPS",
},
HTTPMethod: "GET",
Path: "the/path",
},
numHeaders: 2,
method: "GET",
urlString: "//:4000/the/path",
transport: newrelic.TransportHTTPS,
},

{
testname: "empty ALBTargetGroupRequest",
input: events.ALBTargetGroupRequest{},
numHeaders: 0,
method: "",
urlString: "",
transport: newrelic.TransportUnknown,
},
{
testname: "populated alb request",
testname: "populated ALBTargetGroupRequest",
input: events.ALBTargetGroupRequest{
Headers: map[string]string{
"x-forwarded-port": "3000",
Expand All @@ -131,6 +207,29 @@ func TestEventWebRequest(t *testing.T) {
urlString: "//:3000/the/path",
transport: newrelic.TransportHTTP,
},
{
testname: "nil *ALBTargetGroupRequest",
input: (*events.ALBTargetGroupRequest)(nil),
numHeaders: 0,
method: "",
urlString: "",
transport: newrelic.TransportUnknown,
},
{
testname: "populated *ALBTargetGroupRequest",
input: &events.ALBTargetGroupRequest{
Headers: map[string]string{
"x-forwarded-port": "3000",
"x-forwarded-proto": "HttP",
},
HTTPMethod: "GET",
Path: "the/path",
},
numHeaders: 2,
method: "GET",
urlString: "//:3000/the/path",
transport: newrelic.TransportHTTP,
},
}

for _, tc := range testcases {
Expand Down Expand Up @@ -168,13 +267,13 @@ func TestEventResponse(t *testing.T) {
code int
}{
{
testname: "empty proxy response",
testname: "empty APIGatewayProxyResponse",
input: events.APIGatewayProxyResponse{},
numHeaders: 0,
code: 0,
},
{
testname: "populated proxy response",
testname: "populated APIGatewayProxyResponse",
input: events.APIGatewayProxyResponse{
StatusCode: 200,
Headers: map[string]string{
Expand All @@ -185,13 +284,31 @@ func TestEventResponse(t *testing.T) {
code: 200,
},
{
testname: "empty alb response",
testname: "nil *APIGatewayProxyResponse",
input: (*events.APIGatewayProxyResponse)(nil),
numHeaders: 0,
code: 0,
},
{
testname: "populated *APIGatewayProxyResponse",
input: &events.APIGatewayProxyResponse{
StatusCode: 200,
Headers: map[string]string{
"x-custom-header": "my custom header value",
},
},
numHeaders: 1,
code: 200,
},

{
testname: "empty ALBTargetGroupResponse",
input: events.ALBTargetGroupResponse{},
numHeaders: 0,
code: 0,
},
{
testname: "populated alb response",
testname: "populated ALBTargetGroupResponse",
input: events.ALBTargetGroupResponse{
StatusCode: 200,
Headers: map[string]string{
Expand All @@ -201,6 +318,23 @@ func TestEventResponse(t *testing.T) {
numHeaders: 1,
code: 200,
},
{
testname: "nil *ALBTargetGroupResponse",
input: (*events.ALBTargetGroupResponse)(nil),
numHeaders: 0,
code: 0,
},
{
testname: "populated *ALBTargetGroupResponse",
input: &events.ALBTargetGroupResponse{
StatusCode: 200,
Headers: map[string]string{
"x-custom-header": "my custom header value",
},
},
numHeaders: 1,
code: 200,
},
}

for _, tc := range testcases {
Expand Down