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

Add error attribute to metrics labels #440

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jedrivisser
Copy link

This PR allows you to add specific information about an error to a metric when it occurs. For instance the mysql error code.

An example implementation of the ErrorAttributesGetter function for mysql:

// For information on MySQL errors and mappings, see https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html
func MySQLErrorAttributesGetter(err error) []attribute.KeyValue {
	var mysqlError *mysql.MySQLError
	if errors.As(err, &mysqlError) {
		return []attribute.KeyValue{
			attribute.String("error_type", "mysql"),
			attribute.String("error_number", strconv.FormatUint(uint64(mysqlError.Number), 10)),
		}
	}

	return nil
}

This would be registered with:

driverName, err := otelsql.Register("mysql",otelsql.WithErrorAttributesGetter(MySQLErrorAttributesGetter))

It creates a metric that looks something like:

db_sql_latency_milliseconds_bucket{error_number="1062",error_type="mysql",method="sql.stmt.exec",otel_scope_name="github.com/XSAM/otelsql",otel_scope_version="0.36.0",status="error",le="100"} 1

instead of:

db_sql_latency_milliseconds_bucket{method="sql.stmt.exec",otel_scope_name="github.com/XSAM/otelsql",otel_scope_version="0.36.0",status="error",le="100"} 1

This allows us to use the error number to decide if an error is a critical error or one that is already being managed in code.

@jedrivisser jedrivisser requested a review from XSAM as a code owner March 19, 2025 12:40
Copy link

codecov bot commented Mar 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.2%. Comparing base (4fe3c27) to head (61a22fd).

Additional details and impacted files
@@           Coverage Diff           @@
##            main    #440     +/-   ##
=======================================
+ Coverage   84.0%   84.2%   +0.1%     
=======================================
  Files         14      14             
  Lines        798     805      +7     
=======================================
+ Hits         671     678      +7     
  Misses       103     103             
  Partials      24      24             

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jedrivisser jedrivisser force-pushed the error-attributes branch 2 times, most recently from e264d9c to ccf1012 Compare March 19, 2025 14:51
@jedrivisser
Copy link
Author

jedrivisser commented Mar 19, 2025

Added some more unit tests

Copy link
Owner

@XSAM XSAM left a comment

Choose a reason for hiding this comment

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

@jedrivisser, thanks for creating this PR.

This feature seems reasonable, but some comments need to be resolved before merging.

Please also update the CHANGELOG.me file.

@jedrivisser jedrivisser force-pushed the error-attributes branch 2 times, most recently from a941d74 to 4953d69 Compare March 24, 2025 14:06
@jedrivisser
Copy link
Author

Thanks for the review! I think I have addressed all issues. Please let me know if I missed or misunderstood anything

This allows you to add specific information about an error to a metric when it occurs. For instance the error code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants