-
Notifications
You must be signed in to change notification settings - Fork 111
/
app.go
728 lines (647 loc) · 21 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
package local
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"time"
"github.com/bmatcuk/doublestar/v4"
"github.com/c2h5oh/datasize"
"github.com/rilldata/rill/cli/pkg/browser"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/dotrill"
"github.com/rilldata/rill/cli/pkg/update"
"github.com/rilldata/rill/cli/pkg/web"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime"
"github.com/rilldata/rill/runtime/compilers/rillv1"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/pkg/activity"
"github.com/rilldata/rill/runtime/pkg/debugserver"
"github.com/rilldata/rill/runtime/pkg/email"
"github.com/rilldata/rill/runtime/pkg/graceful"
"github.com/rilldata/rill/runtime/pkg/observability"
"github.com/rilldata/rill/runtime/pkg/ratelimit"
runtimeserver "github.com/rilldata/rill/runtime/server"
"go.opentelemetry.io/otel/attribute"
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
"golang.org/x/sync/errgroup"
"gopkg.in/natefinch/lumberjack.v2"
)
type LogFormat string
// Default log formats for logger
const (
LogFormatConsole = "console"
LogFormatJSON = "json"
)
// Default instance config on local.
const (
DefaultInstanceID = "default"
DefaultOLAPDriver = "duckdb"
DefaultOLAPDSN = "main.db"
DefaultCatalogStore = "meta.db"
DefaultDBDir = "tmp"
)
// App encapsulates the logic associated with configuring and running the UI and the runtime in a local environment.
// Here, a local environment means a non-authenticated, single-instance and single-project setup on localhost.
// App encapsulates logic shared between different CLI commands, like start, init, build and source.
type App struct {
Context context.Context
Runtime *runtime.Runtime
Instance *drivers.Instance
Logger *zap.SugaredLogger
BaseLogger *zap.Logger
Version cmdutil.Version
Verbose bool
Debug bool
ProjectPath string
observabilityShutdown observability.ShutdownFunc
loggerCleanUp func()
activity *activity.Client
}
type AppOptions struct {
Version cmdutil.Version
Verbose bool
Debug bool
Reset bool
Environment string
OlapDriver string
OlapDSN string
ProjectPath string
LogFormat LogFormat
Variables map[string]string
Activity *activity.Client
AdminURL string
AdminToken string
}
func NewApp(ctx context.Context, opts *AppOptions) (*App, error) {
// Setup logger
logger, cleanupFn := initLogger(opts.Verbose, opts.LogFormat)
sugarLogger := logger.Sugar()
// Init Prometheus telemetry
shutdown, err := observability.Start(ctx, logger, &observability.Options{
MetricsExporter: observability.PrometheusExporter,
TracesExporter: observability.NoopExporter,
ServiceName: "rill-local",
ServiceVersion: opts.Version.String(),
})
if err != nil {
return nil, err
}
// Get full path to project
projectPath, err := filepath.Abs(opts.ProjectPath)
if err != nil {
return nil, err
}
dbDirPath := filepath.Join(projectPath, DefaultDBDir)
err = os.MkdirAll(dbDirPath, os.ModePerm) // Create project dir and db dir if it doesn't exist
if err != nil {
return nil, err
}
// old behaviour when data was stored in a stage.db file in the project directory.
// drop old file, remove this code after some time
_, err = os.Stat(filepath.Join(projectPath, "stage.db"))
if err == nil { // a old stage.db file exists
_ = os.Remove(filepath.Join(projectPath, "stage.db"))
_ = os.Remove(filepath.Join(projectPath, "stage.db.wal"))
logger.Info("Dropping old stage.db file and rebuilding project")
}
// Create a local runtime with an in-memory metastore
systemConnectors := []*runtimev1.Connector{
{
Type: "sqlite",
Name: "metastore",
Config: map[string]string{"dsn": "file:rill?mode=memory&cache=shared"},
},
}
// Sender for sending transactional emails.
// We use a noop sender by default, but you can uncomment the SMTP sender to send emails from localhost for testing.
sender := email.NewNoopSender()
// Uncomment to send emails for testing:
// err = godotenv.Load()
// if err != nil {
// return nil, fmt.Errorf("failed to load .env file: %w", err)
// }
// smtpPort, err := strconv.Atoi(os.Getenv("RILL_RUNTIME_EMAIL_SMTP_PORT"))
// if err != nil {
// return nil, fmt.Errorf("failed to get SMTP port: %w", err)
// }
// sender, err := email.NewSMTPSender(&email.SMTPOptions{
// SMTPHost: os.Getenv("RILL_RUNTIME_EMAIL_SMTP_HOST"),
// SMTPPort: smtpPort,
// SMTPUsername: os.Getenv("RILL_RUNTIME_EMAIL_SMTP_USERNAME"),
// SMTPPassword: os.Getenv("RILL_RUNTIME_EMAIL_SMTP_PASSWORD"),
// FromEmail: os.Getenv("RILL_RUNTIME_EMAIL_SENDER_EMAIL"),
// FromName: os.Getenv("RILL_RUNTIME_EMAIL_SENDER_NAME"),
// BCC: os.Getenv("RILL_RUNTIME_EMAIL_BCC"),
// })
// if err != nil {
// return nil, fmt.Errorf("failed to create email sender: %w", err)
// }
rtOpts := &runtime.Options{
ConnectionCacheSize: 100,
MetastoreConnector: "metastore",
QueryCacheSizeBytes: int64(datasize.MB * 100),
AllowHostAccess: true,
DataDir: dbDirPath,
SystemConnectors: systemConnectors,
SecurityEngineCacheSize: 1000,
ControllerLogBufferCapacity: 10000,
ControllerLogBufferSizeBytes: int64(datasize.MB * 16),
}
rt, err := runtime.New(ctx, rtOpts, logger, opts.Activity, email.New(sender))
if err != nil {
return nil, err
}
// Merge opts.Variables with some local overrides of the defaults in runtime/drivers.InstanceConfig.
vars := map[string]string{
"rill.download_row_limit": "0", // 0 means unlimited
"rill.stage_changes": "false",
}
for k, v := range opts.Variables {
vars[k] = v
}
// Prepare connectors for the instance
var connectors []*runtimev1.Connector
// If the OLAP is the default OLAP (DuckDB in stage.db), we make it relative to the project directory (not the working directory)
defaultOLAP := false
olapDSN := opts.OlapDSN
olapCfg := make(map[string]string)
if opts.OlapDriver == DefaultOLAPDriver && olapDSN == DefaultOLAPDSN {
defaultOLAP = true
olapDSN = path.Join(dbDirPath, olapDSN)
// Set path which overrides the duckdb's default behaviour to store duckdb data in data_dir/<instance_id>/<connector> directory which is not backward compatible
olapCfg["path"] = olapDSN
val, err := isExternalStorageEnabled(dbDirPath, vars)
if err != nil {
return nil, err
}
olapCfg["external_table_storage"] = strconv.FormatBool(val)
}
if opts.Reset {
_ = os.RemoveAll(dbDirPath)
err = os.MkdirAll(dbDirPath, os.ModePerm)
if err != nil {
return nil, err
}
}
// Set default DuckDB pool size to 4
olapCfg["dsn"] = olapDSN
if opts.OlapDriver == "duckdb" {
olapCfg["pool_size"] = "4"
if !defaultOLAP {
olapCfg["error_on_incompatible_version"] = "true"
}
}
// Add OLAP connector
olapConnector := &runtimev1.Connector{
Type: opts.OlapDriver,
Name: opts.OlapDriver,
Config: olapCfg,
}
connectors = append(connectors, olapConnector)
// The repo connector is the local project directory
repoConnector := &runtimev1.Connector{
Type: "file",
Name: "repo",
Config: map[string]string{"dsn": projectPath},
}
connectors = append(connectors, repoConnector)
// The catalog connector is a SQLite database in the project directory's tmp folder
catalogConnector := &runtimev1.Connector{
Type: "sqlite",
Name: "catalog",
Config: map[string]string{"dsn": fmt.Sprintf("file:%s?cache=shared", filepath.Join(dbDirPath, DefaultCatalogStore))},
}
connectors = append(connectors, catalogConnector)
// Use the admin service for AI
aiConnector := &runtimev1.Connector{
Name: "admin",
Type: "admin",
Config: map[string]string{
"admin_url": opts.AdminURL,
"access_token": opts.AdminToken,
},
}
connectors = append(connectors, aiConnector)
// Print start status – need to do it before creating the instance, since doing so immediately starts the controller
isInit := IsProjectInit(projectPath)
if isInit {
sugarLogger.Infof("Hydrating project '%s'", projectPath)
}
// Create instance with its repo set to the project directory
inst := &drivers.Instance{
ID: DefaultInstanceID,
Environment: opts.Environment,
OLAPConnector: olapConnector.Name,
RepoConnector: repoConnector.Name,
AIConnector: aiConnector.Name,
CatalogConnector: catalogConnector.Name,
Connectors: connectors,
Variables: vars,
Annotations: map[string]string{},
WatchRepo: true,
// ModelMaterializeDelaySeconds: 30, // TODO: Enable when we support skipping it for the initial load
IgnoreInitialInvalidProjectError: !isInit, // See ProjectParser reconciler for details
}
err = rt.CreateInstance(ctx, inst)
if err != nil {
return nil, err
}
// Create app
app := &App{
Context: ctx,
Runtime: rt,
Instance: inst,
Logger: sugarLogger,
BaseLogger: logger,
Version: opts.Version,
Verbose: opts.Verbose,
Debug: opts.Debug,
ProjectPath: projectPath,
observabilityShutdown: shutdown,
loggerCleanUp: cleanupFn,
activity: opts.Activity,
}
// Collect and emit information about connectors at start time
err = app.emitStartEvent(ctx)
if err != nil {
logger.Debug("failed to emit start event", zap.Error(err))
}
return app, nil
}
func (a *App) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := a.observabilityShutdown(ctx)
if err != nil {
a.Logger.Error("Observability shutdown failed", zap.Error(err))
}
err = a.Runtime.Close()
if err != nil {
a.Logger.Error("Graceful shutdown failed", zap.Error(err))
} else {
a.Logger.Info("Rill shutdown gracefully")
}
a.loggerCleanUp()
return nil
}
func (a *App) Serve(httpPort, grpcPort int, enableUI, openBrowser, readonly bool, userID, tlsCertPath, tlsKeyPath string) error {
// Get analytics info
installID, enabled, err := dotrill.AnalyticsInfo()
if err != nil {
a.Logger.Warnf("error finding install ID: %v", err)
}
// Build local info for frontend
inf := &localInfo{
InstanceID: a.Instance.ID,
GRPCPort: grpcPort,
InstallID: installID,
ProjectPath: a.ProjectPath,
UserID: userID,
Version: a.Version.Number,
BuildCommit: a.Version.Commit,
BuildTime: a.Version.Timestamp,
IsDev: a.Version.IsDev(),
AnalyticsEnabled: enabled,
Readonly: readonly,
}
// Create server logger
serverLogger := a.BaseLogger
// It only logs error messages when !verbose to prevent lots of req/res info messages.
if !a.Verbose {
serverLogger = a.BaseLogger.WithOptions(zap.IncreaseLevel(zap.ErrorLevel))
}
// Prepare errgroup and context with graceful shutdown
gctx := graceful.WithCancelOnTerminate(a.Context)
group, ctx := errgroup.WithContext(gctx)
// Create a runtime server
opts := &runtimeserver.Options{
HTTPPort: httpPort,
GRPCPort: grpcPort,
TLSCertPath: tlsCertPath,
TLSKeyPath: tlsKeyPath,
AllowedOrigins: []string{"*"},
ServePrometheus: true,
}
runtimeServer, err := runtimeserver.NewServer(ctx, opts, a.Runtime, serverLogger, ratelimit.NewNoop(), a.activity)
if err != nil {
return err
}
// Start the gRPC server
group.Go(func() error {
return runtimeServer.ServeGRPC(ctx)
})
// Start the local HTTP server
group.Go(func() error {
return runtimeServer.ServeHTTP(ctx, func(mux *http.ServeMux) {
// Inject local-only endpoints on the server for the local UI and local backend endpoints
if enableUI {
mux.Handle("/", web.StaticHandler())
}
mux.Handle("/local/config", a.infoHandler(inf))
mux.Handle("/local/version", a.versionHandler())
mux.Handle("/local/track", a.trackingHandler())
})
})
// Start debug server on port 6060
if a.Debug {
group.Go(func() error { return debugserver.ServeHTTP(ctx, 6060) })
}
// if keypath and certpath are provided
secure := tlsCertPath != "" && tlsKeyPath != ""
// Open the browser when health check succeeds
go a.pollServer(ctx, httpPort, enableUI && openBrowser, secure)
// Run the server
err = group.Wait()
if err != nil && !errors.Is(err, context.Canceled) {
return fmt.Errorf("server crashed: %w", err)
}
return nil
}
func (a *App) pollServer(ctx context.Context, httpPort int, openOnHealthy, secure bool) {
client := &http.Client{Timeout: time.Second}
scheme := "http"
if secure {
scheme = "https"
client.Transport = &http.Transport{
// nolint:gosec // this is a health check against localhost, so it's safe to ignore the cert
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
uri := fmt.Sprintf("%s://localhost:%d", scheme, httpPort)
for {
// Check for cancellation
if ctx.Err() != nil {
return
}
// Check if server is up
resp, err := client.Get(uri + "/v1/ping")
if err == nil {
resp.Body.Close()
if resp.StatusCode < http.StatusInternalServerError {
break
}
}
// Wait a bit and retry
time.Sleep(250 * time.Millisecond)
}
// Health check succeeded
a.Logger.Infof("Serving Rill on: %s", uri)
if openOnHealthy {
err := browser.Open(uri)
if err != nil {
a.Logger.Debugf("could not open browser: %v", err)
}
}
}
type localInfo struct {
InstanceID string `json:"instance_id"`
GRPCPort int `json:"grpc_port"`
InstallID string `json:"install_id"`
UserID string `json:"user_id"`
ProjectPath string `json:"project_path"`
Version string `json:"version"`
BuildCommit string `json:"build_commit"`
BuildTime string `json:"build_time"`
IsDev bool `json:"is_dev"`
AnalyticsEnabled bool `json:"analytics_enabled"`
Readonly bool `json:"readonly"`
}
// infoHandler servers the local info struct.
func (a *App) infoHandler(info *localInfo) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(info)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Header().Add("Content-Type", "application/json")
_, err = w.Write(data)
if err != nil {
http.Error(w, fmt.Sprintf("failed to write response data: %s", err), http.StatusInternalServerError)
return
}
})
}
// versionHandler servers the version struct.
func (a *App) versionHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get the latest version available
latestVersion, err := update.LatestVersion(r.Context())
if err != nil {
a.Logger.Warnf("error finding latest version: %v", err)
}
inf := &versionInfo{
CurrentVersion: a.Version.Number,
LatestVersion: latestVersion,
}
data, err := json.Marshal(inf)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Header().Add("Content-Type", "application/json")
_, err = w.Write(data)
if err != nil {
http.Error(w, fmt.Sprintf("failed to write response data: %s", err), http.StatusInternalServerError)
return
}
})
}
type versionInfo struct {
CurrentVersion string `json:"current_version"`
LatestVersion string `json:"latest_version"`
}
// trackingHandler proxies events to intake.rilldata.io.
func (a *App) trackingHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Read entire body up front (since it may be closed before the request is sent in the goroutine below)
body, err := io.ReadAll(r.Body)
if err != nil {
a.BaseLogger.Info("failed to read telemetry request", zap.Error(err))
w.WriteHeader(http.StatusOK)
return
}
// Parse the body as JSON
var event map[string]any
err = json.Unmarshal(body, &event)
if err != nil {
a.BaseLogger.Info("failed to parse telemetry request", zap.Error(err))
w.WriteHeader(http.StatusOK)
return
}
// Pass as raw event to the telemetry client
err = a.activity.RecordRaw(event)
if err != nil {
a.BaseLogger.Info("failed to proxy telemetry event from UI", zap.Error(err))
}
w.WriteHeader(http.StatusOK)
})
}
// emitStartEvent sends a telemetry event with information about the project' state.
// It is not a blocking operation (events are flushed in the background).
func (a *App) emitStartEvent(ctx context.Context) error {
repo, instanceID, err := cmdutil.RepoForProjectPath(a.ProjectPath)
if err != nil {
return err
}
parser, err := rillv1.Parse(ctx, repo, instanceID, a.Instance.Environment, a.Instance.OLAPConnector)
if err != nil {
return err
}
connectors, err := parser.AnalyzeConnectors(ctx)
if err != nil {
return err
}
var connectorNames []string
for _, connector := range connectors {
connectorNames = append(connectorNames, connector.Name)
}
a.activity.RecordBehavioralLegacy(activity.BehavioralEventAppStart, attribute.StringSlice("connectors", connectorNames), attribute.String("olap_connector", a.Instance.OLAPConnector))
return nil
}
// IsProjectInit checks if the project is initialized by checking if rill.yaml exists in the project directory.
// It doesn't use any runtime functions since we need the ability to check this before creating the instance.
func IsProjectInit(projectPath string) bool {
rillYAML := filepath.Join(projectPath, "rill.yaml")
if _, err := os.Stat(rillYAML); err != nil {
return false
}
return true
}
func ParseLogFormat(format string) (LogFormat, bool) {
switch format {
case "json":
return LogFormatJSON, true
case "console":
return LogFormatConsole, true
default:
return "", false
}
}
func initLogger(isVerbose bool, logFormat LogFormat) (logger *zap.Logger, cleanupFn func()) {
logLevel := zapcore.InfoLevel
if isVerbose {
logLevel = zapcore.DebugLevel
}
logPath, err := dotrill.ResolveFilename("rill.log", true)
if err != nil {
panic(err)
}
// lumberjack.Logger is already safe for concurrent use, so we don't need to
// lock it.
luLogger := &lumberjack.Logger{
Filename: logPath,
MaxSize: 100, // megabytes
MaxBackups: 3,
MaxAge: 30, // days
Compress: true,
}
cfg := zap.NewProductionEncoderConfig()
// hide logger name like `console`
cfg.NameKey = zapcore.OmitKey
fileCore := zapcore.NewCore(zapcore.NewJSONEncoder(cfg), zapcore.AddSync(luLogger), logLevel)
var consoleEncoder zapcore.Encoder
opts := make([]zap.Option, 0)
switch logFormat {
case LogFormatJSON:
cfg := zap.NewProductionEncoderConfig()
cfg.NameKey = zapcore.OmitKey
// never
opts = append(opts, zap.AddStacktrace(zapcore.InvalidLevel))
consoleEncoder = zapcore.NewJSONEncoder(cfg)
case LogFormatConsole:
encCfg := zap.NewDevelopmentEncoderConfig()
encCfg.NameKey = zapcore.OmitKey
encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
encCfg.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02T15:04:05.000")
consoleEncoder = zapcore.NewConsoleEncoder(encCfg)
}
// if it's not verbose, skip instance_id field
if !isVerbose {
consoleEncoder = skipFieldZapEncoder{
Encoder: consoleEncoder,
fields: []string{"instance_id"},
}
}
core := zapcore.NewTee(
fileCore,
zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stdout), logLevel),
)
return zap.New(core, opts...), func() {
_ = logger.Sync()
luLogger.Close()
}
}
// skipFieldZapEncoder skips fields with the given keys. only string fields are supported.
type skipFieldZapEncoder struct {
zapcore.Encoder
fields []string
}
func (s skipFieldZapEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
res := make([]zapcore.Field, 0, len(fields))
for _, field := range fields {
skip := false
for _, skipField := range s.fields {
if field.Key == skipField {
skip = true
break
}
}
if !skip {
res = append(res, field)
}
}
return s.Encoder.EncodeEntry(entry, res)
}
func (s skipFieldZapEncoder) Clone() zapcore.Encoder {
return skipFieldZapEncoder{
Encoder: s.Encoder.Clone(),
fields: s.fields,
}
}
func (s skipFieldZapEncoder) AddString(key, val string) {
skip := false
for _, skipField := range s.fields {
if key == skipField {
skip = true
break
}
}
if !skip {
s.Encoder.AddString(key, val)
}
}
// isExternalStorageEnabled determines if external storage can be enabled.
// we can't always enable `external_table_storage` if the project dir already has a db file
// it could have been created with older logic where every source was a table in the main db
func isExternalStorageEnabled(dbPath string, variables map[string]string) (bool, error) {
_, err := os.Stat(filepath.Join(dbPath, DefaultOLAPDSN))
if err != nil {
// fresh project
// check if flag explicitly passed
val, ok := variables["connector.duckdb.external_table_storage"]
if !ok {
// mark enabled by default
return true, nil
}
return strconv.ParseBool(val)
}
fsRoot := os.DirFS(dbPath)
glob := path.Clean(path.Join("./", filepath.Join("*", "version.txt")))
matches, err := doublestar.Glob(fsRoot, glob)
if err != nil {
return false, err
}
return len(matches) > 0, nil
}