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

Update tracing check for whether error has stack #4982

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 14 additions & 13 deletions util/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,23 @@ func Helper() {
func Traces(err error) []*Stack {
var st []*Stack

wrapped, ok := err.(interface {
Unwrap() error
})
if ok {
st = Traces(wrapped.Unwrap())
switch e := err.(type) {
case interface{ Unwrap() error }:
st = Traces(e.Unwrap())
case interface{ Unwrap() []error }:
for _, ue := range e.Unwrap() {
st = Traces(ue)
// Only take first stack
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this make sense? For nested errors it is expected that there are multiple stacktraces and they are all merged into array here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My worry is that merging the arrays in this case would lead to a stack which doesn't make sense. Essentially this would be the case where you had multiple calls to errors.Wrap from either a loop or multiple goroutines running in parallel, then combined using errors.Join. The stacks would not be part of a single stack. I think this case would be rare and the case where that is used, you could have multiple of the same stack from parallel processes running into the same error case.

If we wanted to support that, then [][]*Stack as the return would make more sense. It doesn't seem like a very relevant edge case to me though.

if len(st) > 0 {
break
}
}
}

if ste, ok := err.(interface {
StackTrace() errors.StackTrace
}); ok {
switch ste := err.(type) {
case interface{ StackTrace() errors.StackTrace }:
st = append(st, convertStack(ste.StackTrace()))
}

if ste, ok := err.(interface {
StackTrace() *Stack
}); ok {
case interface{ StackTrace() *Stack }:
st = append(st, ste.StackTrace())
}

Expand Down
30 changes: 25 additions & 5 deletions util/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"net/http/httptrace"

"github.com/moby/buildkit/util/bklog"
"go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute"
Expand All @@ -15,6 +14,11 @@ import (
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"

"github.com/pkg/errors"

"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/stack"
)

// StartSpan starts a new span as a child of the span in context.
Expand All @@ -30,14 +34,30 @@ func StartSpan(ctx context.Context, operationName string, opts ...trace.SpanStar
return span, ctx
}

func hasStacktrace(err error) bool {
switch e := err.(type) {
case interface{ StackTrace() *stack.Stack }:
return true
case interface{ StackTrace() errors.StackTrace }:
return true
case interface{ Unwrap() error }:
return hasStacktrace(e.Unwrap())
case interface{ Unwrap() []error }:
for _, ue := range e.Unwrap() {
if hasStacktrace(ue) {
return true
}
}
}
return false
}

// FinishWithError finalizes the span and sets the error if one is passed
func FinishWithError(span trace.Span, err error) {
if err != nil {
span.RecordError(err)
if _, ok := err.(interface {
Cause() error
}); ok {
span.SetAttributes(attribute.String(string(semconv.ExceptionStacktraceKey), fmt.Sprintf("%+v", err)))
if hasStacktrace(err) {
span.SetAttributes(attribute.String(string(semconv.ExceptionStacktraceKey), fmt.Sprintf("%+v", stack.Formatter(err))))
}
span.SetStatus(codes.Error, err.Error())
}
Expand Down
Loading