Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/awsxray] Add span links and messenger field translation to x-ray exporter. #20313

Merged
merged 21 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/awsxray-support-span-links-and-messaging-field.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: 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: Adding translation support for span links and the messaging field for the aws x-ray exporter

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

# (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:
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local/
vendor/

# GoLand IDEA
/.idea/
Expand Down
40 changes: 40 additions & 0 deletions exporter/awsxrayexporter/internal/translator/messaging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package translator // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter/internal/translator"

import (
"strings"

"go.opentelemetry.io/collector/pdata/pcommon"
)

const messagingPrefix = "messaging."

func makeMessaging(attributes map[string]pcommon.Value) (map[string]pcommon.Value, map[string]interface{}) {
var (
filtered = make(map[string]pcommon.Value)
messaging = make(map[string]interface{})
)

for key, value := range attributes {
if strings.HasPrefix(key, messagingPrefix) {
messaging[strings.TrimPrefix(key, messagingPrefix)] = value.AsRaw()
} else {
filtered[key] = value
}
}

return filtered, messaging
}
164 changes: 164 additions & 0 deletions exporter/awsxrayexporter/internal/translator/messaging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package translator // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter/internal/translator"

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/ptrace"
)

func TestMessagingSimple(t *testing.T) {
spanName := "ProcessingMessage"
parentSpanID := newSegmentID()
attributes := make(map[string]interface{})
resource := constructDefaultResource()
span := constructServerSpan(parentSpanID, spanName, ptrace.StatusCodeOk, "OK", attributes)

span.Attributes().PutStr("messaging.operation", "process")
span.Attributes().PutStr("messaging.system", "AmazonSQS")
span.Attributes().PutStr("notMessaging", "myValue")

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

assert.Equal(t, 2, len(segment.Messaging))
assert.Equal(t, "process", segment.Messaging["operation"])
assert.Equal(t, "AmazonSQS", segment.Messaging["system"])
assert.Equal(t, "myValue", segment.Metadata["default"]["notMessaging"])
assert.Equal(t, 0, len(segment.Annotations))

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

assert.True(t, strings.Contains(jsonStr, "messaging"))

assert.True(t, strings.Contains(jsonStr, "operation"))
assert.True(t, strings.Contains(jsonStr, "process"))

assert.True(t, strings.Contains(jsonStr, "system"))
assert.True(t, strings.Contains(jsonStr, "AmazonSQS"))

assert.True(t, strings.Contains(jsonStr, "notMessaging"))
assert.True(t, strings.Contains(jsonStr, "myValue"))
}

func TestMessagingEmpty(t *testing.T) {
spanName := "ProcessingMessage"
parentSpanID := newSegmentID()
attributes := make(map[string]interface{})
resource := constructDefaultResource()
span := constructServerSpan(parentSpanID, spanName, ptrace.StatusCodeOk, "OK", attributes)

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

assert.Equal(t, 0, len(segment.Messaging))

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

assert.False(t, strings.Contains(jsonStr, "messaging"))
}

func TestMessagingComplex(t *testing.T) {
spanName := "ProcessingMessage"
parentSpanID := newSegmentID()
attributes := make(map[string]interface{})
resource := constructDefaultResource()
span := constructServerSpan(parentSpanID, spanName, ptrace.StatusCodeOk, "OK", attributes)

span.Attributes().PutStr("messaging.operation", "process")
span.Attributes().PutStr("messaging.system", "AmazonSQS")
span.Attributes().PutInt("messaging.message_count", 7)
span.Attributes().PutInt("messaging.payload_size_bytes", 2048)
span.Attributes().PutInt("messaging.payload_compressed_size_bytes", 1024)
span.Attributes().PutStr("messaging.conversation_id", "MyConversationId")
span.Attributes().PutStr("messaging.id", "452a7c7c7c7048c2f887f61572b18fc2")
var slice = span.Attributes().PutEmptySlice("messaging.sliceData")
slice.AppendEmpty().SetStr("alpha")
slice.AppendEmpty().SetStr("beta")

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

assert.Equal(t, 8, len(segment.Messaging))
assert.Equal(t, "process", segment.Messaging["operation"])
assert.Equal(t, "AmazonSQS", segment.Messaging["system"])
assert.Equal(t, int64(7), segment.Messaging["message_count"])
assert.Equal(t, int64(2048), segment.Messaging["payload_size_bytes"])
assert.Equal(t, int64(1024), segment.Messaging["payload_compressed_size_bytes"])
assert.Equal(t, "MyConversationId", segment.Messaging["conversation_id"])
assert.Equal(t, "452a7c7c7c7048c2f887f61572b18fc2", segment.Messaging["id"])
assert.Equal(t, "alpha", segment.Messaging["sliceData"].([]interface{})[0])
assert.Equal(t, "beta", segment.Messaging["sliceData"].([]interface{})[1])

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

assert.True(t, strings.Contains(jsonStr, "messaging"))

assert.True(t, strings.Contains(jsonStr, "operation"))
assert.True(t, strings.Contains(jsonStr, "process"))

assert.True(t, strings.Contains(jsonStr, "system"))
assert.True(t, strings.Contains(jsonStr, "AmazonSQS"))

assert.True(t, strings.Contains(jsonStr, "message_count"))
assert.True(t, strings.Contains(jsonStr, "7"))

assert.True(t, strings.Contains(jsonStr, "payload_size_bytes"))
assert.True(t, strings.Contains(jsonStr, "2048"))

assert.True(t, strings.Contains(jsonStr, "payload_compressed_size_bytes"))
assert.True(t, strings.Contains(jsonStr, "1024"))

assert.True(t, strings.Contains(jsonStr, "conversation_id"))
assert.True(t, strings.Contains(jsonStr, "MyConversationId"))

assert.True(t, strings.Contains(jsonStr, "id"))
assert.True(t, strings.Contains(jsonStr, "452a7c7c7c7048c2f887f61572b18fc2"))

assert.True(t, strings.Contains(jsonStr, "sliceData"))
assert.True(t, strings.Contains(jsonStr, "alpha"))
assert.True(t, strings.Contains(jsonStr, "beta"))
}

