Skip to content

Commit 95ad12a

Browse files
authored
[exporter/awsxray] Support new X-Ray specific service name attributes as Segment name (#22835)
XRayExporter will be extended to support the new AWS specific service name attributes changed in open-telemetry/opentelemetry-java-contrib#802 for X-Ray trace segment name. The new Span attribute aws.local.service will be set as Server kind segment name, and aws.remote.service attribute will set as Client kind subsegment name if the attribute exists.
1 parent 948f5d8 commit 95ad12a

File tree

3 files changed

+209
-3
lines changed

3 files changed

+209
-3
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use this changelog template to create an entry for release notes.
2+
# If your change doesn't affect end users, such as a test fix or a tooling change,
3+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
4+
5+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
6+
change_type: enhancement
7+
8+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
9+
component: awsxrayexporter
10+
11+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
12+
note: Support 2 new aws service name attributes for populating X-Ray segment name
13+
14+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
15+
issues: [22835]
16+
17+
# (Optional) One or more lines of additional information to render under the primary note.
18+
# These lines will be padded with 2 spaces and then inserted directly into the document.
19+
# Use pipe (|) for multiline entries.
20+
subtext:

exporter/awsxrayexporter/internal/translator/segment.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ const (
3333
OriginAppRunner = "AWS::AppRunner::Service"
3434
)
3535

36+
// x-ray only span attributes - https://github.com/open-telemetry/opentelemetry-java-contrib/pull/802
37+
const (
38+
awsLocalService = "aws.local.service"
39+
awsRemoteService = "aws.remote.service"
40+
)
41+
3642
var (
3743
// reInvalidSpanCharacters defines the invalid letters in a span name as per
3844
// https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html
@@ -107,9 +113,24 @@ func MakeSegment(span ptrace.Span, resource pcommon.Resource, indexedAttrs []str
107113

108114
// X-Ray segment names are service names, unlike span names which are methods. Try to find a service name.
109115

110-
// peer.service should always be prioritized for segment names when set because it is what the user decided.
111-
if peerService, ok := attributes.Get(conventions.AttributePeerService); ok {
112-
name = peerService.Str()
116+
// support x-ray specific service name attributes as segment name if it exists
117+
if span.Kind() == ptrace.SpanKindServer || span.Kind() == ptrace.SpanKindConsumer {
118+
if localServiceName, ok := attributes.Get(awsLocalService); ok {
119+
name = localServiceName.Str()
120+
}
121+
}
122+
if span.Kind() == ptrace.SpanKindClient || span.Kind() == ptrace.SpanKindProducer {
123+
if remoteServiceName, ok := attributes.Get(awsRemoteService); ok {
124+
name = remoteServiceName.Str()
125+
}
126+
}
127+
128+
// peer.service should always be prioritized for segment names when it set by users and
129+
// the new x-ray specific service name attributes are not found
130+
if name == "" {
131+
if peerService, ok := attributes.Get(conventions.AttributePeerService); ok {
132+
name = peerService.Str()
133+
}
113134
}
114135

115136
if namespace == "" {

exporter/awsxrayexporter/internal/translator/segment_test.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,117 @@ func TestSegmentWith2LogGroupsFromConfig(t *testing.T) {
891891
assert.Equal(t, cwl, segment.AWS.CWLogs)
892892
}
893893

894+
func TestClientSpanWithAwsRemoteServiceName(t *testing.T) {
895+
spanName := "ABC.payment"
896+
parentSpanID := newSegmentID()
897+
user := "testingT"
898+
attributes := make(map[string]interface{})
899+
attributes[conventions.AttributeHTTPMethod] = "POST"
900+
attributes[conventions.AttributeHTTPScheme] = "https"
901+
attributes[conventions.AttributeHTTPHost] = "payment.amazonaws.com"
902+
attributes[conventions.AttributeHTTPTarget] = "/"
903+
attributes[conventions.AttributeRPCService] = "ABC"
904+
attributes[awsRemoteService] = "PaymentService"
905+
906+
resource := constructDefaultResource()
907+
span := constructClientSpan(parentSpanID, spanName, 0, "OK", attributes)
908+
909+
segment, _ := MakeSegment(span, resource, nil, false, nil)
910+
assert.Equal(t, "PaymentService", *segment.Name)
911+
assert.Equal(t, "subsegment", *segment.Type)
912+
913+
jsonStr, err := MakeSegmentDocumentString(span, resource, nil, false, nil)
914+
915+
assert.NotNil(t, jsonStr)
916+
assert.Nil(t, err)
917+
assert.True(t, strings.Contains(jsonStr, "PaymentService"))
918+
assert.False(t, strings.Contains(jsonStr, user))
919+
assert.False(t, strings.Contains(jsonStr, "user"))
920+
}
921+
922+
func TestProducerSpanWithAwsRemoteServiceName(t *testing.T) {
923+
spanName := "ABC.payment"
924+
parentSpanID := newSegmentID()
925+
user := "testingT"
926+
attributes := make(map[string]interface{})
927+
attributes[conventions.AttributeHTTPMethod] = "POST"
928+
attributes[conventions.AttributeHTTPScheme] = "https"
929+
attributes[conventions.AttributeHTTPHost] = "payment.amazonaws.com"
930+
attributes[conventions.AttributeHTTPTarget] = "/"
931+
attributes[conventions.AttributeRPCService] = "ABC"
932+
attributes[awsRemoteService] = "ProducerService"
933+
934+
resource := constructDefaultResource()
935+
span := constructProducerSpan(parentSpanID, spanName, 0, "OK", attributes)
936+
937+
segment, _ := MakeSegment(span, resource, nil, false, nil)
938+
assert.Equal(t, "ProducerService", *segment.Name)
939+
assert.Equal(t, "subsegment", *segment.Type)
940+
941+
jsonStr, err := MakeSegmentDocumentString(span, resource, nil, false, nil)
942+
943+
assert.NotNil(t, jsonStr)
944+
assert.Nil(t, err)
945+
assert.True(t, strings.Contains(jsonStr, "ProducerService"))
946+
assert.False(t, strings.Contains(jsonStr, user))
947+
assert.False(t, strings.Contains(jsonStr, "user"))
948+
}
949+
950+
func TestConsumerSpanWithAwsRemoteServiceName(t *testing.T) {
951+
spanName := "ABC.payment"
952+
parentSpanID := newSegmentID()
953+
user := "testingT"
954+
attributes := make(map[string]interface{})
955+
attributes[conventions.AttributeHTTPMethod] = "POST"
956+
attributes[conventions.AttributeHTTPScheme] = "https"
957+
attributes[conventions.AttributeHTTPHost] = "payment.amazonaws.com"
958+
attributes[conventions.AttributeHTTPTarget] = "/"
959+
attributes[conventions.AttributeRPCService] = "ABC"
960+
attributes[awsLocalService] = "ConsumerService"
961+
962+
resource := constructDefaultResource()
963+
span := constructConsumerSpan(parentSpanID, spanName, 0, "OK", attributes)
964+
965+
segment, _ := MakeSegment(span, resource, nil, false, nil)
966+
assert.Equal(t, "ConsumerService", *segment.Name)
967+
968+
jsonStr, err := MakeSegmentDocumentString(span, resource, nil, false, nil)
969+
970+
assert.NotNil(t, jsonStr)
971+
assert.Nil(t, err)
972+
assert.True(t, strings.Contains(jsonStr, "ConsumerService"))
973+
assert.False(t, strings.Contains(jsonStr, user))
974+
assert.False(t, strings.Contains(jsonStr, "user"))
975+
}
976+
977+
func TestServerSpanWithAwsLocalServiceName(t *testing.T) {
978+
spanName := "ABC.payment"
979+
parentSpanID := newSegmentID()
980+
user := "testingT"
981+
attributes := make(map[string]interface{})
982+
attributes[conventions.AttributeHTTPMethod] = "POST"
983+
attributes[conventions.AttributeHTTPScheme] = "https"
984+
attributes[conventions.AttributeHTTPHost] = "payment.amazonaws.com"
985+
attributes[conventions.AttributeHTTPTarget] = "/"
986+
attributes[conventions.AttributeRPCService] = "ABC"
987+
attributes[awsLocalService] = "PaymentLocalService"
988+
attributes[awsRemoteService] = "PaymentService"
989+
990+
resource := constructDefaultResource()
991+
span := constructServerSpan(parentSpanID, spanName, 0, "OK", attributes)
992+
993+
segment, _ := MakeSegment(span, resource, nil, false, nil)
994+
assert.Equal(t, "PaymentLocalService", *segment.Name)
995+
996+
jsonStr, err := MakeSegmentDocumentString(span, resource, nil, false, nil)
997+
998+
assert.NotNil(t, jsonStr)
999+
assert.Nil(t, err)
1000+
assert.True(t, strings.Contains(jsonStr, "PaymentLocalService"))
1001+
assert.False(t, strings.Contains(jsonStr, user))
1002+
assert.False(t, strings.Contains(jsonStr, "user"))
1003+
}
1004+
8941005
func constructClientSpan(parentSpanID pcommon.SpanID, name string, code ptrace.StatusCode, message string, attributes map[string]interface{}) ptrace.Span {
8951006
var (
8961007
traceID = newTraceID()
@@ -945,6 +1056,60 @@ func constructServerSpan(parentSpanID pcommon.SpanID, name string, code ptrace.S
9451056
return span
9461057
}
9471058

1059+
func constructConsumerSpan(parentSpanID pcommon.SpanID, name string, code ptrace.StatusCode, message string, attributes map[string]interface{}) ptrace.Span {
1060+
var (
1061+
traceID = newTraceID()
1062+
spanID = newSegmentID()
1063+
endTime = time.Now()
1064+
startTime = endTime.Add(-215 * time.Millisecond)
1065+
spanAttributes = constructSpanAttributes(attributes)
1066+
)
1067+
1068+
span := ptrace.NewSpan()
1069+
span.SetTraceID(traceID)
1070+
span.SetSpanID(spanID)
1071+
span.SetParentSpanID(parentSpanID)
1072+
span.SetName(name)
1073+
span.SetKind(ptrace.SpanKindConsumer)
1074+
span.SetStartTimestamp(pcommon.NewTimestampFromTime(startTime))
1075+
span.SetEndTimestamp(pcommon.NewTimestampFromTime(endTime))
1076+
1077+
status := ptrace.NewStatus()
1078+
status.SetCode(code)
1079+
status.SetMessage(message)
1080+
status.CopyTo(span.Status())
1081+
1082+
spanAttributes.CopyTo(span.Attributes())
1083+
return span
1084+
}
1085+
1086+
func constructProducerSpan(parentSpanID pcommon.SpanID, name string, code ptrace.StatusCode, message string, attributes map[string]interface{}) ptrace.Span {
1087+
var (
1088+
traceID = newTraceID()
1089+
spanID = newSegmentID()
1090+
endTime = time.Now()
1091+
startTime = endTime.Add(-215 * time.Millisecond)
1092+
spanAttributes = constructSpanAttributes(attributes)
1093+
)
1094+
1095+
span := ptrace.NewSpan()
1096+
span.SetTraceID(traceID)
1097+
span.SetSpanID(spanID)
1098+
span.SetParentSpanID(parentSpanID)
1099+
span.SetName(name)
1100+
span.SetKind(ptrace.SpanKindProducer)
1101+
span.SetStartTimestamp(pcommon.NewTimestampFromTime(startTime))
1102+
span.SetEndTimestamp(pcommon.NewTimestampFromTime(endTime))
1103+
1104+
status := ptrace.NewStatus()
1105+
status.SetCode(code)
1106+
status.SetMessage(message)
1107+
status.CopyTo(span.Status())
1108+
1109+
spanAttributes.CopyTo(span.Attributes())
1110+
return span
1111+
}
1112+
9481113
func constructSpanAttributes(attributes map[string]interface{}) pcommon.Map {
9491114
attrs := pcommon.NewMap()
9501115
for key, value := range attributes {

0 commit comments

Comments
 (0)