fleet/server/datastore/mysql/config.go
Victor Lyuboslavsky ccc36a9cb3
Finishing mysql package migration to slog (#40350)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #40054

# Checklist for submitter

- [ ] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
  - Already present in previous PR

## Testing

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Chores**
* Migrated logging to a structured, context-aware backend for clearer,
richer diagnostics and consistent log formatting.
* Introduced broader context propagation and adjusted internal
interfaces to support the new logging approach (no end-user behavior
changes).
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-24 16:52:36 -06:00

77 lines
2 KiB
Go

package mysql
import (
"log/slog"
"time"
"github.com/fleetdm/fleet/v4/server/config"
common_mysql "github.com/fleetdm/fleet/v4/server/platform/mysql"
"github.com/ngrok/sqlmw"
)
const (
defaultMaxAttempts int = 15
defaultMinLastOpenedAtDiff = time.Hour
)
// DBOption is used to pass optional arguments to a database connection
type DBOption func(o *common_mysql.DBOptions) error
// Logger adds a Logger to the datastore.
func Logger(l *slog.Logger) DBOption {
return func(o *common_mysql.DBOptions) error {
o.Logger = l
return nil
}
}
// WithInterceptor adds the sql Interceptor to the datastore.
func WithInterceptor(i sqlmw.Interceptor) DBOption {
return func(o *common_mysql.DBOptions) error {
o.Interceptor = i
return nil
}
}
// Replica sets the configuration of the read replica for the datastore.
func Replica(conf *config.MysqlConfig) DBOption {
return func(o *common_mysql.DBOptions) error {
o.ReplicaConfig = toCommonMysqlConfig(conf)
return nil
}
}
// LimitAttempts sets a the number of attempts
// to try establishing a connection to the database backend
// the default value is 15 attempts
func LimitAttempts(attempts int) DBOption {
return func(o *common_mysql.DBOptions) error {
o.MaxAttempts = attempts
return nil
}
}
func TracingEnabled(lconfig *config.LoggingConfig) DBOption {
return func(o *common_mysql.DBOptions) error {
o.TracingConfig = toCommonLoggingConfig(lconfig)
return nil
}
}
// WithFleetConfig provides the fleet configuration so that any config option
// that must be used in the datastore layer can be captured here.
func WithFleetConfig(conf *config.FleetConfig) DBOption {
return func(o *common_mysql.DBOptions) error {
o.MinLastOpenedAtDiff = conf.Osquery.MinSoftwareLastOpenedAtDiff
o.PrivateKey = conf.Server.PrivateKey
return nil
}
}
// SQLMode allows setting a custom sql_mode string.
func SQLMode(mode string) DBOption {
return func(o *common_mysql.DBOptions) error {
o.SqlMode = mode
return nil
}
}