Skip to content

Commit

Permalink
[AWS Xray]Change the type of content_length (#2539)
Browse files Browse the repository at this point in the history
* Change the type of attributes content_length

* Re-run integration test

* Add unit-test
  • Loading branch information
JohnWu20 authored Mar 5, 2021
1 parent c0d9ae9 commit d8ed6a8
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 12 deletions.
3 changes: 2 additions & 1 deletion exporter/awsxrayexporter/translator/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ func TestSpanWithNotEnoughHTTPRequestURLAttributes(t *testing.T) {
assert.Equal(t, "192.168.15.32", *httpData.Request.ClientIP)
assert.Equal(t, "GET", *httpData.Request.Method)
assert.Equal(t, "PostmanRuntime/7.21.0", *httpData.Request.UserAgent)
assert.Equal(t, int64(12452), *httpData.Response.ContentLength)
contentLength := *httpData.Response.ContentLength.(*int64)
assert.Equal(t, int64(12452), contentLength)
assert.Equal(t, int64(200), *httpData.Response.Status)
assert.NotNil(t, filtered)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "api.example.com",
"id": "53995c3f42cd8ad8",
"start_time": 14782323361.271,
"end_time": 14789232374.449,
"type": "subsegment",
"trace_id": "1-581cf771-a006649127e371903a2de979",
"parent_id": "defdfd9912dc5a56",
"namespace": "remote",
"traced": true,
"http": {
"request": {
"url": "https://api.example.com/health",
"method": "POST"
},
"response": {
"status": 200,
"content_length": "861"
}
}
}
4 changes: 2 additions & 2 deletions internal/awsxray/tracesegment.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ type RequestData struct {

// ResponseData provides the shape for unmarshalling the response field.
type ResponseData struct {
Status *int64 `json:"status,omitempty"`
ContentLength *int64 `json:"content_length,omitempty"`
Status *int64 `json:"status,omitempty"`
ContentLength interface{} `json:"content_length,omitempty"`
}

// ECSData provides the shape for unmarshalling the ecs field.
Expand Down
4 changes: 2 additions & 2 deletions internal/awsxray/tracesegment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ var rawExpectedSegmentForInstrumentedApp = Segment{
HTTP: &HTTPData{
Response: &ResponseData{
Status: aws.Int64(200),
ContentLength: aws.Int64(713),
ContentLength: *aws.Float64(713),
},
},
AWS: &AWSData{
Expand Down Expand Up @@ -320,7 +320,7 @@ var rawExpectedSegmentForInstrumentedApp = Segment{
HTTP: &HTTPData{
Response: &ResponseData{
Status: aws.Int64(400),
ContentLength: aws.Int64(112),
ContentLength: *aws.Float64(112),
},
},
AWS: &AWSData{
Expand Down
10 changes: 9 additions & 1 deletion receiver/awsxrayreceiver/internal/translator/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ func addHTTP(seg *awsxray.Segment, span *pdata.Span) {
attrs.UpsertInt(conventions.AttributeHTTPStatusCode, *resp.Status)
}

addInt64(resp.ContentLength, conventions.AttributeHTTPResponseContentLength, &attrs)
switch resp.ContentLength.(type) {
case string:
lengthPointer := resp.ContentLength.(string)
addString(&lengthPointer, conventions.AttributeHTTPResponseContentLength, &attrs)
case float64:
length := resp.ContentLength.(float64)
lengthPointer := int64(length)
addInt64(&lengthPointer, conventions.AttributeHTTPResponseContentLength, &attrs)
}
}

}
60 changes: 54 additions & 6 deletions receiver/awsxrayreceiver/internal/translator/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ func TestTranslation(t *testing.T) {
*subseg7318.Name)
childSpan7318Attrs[conventions.AttributeHTTPStatusCode] = pdata.NewAttributeValueInt(
*subseg7318.HTTP.Response.Status)
childSpan7318Attrs[conventions.AttributeHTTPResponseContentLength] = pdata.NewAttributeValueInt(
*subseg7318.HTTP.Response.ContentLength)

contentLength := subseg7318.HTTP.Response.ContentLength.(float64)
childSpan7318Attrs[conventions.AttributeHTTPResponseContentLength] = pdata.NewAttributeValueInt(int64(contentLength))
childSpan7318Attrs[awsxray.AWSOperationAttribute] = pdata.NewAttributeValueString(
*subseg7318.AWS.Operation)
childSpan7318Attrs[awsxray.AWSRegionAttribute] = pdata.NewAttributeValueString(
Expand Down Expand Up @@ -405,8 +406,8 @@ func TestTranslation(t *testing.T) {
*subseg7163.Name)
childSpan7163Attrs[conventions.AttributeHTTPStatusCode] = pdata.NewAttributeValueInt(
*subseg7163.HTTP.Response.Status)
childSpan7163Attrs[conventions.AttributeHTTPResponseContentLength] = pdata.NewAttributeValueInt(
*subseg7163.HTTP.Response.ContentLength)
contentLength = subseg7163.HTTP.Response.ContentLength.(float64)
childSpan7163Attrs[conventions.AttributeHTTPResponseContentLength] = pdata.NewAttributeValueInt(int64(contentLength))
childSpan7163Attrs[awsxray.AWSOperationAttribute] = pdata.NewAttributeValueString(
*subseg7163.AWS.Operation)
childSpan7163Attrs[awsxray.AWSRegionAttribute] = pdata.NewAttributeValueString(
Expand Down Expand Up @@ -718,8 +719,55 @@ func TestTranslation(t *testing.T) {
*seg.HTTP.Response.Status)
attrs[conventions.AttributeHTTPURL] = pdata.NewAttributeValueString(
*seg.HTTP.Request.URL)
attrs[conventions.AttributeHTTPResponseContentLength] = pdata.NewAttributeValueInt(
*seg.HTTP.Response.ContentLength)
contentLength := seg.HTTP.Response.ContentLength.(float64)
attrs[conventions.AttributeHTTPResponseContentLength] = pdata.NewAttributeValueInt(int64(contentLength))
attrs[awsxray.AWSXRayTracedAttribute] = pdata.NewAttributeValueBool(true)
res := perSpanProperties{
traceID: *seg.TraceID,
spanID: *seg.ID,
parentSpanID: seg.ParentID,
name: *seg.Name,
startTimeSec: *seg.StartTime,
endTimeSec: seg.EndTime,
spanKind: pdata.SpanKindCLIENT,
spanStatus: spanSt{
code: pdata.StatusCodeUnset,
},
attrs: attrs,
}
return []perSpanProperties{res}
},
verification: func(testCase string,
_ *awsxray.Segment,
expectedRs *pdata.ResourceSpans, actualTraces *pdata.Traces, err error) {
assert.NoError(t, err, testCase+": translation should've succeeded")
assert.Equal(t, 1, actualTraces.ResourceSpans().Len(),
testCase+": one segment should translate to 1 ResourceSpans")

actualRs := actualTraces.ResourceSpans().At(0)
compare2ResourceSpans(t, testCase, expectedRs, &actualRs)
},
},
{
testCase: "TranslateIndepSubsegmentForContentLengthString",
samplePath: path.Join("../../../../internal/awsxray", "testdata", "indepSubsegmentWithContentLengthString.txt"),
expectedResourceAttrs: func(seg *awsxray.Segment) map[string]pdata.AttributeValue {
attrs := make(map[string]pdata.AttributeValue)
attrs[conventions.AttributeCloudProvider] = pdata.NewAttributeValueString("unknown")
return attrs
},
propsPerSpan: func(_ string, _ *testing.T, seg *awsxray.Segment) []perSpanProperties {
attrs := make(map[string]pdata.AttributeValue)
attrs[conventions.AttributeHTTPMethod] = pdata.NewAttributeValueString(
*seg.HTTP.Request.Method)
attrs[conventions.AttributeHTTPStatusCode] = pdata.NewAttributeValueInt(
*seg.HTTP.Response.Status)
attrs[conventions.AttributeHTTPURL] = pdata.NewAttributeValueString(
*seg.HTTP.Request.URL)

contentLength := seg.HTTP.Response.ContentLength.(string)
attrs[conventions.AttributeHTTPResponseContentLength] = pdata.NewAttributeValueString(contentLength)

attrs[awsxray.AWSXRayTracedAttribute] = pdata.NewAttributeValueBool(true)
res := perSpanProperties{
traceID: *seg.TraceID,
Expand Down

0 comments on commit d8ed6a8

Please sign in to comment.