Skip to content

Conversation

@prestonvasquez
Copy link
Member

@prestonvasquez prestonvasquez commented Nov 18, 2025

GODRIVER-3690

Summary

Add an ErrorCodes function that can be used to parse server error codes from an arbitrary Go error.

Usage:

func publishMetrics(ctx context.Context, evt *event.CommandFailedEvent) {
	attrs := []attribute.KeyValue{}

	if errCodes := mongo.ErrorCodes(evt.Failure); len(errCodes) > 0 {
		// Record the first error code as a status code attribute.
		attrs = append(attrs, semconv.DBResponseStatusCode(errCodes[0]))
	}

	Record(
		ctx,
		evt.Duration.Seconds(),
		dbconv.SystemNameMongoDB,
		attrs...,
	)
}

Background

Would be convenient to get server status codes without caller taking a dependency on the experimental API. For example, OpenTelemetry needs to include db.response.status.code for otelmongo: open-telemetry/opentelemetry-go-contrib#7983 (comment)

An alternative to this design would be to put Codes on event.CommandFailedEvent. Suggest we forgo this solution for two reasons:

  1. It breaks unkeyed composite literals for event.CommandFailedEvent
  2. There may be non-event cases where users would want to validate for error codes.

Copilot AI review requested due to automatic review settings November 18, 2025 01:13
@prestonvasquez prestonvasquez requested a review from a team as a code owner November 18, 2025 01:13
@prestonvasquez prestonvasquez marked this pull request as draft November 18, 2025 01:13
@prestonvasquez prestonvasquez removed the request for review from qingyang-hu November 18, 2025 01:13
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR extends the CommandFailedEvent structure to include error codes, addressing GODRIVER-3690. The change enables users to access error codes directly from command failure events without parsing the error themselves.

Key changes:

  • Added a Codes field to CommandFailedEvent to expose error codes
  • Implemented errorCodes() helper function to extract codes from driver errors
  • Added test coverage for the new field

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
event/monitoring.go Added Codes field to CommandFailedEvent structure
x/mongo/driver/operation.go Populates the new Codes field when publishing failed events; removed blank line
x/mongo/driver/errors.go Implemented errorCodes() function to extract error codes from driver errors
mongo/errors.go Refactored variable declarations to use var() block (style change)
internal/integration/clam_prose_test.go Added test case for CommandFailedEvent.Codes; removed blank line

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mongodb-drivers-pr-bot
Copy link
Contributor

mongodb-drivers-pr-bot bot commented Nov 18, 2025

🧪 Performance Results

Commit SHA: 03fda7a

The following benchmark tests for version 69335dbe9bbda10007c74eb0 had statistically significant changes (i.e., |z-score| > 1.96):

Benchmark Measurement % Change Patch Value Stable Region H-Score Z-Score
BenchmarkSingleRunCommand ns_per_op 15.1227 158166.0000 Avg: 137389.0303
Med: 134746.0000
Stdev: 8166.0827
0.7984 2.5443
BenchmarkSingleRunCommand ops_per_second_med -12.9561 6691.0444 Avg: 7686.9751
Med: 7819.6171
Stdev: 390.8930
0.7929 -2.5478
BenchmarkSingleFindOneByID ns_per_op 11.0930 274874.0000 Avg: 247426.8485
Med: 245742.0000
Stdev: 10140.0996
0.8010 2.7068
BenchmarkSingleFindOneByID total_mem_allocs -9.8037 1495287.0000 Avg: 1657814.7500
Med: 1669065.0000
Stdev: 81358.2818
0.7376 -1.9977
BenchmarkSingleFindOneByID ops_per_second_med -9.5669 3756.2777 Avg: 4153.6555
Med: 4188.7448
Stdev: 166.7453
0.7748 -2.3831
BenchmarkSingleFindOneByID ops_per_second_max -7.9284 4349.0930 Avg: 4723.6006
Med: 4720.1469
Stdev: 154.9727
0.7769 -2.4166
BenchmarkSmallDocInsertOne ops_per_second_max -7.6144 5747.9193 Avg: 6221.6591
Med: 6255.1605
Stdev: 235.0399
0.7324 -2.0156
BenchmarkBSONFullDocumentDecoding total_mem_allocs -4.3462 9423022.0000 Avg: 9851175.1200
Med: 9862458.0000
Stdev: 202458.9840
0.7437 -2.1148
BenchmarkBSONFullDocumentDecoding total_bytes_allocated -4.3459 389863000.0000 Avg: 407575990.7200
Med: 408022784.0000
Stdev: 8407879.2257
0.7428 -2.1067
BenchmarkBSONDeepDocumentDecoding total_time_seconds 1.5220 1.2174 Avg: 1.1992
Med: 1.1988
Stdev: 0.0057
0.8455 3.2080
BenchmarkBSONFlatDocumentDecoding total_time_seconds 1.1055 1.2127 Avg: 1.1995
Med: 1.1994
Stdev: 0.0066
0.7400 1.9947

For a comprehensive view of all microbenchmark results for this PR's commit, please check out the Evergreen perf task for this patch.

@mongodb-drivers-pr-bot
Copy link
Contributor

mongodb-drivers-pr-bot bot commented Nov 18, 2025

API Change Report

./v2/mongo

compatible changes

ErrorCodes: added

@prestonvasquez prestonvasquez force-pushed the feature/godriver-3690-extend-event-failures-with-error-codes branch 2 times, most recently from 3b7f589 to 63d4781 Compare December 3, 2025 00:20
@prestonvasquez prestonvasquez force-pushed the feature/godriver-3690-extend-event-failures-with-error-codes branch from 63d4781 to 58198cf Compare December 3, 2025 00:23
@prestonvasquez prestonvasquez added review-priority-normal Medium Priority PR for Review: within 1 business day feature labels Dec 3, 2025
@prestonvasquez prestonvasquez changed the title (POC) GODRIVER-3690 Extend CommandFailedEvent with error codes GODRIVER-3690 Add ErrorCodesFrom to the mongo package Dec 3, 2025
@prestonvasquez prestonvasquez marked this pull request as ready for review December 3, 2025 00:41
RafaelCenzano
RafaelCenzano previously approved these changes Dec 4, 2025
Copy link

@RafaelCenzano RafaelCenzano left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +913 to +915
var ec interface{ ErrorCodes() []int }
if errors.As(wrapErrors(err), &ec) {
return ec.ErrorCodes()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can users use errors.As to convert the error to a mongo.ServerError and get the same information?

E.g.

var se mongo.ServerError
if errors.As(err, &se) {
    se.ErrorCodes()
}

Copy link
Member Author

@prestonvasquez prestonvasquez Dec 5, 2025

Choose a reason for hiding this comment

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

This would only work if mongo.ServerError is in the error tree for the arbitrary error which isn't the case for errors propagated by event monitoring. Here's an example: https://github.com/prestonvasquez/go-playground/blob/01e0e77437bdd9ba0a23d912121e152880dc19e9/mgd_errors_test.go#L17

mongo/errors.go Outdated
}

// ErrorCodesFrom returns the list of server error codes contained in err.
func ErrorCodesFrom(err error) []int {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Optional: Consider naming this ErrorCodes, which matches the method name on the error types.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea 👍

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

Labels

feature review-priority-normal Medium Priority PR for Review: within 1 business day

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants