Skip to content

Commit

Permalink
[exporter/awsxray] Adding Log Group Names to X-Ray Exporter Config (#…
Browse files Browse the repository at this point in the history
…16859)

* Adding Log Groups from Config

* Fixing Name

* Adding to README.md

* Adding change log

* Golang style check fix

* Fixing linter

* Running go mod tidy

* Updating go.mod

* Revert "Updating go.mod"

This reverts commit 3b5c38a.

* Reverting signalreceiver

* Reverting tools update

* Fix imports

* Style check

* Removing whitespace

* Ran goimports

* Ran goimports again

* Testing
  • Loading branch information
atshaw43 authored Dec 28, 2022
1 parent dec0e47 commit 3b25b98
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 65 deletions.
16 changes: 16 additions & 0 deletions .chloggen/xray_add_loggroupnames.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: xrayexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add Cloud Watch log group names from xray exporter config

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

# (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:
3 changes: 2 additions & 1 deletion exporter/awsxrayexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ comparable AWS X-Ray Daemon configuration values.
| `role_arn` | IAM role to upload segments to a different account. | |
| `indexed_attributes` | List of attribute names to be converted to X-Ray annotations. | |
| `index_all_attributes` | Enable or disable conversion of all OpenTelemetry attributes to X-Ray annotations. | false |
| `aws_log_groups` | List of log group names for cloud watch. | |

## AWS Credential Configuration

Expand All @@ -75,4 +76,4 @@ credential configuration.