func TestMessagingWithIndexedAttributes(t *testing.T) {
spanName := "ProcessingMessage"
parentSpanID := newSegmentID()
attributes := make(map[string]interface{})
resource := constructDefaultResource()
span := constructServerSpan(parentSpanID, spanName, ptrace.StatusCodeOk, "OK", attributes)

span.Attributes().PutStr("messaging.operation", "process")
span.Attributes().PutStr("messaging.system", "AmazonSQS")

segment, _ := MakeSegment(span, resource, []string{"messaging.system"}, false, nil)

assert.Equal(t, 2, len(segment.Messaging))
assert.Equal(t, "process", segment.Messaging["operation"])
assert.Equal(t, "AmazonSQS", segment.Messaging["system"])
assert.Equal(t, 1, len(segment.Annotations))
assert.Equal(t, "AmazonSQS", segment.Annotations["messaging_system"])

jsonStr, _ := MakeSegmentDocumentString(span, resource, []string{"messaging.system"}, false, nil)

assert.True(t, strings.Contains(jsonStr, "messaging"))

assert.True(t, strings.Contains(jsonStr, "operation"))
assert.True(t, strings.Contains(jsonStr, "process"))

assert.True(t, strings.Contains(jsonStr, "system"))
assert.True(t, strings.Contains(jsonStr, "AmazonSQS"))
assert.True(t, strings.Contains(jsonStr, "annotations"))
assert.True(t, strings.Contains(jsonStr, "messaging_system"))
}
10 changes: 9 additions & 1 deletion exporter/awsxrayexporter/internal/translator/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,18 @@ func MakeSegment(span ptrace.Span, resource pcommon.Resource, indexedAttrs []str
awsfiltered, aws = makeAws(causefiltered, resource, logGroupNames)
service = makeService(resource)
sqlfiltered, sql = makeSQL(span, awsfiltered)
additionalAttrs = addSpecialAttributes(sqlfiltered, indexedAttrs, attributes)
messagingFiltered, messaging = makeMessaging(sqlfiltered)
additionalAttrs = addSpecialAttributes(messagingFiltered, indexedAttrs, attributes)
user, annotations, metadata = makeXRayAttributes(additionalAttrs, resource, storeResource, indexedAttrs, indexAllAttrs)
spanLinks, makeSpanLinkErr = makeSpanLinks(span.Links())
name string
namespace string
)

if makeSpanLinkErr != nil {
return nil, makeSpanLinkErr
}

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

// support x-ray specific service name attributes as segment name if it exists
Expand Down Expand Up @@ -221,6 +227,8 @@ func MakeSegment(span ptrace.Span, resource pcommon.Resource, indexedAttrs []str
Annotations: annotations,
Metadata: metadata,
Type: awsxray.String(segmentType),
Links: spanLinks,
Messaging: messaging,
}, nil
}

Expand Down
54 changes: 54 additions & 0 deletions exporter/awsxrayexporter/internal/translator/spanLinks.go
atshaw43 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package translator // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsxrayexporter/internal/translator"

import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"

awsxray "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray"
)

func makeSpanLinks(links ptrace.SpanLinkSlice) ([]awsxray.SpanLinkData, error) {
var spanLinkDataArray []awsxray.SpanLinkData

for i := 0; i < links.Len(); i++ {
var spanLinkData awsxray.SpanLinkData
var link = links.At(i)

var spanID = link.SpanID().String()
traceID, err := convertToAmazonTraceID(link.TraceID())

if err != nil {
return nil, err
}

spanLinkData.SpanID = &spanID
spanLinkData.TraceID = &traceID

if link.Attributes().Len() > 0 {
spanLinkData.Attributes = make(map[string]interface{})

link.Attributes().Range(func(k string, v pcommon.Value) bool {
spanLinkData.Attributes[k] = v.AsRaw()
return true
})
}

spanLinkDataArray = append(spanLinkDataArray, spanLinkData)
}

return spanLinkDataArray, nil
}
Loading