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

feat: tracing add custom report err func #3370

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

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
@@ -15,10 +15,14 @@ import (
// Option is tracing option.
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.

已修改

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
@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

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.

3 participants