Skip to content

Commit

Permalink
Fix XRay segment bug. the upstream PR - open-telemetry#24381
Browse files Browse the repository at this point in the history
  • Loading branch information
mxiamxia committed Jul 21, 2023
1 parent a0883a0 commit 992600b
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 38 deletions.
36 changes: 28 additions & 8 deletions exporter/awsxrayexporter/internal/translator/cause.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func makeCause(span ptrace.Span, attributes map[string]pcommon.Value, resource p
if val, ok := resource.Attributes().Get(conventions.AttributeTelemetrySDKLanguage); ok {
language = val.Str()
}
isRemote := false
if span.Kind() == ptrace.SpanKindClient || span.Kind() == ptrace.SpanKindProducer {
isRemote = true
}

var exceptions []awsxray.Exception
for i := 0; i < span.Events().Len(); i++ {
Expand All @@ -70,7 +74,7 @@ func makeCause(span ptrace.Span, attributes map[string]pcommon.Value, resource p
stacktrace = val.Str()
}

parsed := parseException(exceptionType, message, stacktrace, language)
parsed := parseException(exceptionType, message, stacktrace, isRemote, language)
exceptions = append(exceptions, parsed...)
}
}
Expand Down Expand Up @@ -117,24 +121,35 @@ func makeCause(span ptrace.Span, attributes map[string]pcommon.Value, resource p
val, ok := span.Attributes().Get(conventions.AttributeHTTPStatusCode)

switch {
case status.Code() != ptrace.StatusCodeError:
isError = false
isThrottle = false
isFault = false
// The segment status for http spans will be based on their http.statuscode as we found some http
// spans does not fill with status.Code() but always filled with http.statuscode
case ok:
code := val.Int()
// We only differentiate between faults (server errors) and errors (client errors) for HTTP spans.
if code >= 400 && code <= 499 {
switch {
case code >= 400 && code <= 499:
isError = true
isFault = false
if code == 429 {
isThrottle = true
}
} else {
case code >= 500 && code <= 599:
isError = false
isThrottle = false
isFault = true
case status.Code() == ptrace.StatusCodeError:
isError = false
isThrottle = false
isFault = true
default:
isError = false
isThrottle = false
isFault = false
}
case status.Code() != ptrace.StatusCodeError:
isError = false
isThrottle = false
isFault = false
default:
isError = false
isThrottle = false
Expand All @@ -144,12 +159,13 @@ func makeCause(span ptrace.Span, attributes map[string]pcommon.Value, resource p
return isError, isFault, isThrottle, filtered, cause
}

func parseException(exceptionType string, message string, stacktrace string, language string) []awsxray.Exception {
func parseException(exceptionType string, message string, stacktrace string, isRemote bool, language string) []awsxray.Exception {
exceptions := make([]awsxray.Exception, 0, 1)
segmentID := newSegmentID()
exceptions = append(exceptions, awsxray.Exception{
ID: aws.String(hex.EncodeToString(segmentID[:])),
Type: aws.String(exceptionType),
Remote: aws.Bool(isRemote),
Message: aws.String(message),
})

Expand Down Expand Up @@ -181,6 +197,7 @@ func fillJavaStacktrace(stacktrace string, exceptions []awsxray.Exception) []aws

// Skip first line containing top level message
exception := &exceptions[0]
isRemote := exception.Remote
_, err := r.ReadLine()
if err != nil {
return exceptions
Expand Down Expand Up @@ -248,6 +265,7 @@ func fillJavaStacktrace(stacktrace string, exceptions []awsxray.Exception) []aws
exceptions = append(exceptions, awsxray.Exception{
ID: aws.String(hex.EncodeToString(segmentID[:])),
Type: aws.String(causeType),
Remote: isRemote,
Message: aws.String(causeMessage),
Stack: nil,
})
Expand Down Expand Up @@ -287,6 +305,7 @@ func fillPythonStacktrace(stacktrace string, exceptions []awsxray.Exception) []a
}
line := lines[lineIdx]
exception := &exceptions[0]
isRemote := exception.Remote

exception.Stack = nil
for {
Expand Down Expand Up @@ -344,6 +363,7 @@ func fillPythonStacktrace(stacktrace string, exceptions []awsxray.Exception) []a
exceptions = append(exceptions, awsxray.Exception{
ID: aws.String(hex.EncodeToString(segmentID[:])),
Type: aws.String(causeType),
Remote: isRemote,
Message: aws.String(causeMessage),
})
// when append causes `exceptions` to outgrow its existing
Expand Down
Loading

0 comments on commit 992600b

Please sign in to comment.