Skip to content

Commit

Permalink
PR feedback and refactor: bring logwriter logic directly into sqliteS…
Browse files Browse the repository at this point in the history
…tore and unexport storeName, clean up
  • Loading branch information
zackattack01 committed Jun 25, 2024
1 parent 5060d05 commit 84ce6c1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 62 deletions.
12 changes: 6 additions & 6 deletions ee/agent/storage/sqlite/keyvalue_store_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import (
_ "modernc.org/sqlite"
)

type StoreName int
type storeName int

const (
StartupSettingsStore StoreName = iota
WatchdogLogStore StoreName = 1
StartupSettingsStore storeName = iota
WatchdogLogStore storeName = 1
)

var missingMigrationErrFormat = regexp.MustCompile(`no migration found for version \d+`)

// String translates the exported int constant to the actual name of the
// supported table in the sqlite database.
func (s StoreName) String() string {
func (s storeName) String() string {
switch s {
case StartupSettingsStore:
return "startup_settings"
Expand Down Expand Up @@ -62,7 +62,7 @@ type sqliteColumns struct {

// OpenRO opens a connection to the database in the given root directory; it does
// not perform database creation or migration.
func OpenRO(ctx context.Context, rootDirectory string, name StoreName) (*sqliteStore, error) {
func OpenRO(ctx context.Context, rootDirectory string, name storeName) (*sqliteStore, error) {
if name.String() == "" {
return nil, fmt.Errorf("unsupported table %d", name)
}
Expand All @@ -82,7 +82,7 @@ func OpenRO(ctx context.Context, rootDirectory string, name StoreName) (*sqliteS

// OpenRW creates a validated database connection to a validated database, performing
// migrations if necessary.
func OpenRW(ctx context.Context, rootDirectory string, name StoreName) (*sqliteStore, error) {
func OpenRW(ctx context.Context, rootDirectory string, name storeName) (*sqliteStore, error) {
if name.String() == "" {
return nil, fmt.Errorf("unsupported table %d", name)
}
Expand Down
25 changes: 25 additions & 0 deletions ee/agent/storage/sqlite/logstore_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"strings"
"time"
)

func (s *sqliteStore) AppendValue(timestamp int64, value []byte) error {
Expand Down Expand Up @@ -98,3 +99,27 @@ func (s *sqliteStore) ForEach(fn func(rowid, timestamp int64, v []byte) error) e

return nil
}

// Write implements the io.Writer interface, allowing sqliteStore to be used as
// a logging backend via multislogger handler
func (s *sqliteStore) Write(p []byte) (n int, err error) {
if s.readOnly {
return 0, errors.New("cannot perform write with RO connection")
}

colInfo := s.getColumns()
if s == nil || s.conn == nil || colInfo == nil {
return 0, errors.New("store is nil")
}

if !colInfo.isLogstore {
return 0, errors.New("this table type is not supported for timestamped logging")
}

timestamp := time.Now().Unix()
if err := s.AppendValue(timestamp, p); err != nil {
return 0, err
}

return len(p), nil
}
9 changes: 5 additions & 4 deletions ee/watchdog/watchdog_service_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/kolide/launcher/ee/gowrapper"
"github.com/kolide/launcher/pkg/launcher"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/kolide/launcher/pkg/log/sqlitelogger"
"github.com/pkg/errors"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc"
Expand Down Expand Up @@ -51,12 +50,14 @@ func RunWatchdogService(systemSlogger *multislogger.MultiSlogger, args []string)
// Create a local logger to drop logs into the sqlite DB. These will be collected and published
// to debug.json from the primary launcher invocation
if opts.RootDirectory != "" {
ll, err := sqlitelogger.NewSqliteLogWriter(ctx, opts.RootDirectory, agentsqlite.WatchdogLogStore)
logWriter, err := agentsqlite.OpenRW(ctx, opts.RootDirectory, agentsqlite.WatchdogLogStore)
if err != nil {
return fmt.Errorf("initializing sqlite log writer: %w", err)
return fmt.Errorf("opening log db in %s: %w", opts.RootDirectory, err)
}

localSloggerHandler := slog.NewJSONHandler(ll, &slog.HandlerOptions{Level: slog.LevelDebug})
defer logWriter.Close()

localSloggerHandler := slog.NewJSONHandler(logWriter, &slog.HandlerOptions{Level: slog.LevelDebug})

// add the sqlite handler to both local and systemSloggers
localSlogger.AddHandler(localSloggerHandler)
Expand Down
52 changes: 0 additions & 52 deletions pkg/log/sqlitelogger/sqlite_logger.go

This file was deleted.

0 comments on commit 84ce6c1

Please sign in to comment.