Skip to content

Commit

Permalink
Fix the raw-message-delivery case.
Browse files Browse the repository at this point in the history
From #19888
  • Loading branch information
purple4reina committed Nov 3, 2023
1 parent d4d74aa commit cb068c0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
21 changes: 15 additions & 6 deletions pkg/serverless/trace/propagation/carriers.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,23 @@ func sqsMessageCarrier(event events.SQSMessage) (tracer.TextMapReader, error) {
// context from the events.SQSMessageAttribute field on an events.SQSMessage
// type.
func sqsMessageAttrCarrier(attr events.SQSMessageAttribute) (tracer.TextMapReader, error) {
var carrier tracer.TextMapCarrier
if attr.DataType != "String" {
var bytes []byte
switch attr.DataType {
case "String":
if attr.StringValue == nil {
return nil, errors.New("String value not found in _datadog payload")
}
bytes = []byte(*attr.StringValue)
case "Binary":
// SNS => SQS => Lambda with SQS's subscription to SNS has enabled RAW
// MESSAGE DELIVERY option
bytes = attr.BinaryValue // No need to decode base64 because already decoded
default:
return nil, errors.New("Unsupported DataType in _datadog payload")
}
if attr.StringValue == nil {
return nil, errors.New("String value not found in _datadog payload")
}
if err := json.Unmarshal([]byte(*attr.StringValue), &carrier); err != nil {

var carrier tracer.TextMapCarrier
if err := json.Unmarshal(bytes, &carrier); err != nil {
return nil, fmt.Errorf("Error unmarshaling payload value: %w", err)
}
return carrier, nil
Expand Down
46 changes: 41 additions & 5 deletions pkg/serverless/trace/propagation/carriers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestSQSMessageAttrCarrier(t *testing.T) {
expNoErr bool
}{
{
name: "datadog-map",
name: "string-datadog-map",
attr: events.SQSMessageAttribute{
DataType: "String",
StringValue: aws.String(headersAll),
Expand All @@ -45,7 +45,7 @@ func TestSQSMessageAttrCarrier(t *testing.T) {
expNoErr: true,
},
{
name: "empty-map",
name: "string-empty-map",
attr: events.SQSMessageAttribute{
DataType: "String",
StringValue: aws.String("{}"),
Expand All @@ -54,7 +54,7 @@ func TestSQSMessageAttrCarrier(t *testing.T) {
expNoErr: true,
},
{
name: "empty-string",
name: "string-empty-string",
attr: events.SQSMessageAttribute{
DataType: "String",
StringValue: aws.String(""),
Expand All @@ -63,18 +63,54 @@ func TestSQSMessageAttrCarrier(t *testing.T) {
expNoErr: false,
},
{
name: "nil-string",
name: "string-nil-string",
attr: events.SQSMessageAttribute{
DataType: "String",
StringValue: nil,
},
expMap: nil,
expNoErr: false,
},
{
name: "binary-datadog-map",
attr: events.SQSMessageAttribute{
DataType: "Binary",
BinaryValue: []byte(headersAll),
},
expMap: headersMapAll,
expNoErr: true,
},
{
name: "binary-empty-map",
attr: events.SQSMessageAttribute{
DataType: "Binary",
BinaryValue: []byte("{}"),
},
expMap: map[string]string{},
expNoErr: true,
},
{
name: "binary-empty-string",
attr: events.SQSMessageAttribute{
DataType: "Binary",
BinaryValue: []byte(""),
},
expMap: nil,
expNoErr: false,
},
{
name: "binary-nil-string",
attr: events.SQSMessageAttribute{
DataType: "Binary",
BinaryValue: nil,
},
expMap: nil,
expNoErr: false,
},
{
name: "wrong-data-type",
attr: events.SQSMessageAttribute{
DataType: "Binary",
DataType: "Purple",
},
expMap: nil,
expNoErr: false,
Expand Down

0 comments on commit cb068c0

Please sign in to comment.