Skip to content

Commit

Permalink
Address Feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Mahad Zaryab <[email protected]>
  • Loading branch information
mahadzaryab1 committed Dec 16, 2024
1 parent 7bfd22f commit 107738a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
22 changes: 9 additions & 13 deletions cmd/query/app/querysvc/adjuster/resourceattributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ func TestResourceAttributesAdjuster_SpanWithLibraryAttributes(t *testing.T) {
span.Attributes().PutStr("another_key", "another_value")

adjuster := ResourceAttributes()
err := adjuster.Adjust(traces)
require.NoError(t, err)
require.NoError(t, adjuster.Adjust(traces))

resultSpanAttributes := traces.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes()
require.Equal(t, 2, resultSpanAttributes.Len())
Expand Down Expand Up @@ -69,8 +68,7 @@ func TestResourceAttributesAdjuster_SpanWithoutLibraryAttributes(t *testing.T) {
span.Attributes().PutStr("random_key", "random_value")

adjuster := ResourceAttributes()
err := adjuster.Adjust(traces)
require.NoError(t, err)
require.NoError(t, adjuster.Adjust(traces))

resultSpanAttributes := traces.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes()
require.Equal(t, 1, resultSpanAttributes.Len())
Expand All @@ -88,10 +86,10 @@ func TestResourceAttributesAdjuster_SpanWithConflictingLibraryAttributes(t *test
span.Attributes().PutStr(string(otelsemconv.TelemetrySDKLanguageKey), "Java")

adjuster := ResourceAttributes()
err := adjuster.Adjust(traces)
require.NoError(t, err)
require.NoError(t, adjuster.Adjust(traces))

resultSpanAttributes := traces.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes()
resultSpan := traces.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0)
resultSpanAttributes := resultSpan.Attributes()
require.Equal(t, 3, resultSpanAttributes.Len())
val, ok := resultSpanAttributes.Get("random_key")
require.True(t, ok)
Expand All @@ -102,11 +100,10 @@ func TestResourceAttributesAdjuster_SpanWithConflictingLibraryAttributes(t *test
require.True(t, ok)
require.Equal(t, "Java", val.Str())

val, ok = resultSpanAttributes.Get(jptrace.WarningsAttribute)
warnings := jptrace.GetWarnings(resultSpan)
require.True(t, ok)
warnings := val.Slice()
require.Equal(t, 1, warnings.Len())
require.Equal(t, "conflicting values between Span and Resource for attribute telemetry.sdk.language", warnings.At(0).Str())
require.Len(t, warnings, 1)
require.Equal(t, "conflicting values between Span and Resource for attribute telemetry.sdk.language", warnings[0])

resultResourceAttributes := traces.ResourceSpans().At(0).Resource().Attributes()
val, ok = resultResourceAttributes.Get(string(otelsemconv.TelemetrySDKLanguageKey))
Expand All @@ -123,8 +120,7 @@ func TestResourceAttributesAdjuster_SpanWithNonConflictingLibraryAttributes(t *t
span.Attributes().PutStr(string(otelsemconv.TelemetrySDKLanguageKey), "Go")

adjuster := ResourceAttributes()
err := adjuster.Adjust(traces)
require.NoError(t, err)
require.NoError(t, adjuster.Adjust(traces))

resultSpanAttributes := traces.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes()
require.Equal(t, 1, resultSpanAttributes.Len())
Expand Down
18 changes: 15 additions & 3 deletions internal/jptrace/warning.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,27 @@ const (
// store various warnings produced from transformations,
// such as inbound sanitizers and outbound adjusters.
// The value type of the attribute is a string slice.
WarningsAttribute = "jaeger.internal.warnings"
warningsAttribute = "jaeger.internal.warnings"
)

func AddWarning(span ptrace.Span, warning string) {
var warnings pcommon.Slice
if currWarnings, ok := span.Attributes().Get(WarningsAttribute); ok {
if currWarnings, ok := span.Attributes().Get(warningsAttribute); ok {
warnings = currWarnings.Slice()
} else {
warnings = span.Attributes().PutEmptySlice(WarningsAttribute)
warnings = span.Attributes().PutEmptySlice(warningsAttribute)
}
warnings.AppendEmpty().SetStr(warning)
}

func GetWarnings(span ptrace.Span) []string {
if w, ok := span.Attributes().Get(warningsAttribute); ok {
warnings := []string{}
ws := w.Slice()
for i := 0; i < ws.Len(); i++ {
warnings = append(warnings, ws.At(i).Str())
}
return warnings
}
return nil
}
37 changes: 37 additions & 0 deletions internal/jptrace/warning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,40 @@ func TestAddWarning(t *testing.T) {
})
}
}
func TestGetWarnings(t *testing.T) {
tests := []struct {
name string
existing []string
expected []string
}{
{
name: "get from nil warnings",
existing: nil,
expected: nil,
},
{
name: "get from empty warnings",
existing: []string{},
expected: []string{},
},
{
name: "get from existing warnings",
existing: []string{"existing warning 1", "existing warning 2"},
expected: []string{"existing warning 1", "existing warning 2"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
span := ptrace.NewSpan()
attrs := span.Attributes()
if test.existing != nil {
warnings := attrs.PutEmptySlice("jaeger.internal.warnings")
for _, warn := range test.existing {
warnings.AppendEmpty().SetStr(warn)
}
}
actual := GetWarnings(span)
assert.Equal(t, test.expected, actual)
})
}
}

0 comments on commit 107738a

Please sign in to comment.