[3.3.3 backport] CBG-5116: Diagnostic API Improvements#7999
[3.3.3 backport] CBG-5116: Diagnostic API Improvements#7999bbrks merged 15 commits intorelease/3.3.3from
Conversation
…ad of query param (#7956)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Redocly previews |
There was a problem hiding this comment.
Pull request overview
This PR backports diagnostic API improvements to version 3.3.3, changing the HTTP methods for sync function and import filter dry-run endpoints from GET to POST, and enhancing their functionality with support for custom functions, user context, metadata, and improved logging.
Changes:
- Changed HTTP methods from GET to POST for
_syncand_import_filterdiagnostic endpoints - Enhanced dry-run endpoints to accept custom sync functions and import filters in request bodies
- Added support for user context, user xattrs, and logging capture during dry-run operations
Reviewed changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| rest/routing.go | Updated HTTP methods from GET to POST for diagnostic endpoints |
| rest/handler.go | Added audit event handling for diagnostic server requests |
| rest/diagnostic_doc_api_test.go | Removed old test and added comprehensive new tests for sync function and import filter dry-run functionality |
| rest/diagnostic_doc_api.go | Refactored handlers to accept POST requests with enhanced payload structures and logging support |
| rest/audit_test.go | Added test case for diagnostic request auditing |
| docs/api/paths/diagnostic/keyspace-sync.yaml | Updated API documentation to reflect POST method and new request body structure |
| docs/api/paths/diagnostic/keyspace-import_filter.yaml | Updated API documentation to reflect POST method and new request body structure |
| docs/api/diagnostic.yaml | Fixed incorrect endpoint paths in API documentation |
| docs/api/components/schemas.yaml | Added new schema definitions for diagnostic function exception and logging |
| db/revision_test.go | Updated benchmark to use renamed exported function |
| db/revision.go | Exported StripInternalProperties function (renamed from stripInternalProperties) |
| db/import_test.go | Updated test calls to match refactored EvaluateFunction signature |
| db/import_listener.go | Updated calls to use renamed UserXattrKey method |
| db/import.go | Refactored import filter evaluation to support custom functions and logging |
| db/document.go | Updated call to use renamed UserXattrKey method |
| db/database_collection.go | Exported UserXattrKey method (renamed from userXattrKey) |
| db/database.go | Updated calls to use renamed methods |
| db/crud.go | Refactored sync function dry-run to accept custom functions and support logging |
| db/change_cache.go | Updated calls to use renamed UserXattrKey method |
| channels/sync_runner.go | Added NewSyncRunnerWithLogging to support custom logging functions |
| base/error.go | Added custom error types for sync function and import filter dry-run errors |
| base/audit_events.go | Added new audit event ID for diagnostic HTTP API requests |
| ShouldImport bool `json:"shouldImport"` | ||
| Error string `json:"error"` | ||
| ShouldImport bool `json:"shouldImport"` | ||
| Exception string `json:"exception"` |
There was a problem hiding this comment.
The Exception field in ImportFilterDryRun should have the omitempty JSON tag for consistency with the SyncFnDryRun struct where Exception is marked as omitempty (line 33). This ensures the field is omitted from JSON responses when empty.
| Exception string `json:"exception"` | |
| Exception string `json:"exception,omitempty"` |
| description: |- | ||
| Optional XATTR values to include during evaluation. | ||
|
|
||
| To provide the user XATTR, include a property whose name exactly matches the configured `user_xattr_key`. |
There was a problem hiding this comment.
The description spans multiple lines inconsistently. The continuation should be part of a single description field or properly formatted with line continuation indicators in YAML.
| description: |- | |
| Optional XATTR values to include during evaluation. | |
| To provide the user XATTR, include a property whose name exactly matches the configured `user_xattr_key`. | |
| description: Optional XATTR values to include during evaluation. To provide the user XATTR, include a property whose name exactly matches the configured `user_xattr_key`. |
| var docid string | ||
|
|
||
| id, _ := syncDryRunPayload.Doc[db.BodyId].(string) | ||
| docid = cmp.Or(bucketDocID, id, SYNC_FN_DIAGNOSTIC_DOCID) |
There was a problem hiding this comment.
The use of cmp.Or to select the first non-empty string from multiple sources works correctly, but the logic would be clearer with explicit conditional checks given the significance of docid selection in this context. Consider using an explicit if-else chain to make the precedence order more apparent.
bbrks
left a comment
There was a problem hiding this comment.
Looks fine but I have a few questions (that may also affect main/4.0.3)
| result, err := i.Call(ctx, doc) | ||
| if err != nil { | ||
| if !dryRun { | ||
| base.WarnfCtx(ctx, "Unexpected error invoking import filter for document %s - processing aborted, document will not be imported. Error: %v", base.UD(doc), err) |
There was a problem hiding this comment.
I've just noticed we're removing this warning logging and relying on the caller which only logs at info. Was this intentional? It seems like customers may miss this message at info level.
There was a problem hiding this comment.
We do return the error back to the handler, which will send then return this error as part of exception in the response body
There was a problem hiding this comment.
As discussed - there is no request here to get back to a user. An import is sourced from a doc coming over the DCP feed to Sync Gateway. Warnings are the only way of communicating this error back.
| return boolResult, nil | ||
| default: | ||
| if !dryRun { | ||
| base.WarnfCtx(ctx, "Import filter function returned non-boolean result %v Type: %T", result, result) |
There was a problem hiding this comment.
Likewise, this warning going means that customers won't see this since the caller logs errors at info level.
There was a problem hiding this comment.
Similarly, the error will be sent back to the user as a exception in the response
| } | ||
| bodyBytes, err := json.Marshal(request) | ||
| require.NoError(t, err) | ||
| RequireStatus(t, rt.SendDiagnosticRequest(http.MethodPost, "/{{.keyspace}}/_sync", string(bodyBytes)), http.StatusOK) |
There was a problem hiding this comment.
Just needs the extra auth test commit pulled into this PR branch from #8006
|
See #8017 for restoration of warning logs - we can cherry-pick that into this Edit: I'll follow up with this cherry-pick so you can review independently. |
CBG-5116
Describe your PR here...
Pre-review checklist
fmt.Print,log.Print, ...)base.UD(docID),base.MD(dbName))docs/apiDependencies (if applicable)
Integration Tests
GSI=true,xattrs=truehttps://jenkins.sgwdev.com/job/SyncGatewayIntegration/0000/