Skip to content

Commit 0584eb8

Browse files
committed
add logging to import filter dry run
1 parent 4fd3879 commit 0584eb8

File tree

3 files changed

+162
-32
lines changed

3 files changed

+162
-32
lines changed

db/import.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,9 @@ func (db *DatabaseCollectionWithUser) backupPreImportRevision(ctx context.Contex
489489
// ////// Import Filter Function
490490

491491
// Compiles a JavaScript event function to a jsImportFilterRunner object.
492-
func newImportFilterRunner(ctx context.Context, funcSource string, timeout time.Duration) (sgbucket.JSServerTask, error) {
492+
func newImportFilterRunnerWithLogging(ctx context.Context, funcSource string, timeout time.Duration, errorLogFunc, infoLogFunc func(string)) (sgbucket.JSServerTask, error) {
493493
importFilterRunner := &jsEventTask{}
494-
err := importFilterRunner.InitWithLogging(funcSource, timeout,
495-
func(s string) { base.ErrorfCtx(ctx, base.KeyJavascript.String()+": Import %s", base.UD(s)) },
496-
func(s string) { base.InfofCtx(ctx, base.KeyJavascript, "Import %s", base.UD(s)) })
494+
err := importFilterRunner.InitWithLogging(funcSource, timeout, errorLogFunc, infoLogFunc)
497495
if err != nil {
498496
return nil, err
499497
}
@@ -506,6 +504,12 @@ func newImportFilterRunner(ctx context.Context, funcSource string, timeout time.
506504
return importFilterRunner, nil
507505
}
508506

507+
func newImportFilterRunner(ctx context.Context, funcSource string, timeout time.Duration) (sgbucket.JSServerTask, error) {
508+
errLogFunc := func(s string) { base.ErrorfCtx(ctx, base.KeyJavascript.String()+": Import %s", base.UD(s)) }
509+
infoLogFunc := func(s string) { base.InfofCtx(ctx, base.KeyJavascript, "Import %s", base.UD(s)) }
510+
return newImportFilterRunnerWithLogging(ctx, funcSource, timeout, errLogFunc, infoLogFunc)
511+
}
512+
509513
type ImportFilterFunction struct {
510514
*sgbucket.JSServer
511515
}
@@ -547,7 +551,7 @@ func (i *ImportFilterFunction) EvaluateFunction(ctx context.Context, doc Body, d
547551
}
548552

549553
// ImportFilterDryRun Runs a document through the import filter and returns a boolean and error
550-
func (db *DatabaseCollectionWithUser) ImportFilterDryRun(ctx context.Context, doc Body, importFn string) (bool, error) {
554+
func (db *DatabaseCollectionWithUser) ImportFilterDryRun(ctx context.Context, doc Body, importFn string, errorLogFunc, infoLogFunc func(string)) (bool, error) {
551555

552556
var shouldImport bool
553557

@@ -562,7 +566,7 @@ func (db *DatabaseCollectionWithUser) ImportFilterDryRun(ctx context.Context, do
562566

563567
// create new import filter runner for this dry run
564568
jsTimeout := time.Duration(base.DefaultJavascriptTimeoutSecs) * time.Second
565-
importRunner, err := newImportFilterRunner(ctx, importFn, jsTimeout)
569+
importRunner, err := newImportFilterRunnerWithLogging(ctx, importFn, jsTimeout, errorLogFunc, infoLogFunc)
566570
if err != nil {
567571
return false, errors.New("failed to create import filter runner: " + err.Error())
568572
}

rest/diagnostic_doc_api.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,24 @@ import (
2020
"github.com/couchbase/sync_gateway/db"
2121
)
2222

23-
type SyncFnDryRunLogging struct {
23+
type DryRunLogging struct {
2424
Errors []string `json:"errors"`
2525
Info []string `json:"info"`
2626
}
2727

2828
type SyncFnDryRun struct {
29-
Channels base.Set `json:"channels"`
30-
Access channels.AccessMap `json:"access"`
31-
Roles channels.AccessMap `json:"roles"`
32-
Exception string `json:"exception,omitempty"`
33-
Expiry *uint32 `json:"expiry,omitempty"`
34-
Logging SyncFnDryRunLogging `json:"logging"`
29+
Channels base.Set `json:"channels"`
30+
Access channels.AccessMap `json:"access"`
31+
Roles channels.AccessMap `json:"roles"`
32+
Exception string `json:"exception,omitempty"`
33+
Expiry *uint32 `json:"expiry,omitempty"`
34+
Logging DryRunLogging `json:"logging"`
3535
}
3636

3737
type ImportFilterDryRun struct {
38-
ShouldImport bool `json:"shouldImport"`
39-
Error string `json:"error"`
38+
ShouldImport bool `json:"shouldImport"`
39+
Error string `json:"error"`
40+
Logging DryRunLogging `json:"logging"`
4041
}
4142

4243
type SyncFnDryRunPayload struct {
@@ -173,7 +174,7 @@ func (h *handler) handleSyncFnDryRun() error {
173174
errMsg := syncFnDryRunErr.Error()
174175
resp := SyncFnDryRun{
175176
Exception: errMsg,
176-
Logging: SyncFnDryRunLogging{Errors: logErrors, Info: logInfo},
177+
Logging: DryRunLogging{Errors: logErrors, Info: logInfo},
177178
}
178179
h.writeJSON(resp)
179180
return nil
@@ -189,7 +190,7 @@ func (h *handler) handleSyncFnDryRun() error {
189190
output.Roles,
190191
errorMsg,
191192
output.Expiry,
192-
SyncFnDryRunLogging{Errors: logErrors, Info: logInfo},
193+
DryRunLogging{Errors: logErrors, Info: logInfo},
193194
}
194195
h.writeJSON(resp)
195196
return nil
@@ -220,7 +221,16 @@ func (h *handler) handleImportFilterDryRun() error {
220221
} else {
221222
doc = importFilterPayload.Doc
222223
}
223-
shouldImport, err := h.collection.ImportFilterDryRun(h.ctx(), doc, importFilterPayload.Function)
224+
225+
logErrors := make([]string, 0)
226+
logInfo := make([]string, 0)
227+
errorLogFn := func(s string) {
228+
logErrors = append(logErrors, s)
229+
}
230+
infoLogFn := func(s string) {
231+
logInfo = append(logInfo, s)
232+
}
233+
shouldImport, err := h.collection.ImportFilterDryRun(h.ctx(), doc, importFilterPayload.Function, errorLogFn, infoLogFn)
224234
errorMsg := ""
225235
if err != nil {
226236
var importFilterDryRunErr *base.ImportFilterDryRunError
@@ -232,6 +242,7 @@ func (h *handler) handleImportFilterDryRun() error {
232242
resp := ImportFilterDryRun{
233243
shouldImport,
234244
errorMsg,
245+
DryRunLogging{Errors: logErrors, Info: logInfo},
235246
}
236247
h.writeJSON(resp)
237248
return nil

0 commit comments

Comments
 (0)