Skip to content

Commit

Permalink
support extraction from MultiValueHeaders in ALB target group requests
Browse files Browse the repository at this point in the history
  • Loading branch information
quietsato committed Nov 5, 2024
1 parent c5769f7 commit 6eda679
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
6 changes: 5 additions & 1 deletion pkg/serverless/trace/propagation/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ func (e Extractor) extract(event interface{}) (*TraceContext, error) {
case events.APIGatewayCustomAuthorizerRequestTypeRequest:
carrier, err = headersCarrier(ev.Headers)
case events.ALBTargetGroupRequest:
carrier, err = headersCarrier(ev.Headers)
if len(ev.Headers) > 0 {
carrier, err = headersCarrier(ev.Headers)
} else {
carrier, err = multiValueHeadersCarrier(ev.MultiValueHeaders)
}
case events.LambdaFunctionURLRequest:
carrier, err = headersCarrier(ev.Headers)
case events.StepFunctionPayload:
Expand Down
18 changes: 18 additions & 0 deletions pkg/serverless/trace/propagation/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ var (
}
)

func toMultiValueHeaders(headers map[string]string) map[string][]string {
mvh := make(map[string][]string)
for k, v := range headers {
mvh[k] = []string{v, "dummy"}
}
return mvh
}

func TestNilPropagator(t *testing.T) {
var extractor Extractor
tc, err := extractor.Extract([]byte(`{"headers":` + headersAll + `}`))
Expand Down Expand Up @@ -533,6 +541,16 @@ func TestExtractorExtract(t *testing.T) {
expCtx: ddTraceContext,
expNoErr: true,
},
{
name: "ALBTargetGroupRequestMultiValueHeaders",
events: []interface{}{
events.ALBTargetGroupRequest{
MultiValueHeaders: toMultiValueHeaders(headersMapAll),
},
},
expCtx: ddTraceContext,
expNoErr: true,
},

// events.LambdaFunctionURLRequest:
{
Expand Down
17 changes: 13 additions & 4 deletions pkg/serverless/trigger/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,20 @@ func GetTagsFromALBTargetGroupRequest(event events.ALBTargetGroupRequest) map[st
httpTags := make(map[string]string)
httpTags["http.url_details.path"] = event.Path
httpTags["http.method"] = event.HTTPMethod
if event.Headers != nil {
if event.Headers["Referer"] != "" {
httpTags["http.referer"] = event.Headers["Referer"]

headers := event.Headers
if headers == nil && event.MultiValueHeaders != nil {
headers = make(map[string]string)
for k, v := range event.MultiValueHeaders {
headers[k] = v[0]
}
if ua := event.Headers["User-Agent"]; ua != "" {
}

if headers != nil {
if headers["Referer"] != "" {
httpTags["http.referer"] = headers["Referer"]
}
if ua := headers["User-Agent"]; ua != "" {
httpTags["http.useragent"] = ua
}
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/serverless/trigger/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,25 @@ func TestGetTagsFromALBTargetGroupRequest(t *testing.T) {
}, httpTags)
}

func TestGetTagsFromALBTargetGroupRequestMultiValueHeaders(t *testing.T) {
event := events.ALBTargetGroupRequest{
MultiValueHeaders: map[string][]string{
"key": {"val"},
"Referer": {"referer"},
},
Path: "path",
HTTPMethod: "http-method",
}

httpTags := GetTagsFromALBTargetGroupRequest(event)

assert.Equal(t, map[string]string{
"http.url_details.path": "path",
"http.method": "http-method",
"http.referer": "referer",
}, httpTags)
}

func TestGetTagsFromFunctionURLRequest(t *testing.T) {
event := events.LambdaFunctionURLRequest{
Headers: map[string]string{
Expand Down

0 comments on commit 6eda679

Please sign in to comment.