Skip to content

Conversation

@lawlielt
Copy link
Contributor

Description (what this PR does / why we need it):

By default, when an error occurs, the following action is taken:

span.RecordError(err)
if e := errors.FromError(err); e != nil {
	span.SetAttributes(attribute.Key("rpc.status_code").Int64(int64(e.Code)))
}

The exception.type in RecordError is the type of the error, which doesn't contain much information. To collect more details, we can customize the action and add new attributes.

Here is an example of how to customize error handling and collect more information:

var (
	errCode int32
)

if st, ok := status.FromError(err); ok {
	errCode = int32(st.Code())
} else {
	errCode = 0
}

span.AddEvent("exception", otelTrace.WithAttributes(
	attribute.Key("exception.type").String(fmt.Sprintf("%T", err)),
	attribute.Key("exception.message").String(err.Error()),
))
span.SetAttributes(attribute.Key("rpc.grpc.status_code").Int64(int64(errCode)))

By doing this, we can collect and record more detailed error information, which helps in better monitoring and debugging of the application.

Which issue(s) this PR fixes (resolves / be part of):

Other special notes for the reviewers:

the default grpc error code in opentelmetry library is rpc.grpc.status_code not rpc.status_code, we can use the custom method to compatible that

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 15, 2024
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Jul 16, 2024
type Option func(*options)

// ReportErrHandle report err func handler
type ReportErrHandle func(ctx context.Context, span trace.Span, err error)
Copy link
Member

Choose a reason for hiding this comment

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

ReportErrHandle -> ReportErrorHandle

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

shenqidebaozi
shenqidebaozi previously approved these changes Jul 16, 2024
Copy link
Member

@shenqidebaozi shenqidebaozi left a comment

Choose a reason for hiding this comment

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

LGTM

@dosubot dosubot bot added the LGTM label Jul 16, 2024
@shenqidebaozi shenqidebaozi enabled auto-merge (squash) July 16, 2024 10:22
daemon365
daemon365 previously approved these changes Jul 25, 2024
@shenqidebaozi shenqidebaozi marked this pull request as draft July 25, 2024 02:26
auto-merge was automatically disabled July 25, 2024 02:26

Pull request was converted to draft

@lawlielt lawlielt dismissed stale reviews from shenqidebaozi and daemon365 via aed7a79 September 4, 2025 07:40
@lawlielt lawlielt marked this pull request as ready for review September 4, 2025 07:46
@kagaya85
Copy link
Member

@lawlielt I think errorReporter might be a better name? Like the tracerProvider and propagator fields in the struct

}

// WithReportErrorHandle with custom report err func
func WithReportErrorHandle(f ReportErrorHandle) Option {
Copy link
Member

Choose a reason for hiding this comment

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

WithErrorHandler

type Option func(*options)

// ReportErrorHandle report err func handler
type ReportErrorHandle func(ctx context.Context, span trace.Span, err error)
Copy link
Member

Choose a reason for hiding this comment

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

ErrorHandler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants