-
Notifications
You must be signed in to change notification settings - Fork 10
Add Request ID Tracking for Enhanced Debugging and Distributed Tracing #674
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 adds request ID tracking capabilities to the MongoDB Atlas Go SDK to improve debugging and distributed tracing. The implementation uses context propagation to automatically include X-Request-ID headers in API requests and enhance error messages with request context.
Key Changes:
- Added automatic
X-Request-IDheader propagation from Go context values - Enhanced
ApiErrorwithGetContextualErrormethod that includes request IDs in error messages - Introduced
WithRequestIDhelper function for ergonomic context management
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| openapi/atlas-api.yaml | Added x-is-api-error vendor extension to ApiError schema for template generation |
| tools/config/go-templates/model_simple.mustache | Template modified to generate GetContextualError method for API error types |
| tools/config/go-templates/client.mustache | Template modified to extract and propagate request IDs from context to HTTP headers |
| admin/model_api_error.go | Generated code with GetContextualError method implementation |
| admin/client.go | Generated code with request ID header handling and WithRequestID helper |
| admin/model_api_error_test.go | Unit tests for contextual error formatting |
| admin/client_test.go | Unit tests for request ID header propagation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
AgustinBettati
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Drjacky, thank you for the details and proposed changes.
I would like to gain more context on what value we gain from adding a client defined request-id header to Atlas API. We can continue conversation directly in each comment.
| func (a *ApiError) GetContextualError(ctx context.Context) string { | ||
| baseError := a.Error() | ||
|
|
||
| // Check if context has request ID | ||
| if requestID, ok := ctx.Value("X-Request-ID").(string); ok && requestID != "" { | ||
| return fmt.Sprintf("%s (RequestID: %s)", baseError, requestID) | ||
| } | ||
|
|
||
| return baseError | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we want to place this utility logic within ApiError model? Potentially this can be simplified to a utility function separate from ApiError which simply obtains the RequestID from the Go context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great point! You're absolutely right that putting this logic in the ApiError model creates unnecessary coupling. A utility function approach would be much cleaner and more reusable.
I can simplify this by:
1- Removing the GetContextualError method from ApiError
2- Adding a simple GetRequestID(ctx) utility function
3- Letting users format the error message with request ID in their logging layer
This keeps the models clean and gives users more flexibility in how they format error messages.
Would you prefer I implement it as a simple GetRequestID(ctx) utility function?
| // Test with request ID in context | ||
| ctx := context.WithValue(context.Background(), "X-Request-ID", "test-request-123") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to the statement
Provide specific request identifiers to MongoDB support for faster resolution
Has there been a request from the support team to add this information? Do we know if they are capable of leveraging these values/find them useful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a fair question. I made an assumption that support could leverage these values, but I don't have confirmation from the support team or know the right channel to ask them. Could you help me connect with the right people on the support team to get their opinion?
In the meantime, the primary value I see is actually for client-side debugging and tracing in distributed applications. The main benefits would be:
- Correlating client-side errors with specific API requests in our own logs
- Tracing requests across microservices in our architecture
- Better debugging for complex client applications
Would you be able to help me either:
1- Connect with the support team to understand if they'd find client-provided request IDs valuable, or
2- Should we focus on the client-side benefits for now and consider the support angle separately?
I'm happy to adjust the PR based on whatever feedback we get from support.
The main value is enabling better distributed tracing between client applications and the Atlas API. This allows developers to:
This is particularly valuable for enterprise applications and complex deployments where understanding the full request flow is crucial for debugging and monitoring. |
|
With latest inputs I am seeing the following 2 aspects being handled in this PR:
For For With these considerations I would opt for closing this PR as of now until we receive further inputs from support. Happy to continue the conversation. |
Problem
Currently, the SDK lacks request correlation capabilities, making it difficult to:
Solution
Implement request ID propagation that enables distributed tracing through:
X-Request-IDheader inclusion from contextChanges Made
Template and Specification Changes (Source)
openapi/atlas-api.yaml- Addedx-is-api-error: truevendor extension to ApiError schematools/config/go-templates/client.mustache- Added request ID header propagation inprepareRequestmethodtools/config/go-templates/model_simple.mustache- AddedGetContextualErrormethod with context importsGenerated Code (Expected Output)
admin/client.go- UpdatedprepareRequestmethod with request ID handlingadmin/model_api_error.go- AddedGetContextualErrormethod to ApiError structTests
admin/client_test.go- Tests for request ID header propagationadmin/model_api_error_test.go- Tests forGetContextualErrormethod functionalityUsage
Basic Usage
Distributed Tracing Example
Benefits
Generation Note
This PR modifies source templates and specifications. The included Go file changes reflect the expected output from regeneration. After merging, the following regeneration steps are recommended:
The changes are designed to work with the existing generation pipeline in tools/scripts/generate.sh.
Testing
Comprehensive tests included:
Type of change:
Required Checklist:
make fmtand formatted my code