Skip to content

Commit

Permalink
[exporter/awsxray] Support new X-Ray specific service name attributes…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
mxiamxia committed May 31, 2023
1 parent 948f5d8 commit 95ad12a
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 3 deletions.
20 changes: 20 additions & 0 deletions .chloggen/support-new-xray-service-attribute.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# 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: Support 2 new aws service name attributes for populating X-Ray segment name

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [22835]

# (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:
27 changes: 24 additions & 3 deletions exporter/awsxrayexporter/internal/translator/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ const (
OriginAppRunner = "AWS::AppRunner::Service"
)

// x-ray only span attributes - https://github.com/open-telemetry/opentelemetry-java-contrib/pull/802
const (
awsLocalService = "aws.local.service"
awsRemoteService = "aws.remote.service"
)

var (
// reInvalidSpanCharacters defines the invalid letters in a span name as per
// https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html
Expand Down Expand Up @@ -107,9 +113,24 @@ func MakeSegment(span ptrace.Span, resource pcommon.Resource, indexedAttrs []str

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

// peer.service should always be prioritized for segment names when set because it is what the user decided.
if peerService, ok := attributes.Get(conventions.AttributePeerService); ok {
name = peerService.Str()
// support x-ray specific service name attributes as segment name if it exists
if span.Kind() == ptrace.SpanKindServer || span.Kind() == ptrace.SpanKindConsumer {
if localServiceName, ok := attributes.Get(awsLocalService); ok {
name = localServiceName.Str()
}
}
if span.Kind() == ptrace.SpanKindClient || span.Kind() == ptrace.SpanKindProducer {
if remoteServiceName, ok := attributes.Get(awsRemoteService); ok {
name = remoteServiceName.Str()
}
}

// peer.service should always be prioritized for segment names when it set by users and
// the new x-ray specific service name attributes are not found
if name == "" {
if peerService, ok := attributes.Get(conventions.AttributePeerService); ok {
name = peerService.Str()
}
}

if namespace == "" {
Expand Down
165 changes: 165 additions & 0 deletions exporter/awsxrayexporter/internal/translator/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,117 @@ func TestSegmentWith2LogGroupsFromConfig(t *testing.T) {
assert.Equal(t, cwl, segment.AWS.CWLogs)
}

func TestClientSpanWithAwsRemoteServiceName(t *testing.T) {
spanName := "ABC.payment"
parentSpanID := newSegmentID()
user := "testingT"
attributes := make(map[string]interface{})
attributes[conventions.AttributeHTTPMethod] = "POST"
attributes[conventions.AttributeHTTPScheme] = "https"
attributes[conventions.AttributeHTTPHost] = "payment.amazonaws.com"
attributes[conventions.AttributeHTTPTarget] = "/"
attributes[conventions.AttributeRPCService] = "ABC"
attributes[awsRemoteService] = "PaymentService"

resource := constructDefaultResource()
span := constructClientSpan(parentSpanID, spanName, 0, "OK", attributes)

segment, _ := MakeSegment(span, resource, nil, false, nil)
assert.Equal(t, "PaymentService", *segment.Name)
assert.Equal(t, "subsegment", *segment.Type)

jsonStr, err := MakeSegmentDocumentString(span, resource, nil, false, nil)

assert.NotNil(t, jsonStr)
assert.Nil(t, err)
assert.True(t, strings.Contains(jsonStr, "PaymentService"))
assert.False(t, strings.Contains(jsonStr, user))
assert.False(t, strings.Contains(jsonStr, "user"))
}

func TestProducerSpanWithAwsRemoteServiceName(t *testing.T) {
spanName := "ABC.payment"
parentSpanID := newSegmentID()
user := "testingT"
attributes := make(map[string]interface{})
attributes[conventions.AttributeHTTPMethod] = "POST"
attributes[conventions.AttributeHTTPScheme] = "https"
attributes[conventions.AttributeHTTPHost] = "payment.amazonaws.com"
attributes[conventions.AttributeHTTPTarget] = "/"
attributes[conventions.AttributeRPCService] = "ABC"
attributes[awsRemoteService] = "ProducerService"

resource := constructDefaultResource()
span := constructProducerSpan(parentSpanID, spanName, 0, "OK", attributes)

segment, _ := MakeSegment(span, resource, nil, false, nil)
assert.Equal(t, "ProducerService", *segment.Name)
assert.Equal(t, "subsegment", *segment.Type)

jsonStr, err := MakeSegmentDocumentString(span, resource, nil, false, nil)

assert.NotNil(t, jsonStr)
assert.Nil(t, err)
assert.True(t, strings.Contains(jsonStr, "ProducerService"))
assert.False(t, strings.Contains(jsonStr, user))
assert.False(t, strings.Contains(jsonStr, "user"))
}

func TestConsumerSpanWithAwsRemoteServiceName(t *testing.T) {
spanName := "ABC.payment"
parentSpanID := newSegmentID()
user := "testingT"
attributes := make(map[string]interface{})
attributes[conventions.AttributeHTTPMethod] = "POST"
attributes[conventions.AttributeHTTPScheme] = "https"
attributes[conventions.AttributeHTTPHost] = "payment.amazonaws.com"
attributes[conventions.AttributeHTTPTarget] = "/"
attributes[conventions.AttributeRPCService] = "ABC"
attributes[awsLocalService] = "ConsumerService"

resource := constructDefaultResource()
span := constructConsumerSpan(parentSpanID, spanName, 0, "OK", attributes)

segment, _ := MakeSegment(span, resource, nil, false, nil)
assert.Equal(t, "ConsumerService", *segment.Name)

jsonStr, err := MakeSegmentDocumentString(span, resource, nil, false, nil)

assert.NotNil(t, jsonStr)
assert.Nil(t, err)
assert.True(t, strings.Contains(jsonStr, "ConsumerService"))
assert.False(t, strings.Contains(jsonStr, user))
assert.False(t, strings.Contains(jsonStr, "user"))
}

func TestServerSpanWithAwsLocalServiceName(t *testing.T) {
spanName := "ABC.payment"
parentSpanID := newSegmentID()
user := "testingT"
attributes := make(map[string]interface{})
attributes[conventions.AttributeHTTPMethod] = "POST"
attributes[conventions.AttributeHTTPScheme] = "https"
attributes[conventions.AttributeHTTPHost] = "payment.amazonaws.com"
attributes[conventions.AttributeHTTPTarget] = "/"
attributes[conventions.AttributeRPCService] = "ABC"
attributes[awsLocalService] = "PaymentLocalService"
attributes[awsRemoteService] = "PaymentService"

resource := constructDefaultResource()
span := constructServerSpan(parentSpanID, spanName, 0, "OK", attributes)

segment, _ := MakeSegment(span, resource, nil, false, nil)
assert.Equal(t, "PaymentLocalService", *segment.Name)

jsonStr, err := MakeSegmentDocumentString(span, resource, nil, false, nil)

assert.NotNil(t, jsonStr)
assert.Nil(t, err)
assert.True(t, strings.Contains(jsonStr, "PaymentLocalService"))
assert.False(t, strings.Contains(jsonStr, user))
assert.False(t, strings.Contains(jsonStr, "user"))
}

func constructClientSpan(parentSpanID pcommon.SpanID, name string, code ptrace.StatusCode, message string, attributes map[string]interface{}) ptrace.Span {
var (
traceID = newTraceID()
Expand Down Expand Up @@ -945,6 +1056,60 @@ func constructServerSpan(parentSpanID pcommon.SpanID, name string, code ptrace.S
return span
}

func constructConsumerSpan(parentSpanID pcommon.SpanID, name string, code ptrace.StatusCode, message string, attributes map[string]interface{}) ptrace.Span {
var (
traceID = newTraceID()
spanID = newSegmentID()
endTime = time.Now()
startTime = endTime.Add(-215 * time.Millisecond)
spanAttributes = constructSpanAttributes(attributes)
)

span := ptrace.NewSpan()
span.SetTraceID(traceID)
span.SetSpanID(spanID)
span.SetParentSpanID(parentSpanID)
span.SetName(name)
span.SetKind(ptrace.SpanKindConsumer)
span.SetStartTimestamp(pcommon.NewTimestampFromTime(startTime))
span.SetEndTimestamp(pcommon.NewTimestampFromTime(endTime))

status := ptrace.NewStatus()
status.SetCode(code)
status.SetMessage(message)
status.CopyTo(span.Status())

spanAttributes.CopyTo(span.Attributes())
return span
}

func constructProducerSpan(parentSpanID pcommon.SpanID, name string, code ptrace.StatusCode, message string, attributes map[string]interface{}) ptrace.Span {
var (
traceID = newTraceID()
spanID = newSegmentID()
endTime = time.Now()
startTime = endTime.Add(-215 * time.Millisecond)
spanAttributes = constructSpanAttributes(attributes)
)

span := ptrace.NewSpan()
span.SetTraceID(traceID)
span.SetSpanID(spanID)
span.SetParentSpanID(parentSpanID)
span.SetName(name)
span.SetKind(ptrace.SpanKindProducer)
span.SetStartTimestamp(pcommon.NewTimestampFromTime(startTime))
span.SetEndTimestamp(pcommon.NewTimestampFromTime(endTime))

status := ptrace.NewStatus()
status.SetCode(code)
status.SetMessage(message)
status.CopyTo(span.Status())

spanAttributes.CopyTo(span.Attributes())
return span
}

func constructSpanAttributes(attributes map[string]interface{}) pcommon.Map {
attrs := pcommon.NewMap()
for key, value := range attributes {
Expand Down

0 comments on commit 95ad12a

Please sign in to comment.