Skip to content

Commit

Permalink
[exporter/awsxray] Fix DynamoDB table name translation (#19204)
Browse files Browse the repository at this point in the history
* fix dynamodb table name translation

* added changelog

* add table_names property to segment
  • Loading branch information
jj22ee authored Apr 10, 2023
1 parent a93014c commit 3dc9d21
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
16 changes: 16 additions & 0 deletions .chloggen/xray-exporter-dynamodb-translation-fix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'bug_fix'

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: 'awsxrayexporter'

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Fixed DynamoDB table name from being set as an empty string when reading from attributes"

# One or more tracking issues related to the change
issues: [19204]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
12 changes: 11 additions & 1 deletion exporter/awsxrayexporter/internal/translator/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func makeAws(attributes map[string]pcommon.Value, resource pcommon.Resource, log
requestID string
queueURL string
tableName string
tableNames []string
sdk string
sdkName string
sdkLanguage string
Expand Down Expand Up @@ -167,7 +168,15 @@ func makeAws(attributes map[string]pcommon.Value, resource pcommon.Resource, log
queueURL = value.Str()
}
if value, ok := attributes[conventions.AttributeAWSDynamoDBTableNames]; ok {
tableName = value.Str()
if value.Slice().Len() == 1 {
tableName = value.Slice().At(0).Str()
} else if value.Slice().Len() > 1 {
tableName = ""
tableNames = []string{}
for i := 0; i < value.Slice().Len(); i++ {
tableNames = append(tableNames, value.Slice().At(i).Str())
}
}
}

// EC2 - add ec2 metadata to xray request if
Expand Down Expand Up @@ -264,6 +273,7 @@ func makeAws(attributes map[string]pcommon.Value, resource pcommon.Resource, log
RequestID: awsxray.String(requestID),
QueueURL: awsxray.String(queueURL),
TableName: awsxray.String(tableName),
TableNames: tableNames,
}
return filtered, awsData
}
Expand Down
3 changes: 2 additions & 1 deletion exporter/awsxrayexporter/internal/translator/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ func TestAwsWithDynamoDbAlternateAttribute(t *testing.T) {
func TestAwsWithDynamoDbSemConvAttributes(t *testing.T) {
tableName := "MyTable"
attributes := make(map[string]pcommon.Value)
attributes[conventions.AttributeAWSDynamoDBTableNames] = pcommon.NewValueStr(tableName)
attributes[conventions.AttributeAWSDynamoDBTableNames] = pcommon.NewValueSlice()
attributes[conventions.AttributeAWSDynamoDBTableNames].Slice().AppendEmpty().SetStr(tableName)

filtered, awsData := makeAws(attributes, pcommon.NewResource(), nil)

Expand Down
32 changes: 32 additions & 0 deletions exporter/awsxrayexporter/internal/translator/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,38 @@ func TestFilteredAttributesMetadata(t *testing.T) {
}, segment.Metadata["default"]["map_value"])
}

func TestSpanWithSingleDynamoDBTableHasTableName(t *testing.T) {
spanName := "/api/locations"
parentSpanID := newSegmentID()
attributes := make(map[string]interface{})
attributes[conventions.AttributeAWSDynamoDBTableNames] = []string{"table1"}
resource := constructDefaultResource()
span := constructServerSpan(parentSpanID, spanName, ptrace.StatusCodeError, "OK", attributes)

segment, _ := MakeSegment(span, resource, nil, false, nil)

assert.NotNil(t, segment)
assert.Equal(t, "table1", *segment.AWS.TableName)
assert.Nil(t, segment.AWS.TableNames)
assert.Equal(t, []interface{}{"table1"}, segment.Metadata["default"][conventions.AttributeAWSDynamoDBTableNames])
}

func TestSpanWithMultipleDynamoDBTablesHasTableNames(t *testing.T) {
spanName := "/api/locations"
parentSpanID := newSegmentID()
attributes := make(map[string]interface{})
attributes[conventions.AttributeAWSDynamoDBTableNames] = []string{"table1", "table2"}
resource := constructDefaultResource()
span := constructServerSpan(parentSpanID, spanName, ptrace.StatusCodeError, "OK", attributes)

segment, _ := MakeSegment(span, resource, nil, false, nil)

assert.NotNil(t, segment)
assert.Nil(t, segment.AWS.TableName)
assert.Equal(t, []string{"table1", "table2"}, segment.AWS.TableNames)
assert.Equal(t, []interface{}{"table1", "table2"}, segment.Metadata["default"][conventions.AttributeAWSDynamoDBTableNames])
}

func TestSegmentWithLogGroupsFromConfig(t *testing.T) {
spanName := "/api/locations"
parentSpanID := newSegmentID()
Expand Down
15 changes: 8 additions & 7 deletions internal/aws/xray/tracesegment.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ type AWSData struct {
XRay *XRayMetaData `json:"xray,omitempty"`

// For both segment and subsegments
AccountID *string `json:"account_id,omitempty"`
Operation *string `json:"operation,omitempty"`
RemoteRegion *string `json:"region,omitempty"`
RequestID *string `json:"request_id,omitempty"`
QueueURL *string `json:"queue_url,omitempty"`
TableName *string `json:"table_name,omitempty"`
Retries *int64 `json:"retries,omitempty"`
AccountID *string `json:"account_id,omitempty"`
Operation *string `json:"operation,omitempty"`
RemoteRegion *string `json:"region,omitempty"`
RequestID *string `json:"request_id,omitempty"`
QueueURL *string `json:"queue_url,omitempty"`
TableName *string `json:"table_name,omitempty"`
TableNames []string `json:"table_names,omitempty"`
Retries *int64 `json:"retries,omitempty"`
}

// EC2Metadata represents the EC2 metadata field
Expand Down

0 comments on commit 3dc9d21

Please sign in to comment.