[beta]:https://github.com/open-telemetry/opentelemetry-collector#beta
[contrib]:https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib
[AWS]:https://aws-otel.github.io/docs/getting-started/x-ray#configuring-the-aws-x-ray-exporter
[AWS]:https://aws-otel.github.io/docs/getting-started/x-ray#configuring-the-aws-x-ray-exporter
8 changes: 6 additions & 2 deletions exporter/awsxrayexporter/awsxray.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,18 @@ func newTracesExporter(

func extractResourceSpans(config component.Config, logger *zap.Logger, td ptrace.Traces) []*string {
documents := make([]*string, 0, td.SpanCount())

for i := 0; i < td.ResourceSpans().Len(); i++ {
rspans := td.ResourceSpans().At(i)
resource := rspans.Resource()
for j := 0; j < rspans.ScopeSpans().Len(); j++ {
spans := rspans.ScopeSpans().At(j).Spans()
for k := 0; k < spans.Len(); k++ {
document, localErr := translator.MakeSegmentDocumentString(spans.At(k), resource,
config.(*Config).IndexedAttributes, config.(*Config).IndexAllAttributes)
document, localErr := translator.MakeSegmentDocumentString(
spans.At(k), resource,
config.(*Config).IndexedAttributes,
config.(*Config).IndexAllAttributes,
config.(*Config).LogGroupNames)
if localErr != nil {
logger.Debug("Error translating span.", zap.Error(localErr))
continue
Expand Down
2 changes: 2 additions & 0 deletions exporter/awsxrayexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ type Config struct {
// Set to true to convert all OpenTelemetry attributes to X-Ray annotation (indexed) ignoring the IndexedAttributes option.
// Default value: false
IndexAllAttributes bool `mapstructure:"index_all_attributes"`

LogGroupNames []string `mapstructure:"aws_log_groups"`
}
1 change: 1 addition & 0 deletions exporter/awsxrayexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestLoadConfig(t *testing.T) {
},
IndexedAttributes: []string{"indexed_attr_0", "indexed_attr_1"},
IndexAllAttributes: false,
LogGroupNames: []string{"group1", "group2"},
},
},
}
Expand Down
19 changes: 15 additions & 4 deletions exporter/awsxrayexporter/internal/translator/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
awsxray "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray"
)

func makeAws(attributes map[string]pcommon.Value, resource pcommon.Resource) (map[string]pcommon.Value, *awsxray.AWSData) {
func makeAws(attributes map[string]pcommon.Value, resource pcommon.Resource, logGroupNames []string) (map[string]pcommon.Value, *awsxray.AWSData) {
var (
cloud string
service string
Expand Down Expand Up @@ -219,11 +219,22 @@ func makeAws(attributes map[string]pcommon.Value, resource pcommon.Resource) (ma
}

// Since we must couple log group ARNs and Log Group Names in the same CWLogs object, we first try to derive the
// names from the ARN, then fall back to just recording the names
if logGroupArns != (pcommon.Slice{}) && logGroupArns.Len() > 0 {
// names from the ARN, then fall back to recording the names, if they do not exist in the resource
// then pull from them from config.
switch {
case logGroupArns != (pcommon.Slice{}) && logGroupArns.Len() > 0:
cwl = getLogGroupMetadata(logGroupArns, true)
} else if logGroups != (pcommon.Slice{}) && logGroups.Len() > 0 {
case logGroups != (pcommon.Slice{}) && logGroups.Len() > 0:
cwl = getLogGroupMetadata(logGroups, false)
case logGroupNames != nil:
var configSlice = pcommon.NewSlice()
configSlice.EnsureCapacity(len(logGroupNames))

for _, s := range logGroupNames {
configSlice.AppendEmpty().SetStr(s)
}

cwl = getLogGroupMetadata(configSlice, false)
}

if sdkName != "" && sdkLanguage != "" {
Expand Down
56 changes: 38 additions & 18 deletions exporter/awsxrayexporter/internal/translator/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestAwsFromEc2Resource(t *testing.T) {

attributes := make(map[string]pcommon.Value)

filtered, awsData := makeAws(attributes, resource)
filtered, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestAwsFromEcsResource(t *testing.T) {

attributes := make(map[string]pcommon.Value)

filtered, awsData := makeAws(attributes, resource)
filtered, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestAwsFromBeanstalkResource(t *testing.T) {

attributes := make(map[string]pcommon.Value)

filtered, awsData := makeAws(attributes, resource)
filtered, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestAwsFromEksResource(t *testing.T) {

attributes := make(map[string]pcommon.Value)

filtered, awsData := makeAws(attributes, resource)
filtered, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestAwsWithAwsSqsResources(t *testing.T) {
attributes[awsxray.AWSQueueURLAttribute] = pcommon.NewValueStr(queueURL)
attributes["employee.id"] = pcommon.NewValueStr("XB477")

filtered, awsData := makeAws(attributes, resource)
filtered, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand All @@ -223,7 +223,7 @@ func TestAwsWithRpcAttributes(t *testing.T) {
attributes := make(map[string]pcommon.Value)
attributes[conventions.AttributeRPCMethod] = pcommon.NewValueStr("ListBuckets")

_, awsData := makeAws(attributes, resource)
_, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, awsData)
assert.Equal(t, "ListBuckets", *awsData.Operation)
Expand All @@ -234,7 +234,7 @@ func TestAwsWithSqsAlternateAttribute(t *testing.T) {
attributes := make(map[string]pcommon.Value)
attributes[awsxray.AWSQueueURLAttribute2] = pcommon.NewValueStr(queueURL)

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

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand All @@ -246,7 +246,7 @@ func TestAwsWithAwsSqsSemConvAttributes(t *testing.T) {
attributes := make(map[string]pcommon.Value)
attributes[conventions.AttributeMessagingURL] = pcommon.NewValueStr(queueURL)

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

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestAwsWithAwsDynamoDbResources(t *testing.T) {
attributes[awsxray.AWSRequestIDAttribute] = pcommon.NewValueStr("75107C82-EC8A-4F75-883F-4440B491B0AB")
attributes[awsxray.AWSTableNameAttribute] = pcommon.NewValueStr(tableName)

filtered, awsData := makeAws(attributes, resource)
filtered, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand All @@ -295,7 +295,7 @@ func TestAwsWithDynamoDbAlternateAttribute(t *testing.T) {
attributes := make(map[string]pcommon.Value)
attributes[awsxray.AWSTableNameAttribute2] = pcommon.NewValueStr(tableName)

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

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand All @@ -307,7 +307,7 @@ func TestAwsWithDynamoDbSemConvAttributes(t *testing.T) {
attributes := make(map[string]pcommon.Value)
attributes[conventions.AttributeAWSDynamoDBTableNames] = pcommon.NewValueStr(tableName)

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

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand All @@ -319,7 +319,7 @@ func TestAwsWithRequestIdAlternateAttribute(t *testing.T) {
attributes := make(map[string]pcommon.Value)
attributes[awsxray.AWSRequestIDAttribute2] = pcommon.NewValueStr(requestid)

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

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand All @@ -333,7 +333,7 @@ func TestJavaSDK(t *testing.T) {
resource.Attributes().PutStr(conventions.AttributeTelemetrySDKLanguage, "java")
resource.Attributes().PutStr(conventions.AttributeTelemetrySDKVersion, "1.2.3")

filtered, awsData := makeAws(attributes, resource)
filtered, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand All @@ -349,7 +349,7 @@ func TestJavaAutoInstrumentation(t *testing.T) {
resource.Attributes().PutStr(conventions.AttributeTelemetrySDKVersion, "1.2.3")
resource.Attributes().PutStr(conventions.AttributeTelemetryAutoVersion, "3.4.5")

filtered, awsData := makeAws(attributes, resource)
filtered, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand All @@ -365,7 +365,7 @@ func TestGoSDK(t *testing.T) {
resource.Attributes().PutStr(conventions.AttributeTelemetrySDKLanguage, "go")
resource.Attributes().PutStr(conventions.AttributeTelemetrySDKVersion, "2.0.3")

filtered, awsData := makeAws(attributes, resource)
filtered, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand All @@ -380,7 +380,7 @@ func TestCustomSDK(t *testing.T) {
resource.Attributes().PutStr(conventions.AttributeTelemetrySDKLanguage, "java")
resource.Attributes().PutStr(conventions.AttributeTelemetrySDKVersion, "2.0.3")

filtered, awsData := makeAws(attributes, resource)
filtered, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand All @@ -403,7 +403,7 @@ func TestLogGroups(t *testing.T) {
ava.AppendEmpty().SetStr("group1")
ava.AppendEmpty().SetStr("group2")

filtered, awsData := makeAws(attributes, resource)
filtered, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand Down Expand Up @@ -431,7 +431,27 @@ func TestLogGroupsFromArns(t *testing.T) {
ava.AppendEmpty().SetStr(group1)
ava.AppendEmpty().SetStr(group2)

filtered, awsData := makeAws(attributes, resource)
filtered, awsData := makeAws(attributes, resource, nil)

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
assert.Equal(t, 2, len(awsData.CWLogs))
assert.Contains(t, awsData.CWLogs, cwl1)
assert.Contains(t, awsData.CWLogs, cwl2)
}

func TestLogGroupsFromConfig(t *testing.T) {
cwl1 := awsxray.LogGroupMetadata{
LogGroup: awsxray.String("logGroup1"),
}
cwl2 := awsxray.LogGroupMetadata{
LogGroup: awsxray.String("logGroup2"),
}

attributes := make(map[string]pcommon.Value)
resource := pcommon.NewResource()

filtered, awsData := makeAws(attributes, resource, []string{"logGroup1", "logGroup2"})

assert.NotNil(t, filtered)
assert.NotNil(t, awsData)
Expand Down
8 changes: 4 additions & 4 deletions exporter/awsxrayexporter/internal/translator/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ var (
)

// MakeSegmentDocumentString converts an OpenTelemetry Span to an X-Ray Segment and then serialzies to JSON
func MakeSegmentDocumentString(span ptrace.Span, resource pcommon.Resource, indexedAttrs []string, indexAllAttrs bool) (string, error) {
segment, err := MakeSegment(span, resource, indexedAttrs, indexAllAttrs)
func MakeSegmentDocumentString(span ptrace.Span, resource pcommon.Resource, indexedAttrs []string, indexAllAttrs bool, logGroupNames []string) (string, error) {
segment, err := MakeSegment(span, resource, indexedAttrs, indexAllAttrs, logGroupNames)
if err != nil {
return "", err
}
Expand All @@ -82,7 +82,7 @@ func MakeSegmentDocumentString(span ptrace.Span, resource pcommon.Resource, inde
}

// MakeSegment converts an OpenTelemetry Span to an X-Ray Segment
func MakeSegment(span ptrace.Span, resource pcommon.Resource, indexedAttrs []string, indexAllAttrs bool) (*awsxray.Segment, error) {
func MakeSegment(span ptrace.Span, resource pcommon.Resource, indexedAttrs []string, indexAllAttrs bool, logGroupNames []string) (*awsxray.Segment, error) {
var segmentType string

storeResource := true
Expand All @@ -105,7 +105,7 @@ func MakeSegment(span ptrace.Span, resource pcommon.Resource, indexedAttrs []str
httpfiltered, http = makeHTTP(span)
isError, isFault, isThrottle, causefiltered, cause = makeCause(span, httpfiltered, resource)
origin = determineAwsOrigin(resource)
awsfiltered, aws = makeAws(causefiltered, resource)
awsfiltered, aws = makeAws(causefiltered, resource, logGroupNames)
service = makeService(resource)
sqlfiltered, sql = makeSQL(span, awsfiltered)
user, annotations, metadata = makeXRayAttributes(sqlfiltered, resource, storeResource, indexedAttrs, indexAllAttrs)
Expand Down
Loading

0 comments on commit 3b25b98

Please sign in to comment.