Skip to content

Commit

Permalink
serverless: add http.route tag when available (#21086)
Browse files Browse the repository at this point in the history
This allows serverless spans to get picked-up by API Catalog, that requires http.route to be present.
  • Loading branch information
Hellzy authored Dec 4, 2023
1 parent 95ef792 commit 315c38f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/serverless/invocationlifecycle/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ func TestTriggerTypesLifecycleEventForAPIGatewayRest(t *testing.T) {
assert.Equal(t, map[string]string{
"function_trigger.event_source_arn": "arn:aws:apigateway:us-east-1::/restapis/1234567890/stages/prod",
"http.method": "POST",
"http.route": "/{proxy+}",
"http.url": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com",
"http.url_details.path": "/prod/path/to/resource",
"http.useragent": "Custom User Agent String",
Expand Down Expand Up @@ -442,6 +443,7 @@ func TestTriggerTypesLifecycleEventForAPIGateway5xxResponse(t *testing.T) {
"cold_start": "false",
"function_trigger.event_source_arn": "arn:aws:apigateway:us-east-1::/restapis/1234567890/stages/prod",
"http.method": "POST",
"http.route": "/{proxy+}",
"http.url": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com",
"http.url_details.path": "/prod/path/to/resource",
"http.useragent": "Custom User Agent String",
Expand Down Expand Up @@ -487,6 +489,7 @@ func TestTriggerTypesLifecycleEventForAPIGatewayNonProxy(t *testing.T) {
"cold_start": "false",
"function_trigger.event_source_arn": "arn:aws:apigateway:us-east-1::/restapis/lgxbo6a518/stages/dev",
"http.method": "GET",
"http.route": "/http/get",
"http.url": "lgxbo6a518.execute-api.sa-east-1.amazonaws.com",
"http.url_details.path": "/dev/http/get",
"http.useragent": "curl/7.64.1",
Expand Down Expand Up @@ -535,6 +538,7 @@ func TestTriggerTypesLifecycleEventForAPIGatewayNonProxy5xxResponse(t *testing.T
"cold_start": "false",
"function_trigger.event_source_arn": "arn:aws:apigateway:us-east-1::/restapis/lgxbo6a518/stages/dev",
"http.method": "GET",
"http.route": "/http/get",
"http.url": "lgxbo6a518.execute-api.sa-east-1.amazonaws.com",
"http.url_details.path": "/dev/http/get",
"request_id": "test-request-id",
Expand Down
9 changes: 9 additions & 0 deletions pkg/serverless/trigger/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ func GetTagsFromAPIGatewayEvent(event events.APIGatewayProxyRequest) map[string]
}
httpTags["http.url_details.path"] = event.RequestContext.Path
httpTags["http.method"] = event.RequestContext.HTTPMethod
if event.Resource != "" {
httpTags["http.route"] = event.Resource
}
if event.Headers != nil {
if event.Headers["Referer"] != "" {
httpTags["http.referer"] = event.Headers["Referer"]
Expand All @@ -128,6 +131,9 @@ func GetTagsFromAPIGatewayV2HTTPRequest(event events.APIGatewayV2HTTPRequest) ma
httpTags["http.url"] = event.RequestContext.DomainName
httpTags["http.url_details.path"] = event.RequestContext.HTTP.Path
httpTags["http.method"] = event.RequestContext.HTTP.Method
if event.RouteKey != "" {
httpTags["http.route"] = event.RouteKey
}
if event.Headers != nil {
if event.Headers["Referer"] != "" {
httpTags["http.referer"] = event.Headers["Referer"]
Expand Down Expand Up @@ -164,6 +170,9 @@ func GetTagsFromAPIGatewayCustomAuthorizerRequestTypeEvent(event events.APIGatew
httpTags := make(map[string]string)
httpTags["http.url_details.path"] = event.RequestContext.Path
httpTags["http.method"] = event.HTTPMethod
if event.Resource != "" {
httpTags["http.route"] = event.Resource
}
if referer := event.Headers["Referer"]; referer != "" {
httpTags["http.referer"] = referer
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/serverless/trigger/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func TestExtractFunctionURLEventARN(t *testing.T) {
Path: "path",
HTTPMethod: "http-method",
},
Resource: "/{route}",
}

httpTags := GetTagsFromAPIGatewayEvent(event)
Expand All @@ -247,6 +248,7 @@ func TestExtractFunctionURLEventARN(t *testing.T) {
"http.url_details.path": "path",
"http.method": "http-method",
"http.referer": "referer",
"http.route": "/{route}",
}, httpTags)
}

Expand All @@ -261,6 +263,7 @@ func TestGetTagsFromAPIGatewayEvent(t *testing.T) {
Path: "path",
HTTPMethod: "http-method",
},
Resource: "/{route}",
}

httpTags := GetTagsFromAPIGatewayEvent(event)
Expand All @@ -270,6 +273,7 @@ func TestGetTagsFromAPIGatewayEvent(t *testing.T) {
"http.url_details.path": "path",
"http.method": "http-method",
"http.referer": "referer",
"http.route": "/{route}",
}, httpTags)
}

Expand All @@ -285,6 +289,7 @@ func TestGetTagsFromAPIGatewayV2HTTPRequestNoReferer(t *testing.T) {
Method: "http-method",
},
},
RouteKey: "/{route}",
}

httpTags := GetTagsFromAPIGatewayV2HTTPRequest(event)
Expand All @@ -293,6 +298,7 @@ func TestGetTagsFromAPIGatewayV2HTTPRequestNoReferer(t *testing.T) {
"http.url": "domain-name",
"http.url_details.path": "path",
"http.method": "http-method",
"http.route": "/{route}",
}, httpTags)
}

Expand All @@ -315,13 +321,15 @@ func TestGetTagsFromAPIGatewayCustomAuthorizerRequestTypeEvent(t *testing.T) {
RequestContext: events.APIGatewayCustomAuthorizerRequestTypeRequestContext{
Path: "/path/to/resource",
},
Resource: "/{route}",
}

httpTags := GetTagsFromAPIGatewayCustomAuthorizerRequestTypeEvent(event)

assert.Equal(t, map[string]string{
"http.method": "GET",
"http.url_details.path": "/path/to/resource",
"http.route": "/{route}",
}, httpTags)
}

Expand Down

0 comments on commit 315c38f

Please sign in to comment.