Skip to content

Add error attribute to metrics labels - #440

Merged
XSAM merged 1 commit into
XSAM:mainfrom
diebietse:error-attributes
Mar 25, 2025
Merged

XSAM merged 1 commit into
XSAM:mainfrom
diebietse:error-attributes

Conversation

@jedrivisser

Copy link
Copy Markdown
Contributor

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

codecov Bot commented Mar 19, 2025 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.2%. Comparing base (4fe3c27) to head (61a22fd).
Report is 1 commits behind head on main.

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

jedrivisser commented Mar 19, 2025 •

Copy link
Copy Markdown
Contributor Author

Added some more unit tests

@XSAM XSAM left a comment

Copy link
Copy Markdown
Owner

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.

Comment thread config.go Resolved
Comment thread config.go Resolved
Comment thread option_test.go Resolved
Comment thread utils_test.go Resolved
Comment thread utils_test.go Resolved
Comment thread utils_test.go Resolved
@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
Copy Markdown
Contributor 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

@XSAM XSAM left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

LGTM. Thanks for your contribution!

@XSAM
XSAM merged commit 74d2073 into XSAM:main Mar 25, 2025
@jedrivisser

Copy link
Copy Markdown
Contributor Author

Awesome, thanks!

@jedrivisser
jedrivisser deleted the error-attributes branch March 25, 2025 07:00
@XSAM XSAM mentioned this pull request Mar 26, 2025
XSAM added a commit that referenced this pull request Mar 26, 2025
### Added

- `WithInstrumentErrorAttributesGetter` option to provide additional
error-related attributes. (#440)

### Changed

- Upgrade OTel to `v1.35.0/v0.57.0`. (#437)

### Removed

- Drop support for Go `1.22`. (#447)
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