MM-43077 Allow inline JSON in config.json for advanced logging config (#23324)

* add AdvancedLoggingJSON to LogSettings and deprecate AdvancedLoggingConfig
* allow embedded JSON in config for advanced logging.
Этот коммит содержится в:
Doug Lauder
2023-05-15 10:37:48 -04:00
коммит произвёл GitHub
родитель 156d1429de
Коммит e4075dae18
11 изменённых файлов: 324 добавлений и 74 удалений

Просмотреть файл

@@ -7,13 +7,20 @@ import (
"encoding/json"
"errors"
"path/filepath"
"strconv"
"strings"
"sync"
"github.com/mattermost/mattermost-server/server/public/shared/mlog"
)
const (
LogConfigSrcTypeJSON LogConfigSrcType = "json"
LogConfigSrcTypeFile LogConfigSrcType = "file"
)
type LogSrcListener func(old, new mlog.LoggerConfiguration)
type LogConfigSrcType string
// LogConfigSrc abstracts the Advanced Logging configuration so that implementations can
// fetch from file, database, etc.
@@ -22,7 +29,10 @@ type LogConfigSrc interface {
Get() mlog.LoggerConfiguration
// Set updates the dsn specifying the source and reloads
Set(dsn string, configStore *Store) (err error)
Set(dsn []byte, configStore *Store) (err error)
// GetType returns the type of config source (JSON, file, ...)
GetType() LogConfigSrcType
// Close cleans up resources.
Close() error
@@ -30,8 +40,8 @@ type LogConfigSrc interface {
// NewLogConfigSrc creates an advanced logging configuration source, backed by a
// file, JSON string, or database.
func NewLogConfigSrc(dsn string, configStore *Store) (LogConfigSrc, error) {
if dsn == "" {
func NewLogConfigSrc(dsn json.RawMessage, configStore *Store) (LogConfigSrc, error) {
if len(dsn) == 0 {
return nil, errors.New("dsn should not be empty")
}
@@ -39,17 +49,28 @@ func NewLogConfigSrc(dsn string, configStore *Store) (LogConfigSrc, error) {
return nil, errors.New("configStore should not be nil")
}
dsn = strings.TrimSpace(dsn)
// check if embedded JSON
if isJSONMap(dsn) {
return newJSONSrc(dsn)
}
path := dsn
// Now we're treating the DSN as a string which may contain escaped JSON or be a filespec.
str := strings.TrimSpace(string(dsn))
if s, err := strconv.Unquote(str); err == nil {
str = s
}
// check if escaped JSON
strBytes := []byte(str)
if isJSONMap(strBytes) {
return newJSONSrc(strBytes)
}
// If this is a file based config we need the full path so it can be watched.
if strings.HasPrefix(configStore.String(), "file://") && !filepath.IsAbs(dsn) {
path := str
if strings.HasPrefix(configStore.String(), "file://") && !filepath.IsAbs(path) {
configPath := strings.TrimPrefix(configStore.String(), "file://")
path = filepath.Join(filepath.Dir(configPath), dsn)
path = filepath.Join(filepath.Dir(configPath), path)
}
return newFileSrc(path, configStore)
@@ -63,7 +84,7 @@ type jsonSrc struct {
cfg mlog.LoggerConfiguration
}
func newJSONSrc(data string) (*jsonSrc, error) {
func newJSONSrc(data json.RawMessage) (*jsonSrc, error) {
src := &jsonSrc{}
return src, src.Set(data, nil)
}
@@ -76,8 +97,8 @@ func (src *jsonSrc) Get() mlog.LoggerConfiguration {
}
// Set updates the JSON specifying the source and reloads
func (src *jsonSrc) Set(data string, _ *Store) error {
cfg, err := logTargetCfgFromJSON([]byte(data))
func (src *jsonSrc) Set(data []byte, _ *Store) error {
cfg, err := logTargetCfgFromJSON(data)
if err != nil {
return err
}
@@ -86,6 +107,11 @@ func (src *jsonSrc) Set(data string, _ *Store) error {
return nil
}
// GetType returns the config source type.
func (src *jsonSrc) GetType() LogConfigSrcType {
return LogConfigSrcTypeJSON
}
func (src *jsonSrc) set(cfg mlog.LoggerConfiguration) {
src.mutex.Lock()
defer src.mutex.Unlock()
@@ -112,7 +138,7 @@ func newFileSrc(path string, configStore *Store) (*fileSrc, error) {
src := &fileSrc{
path: path,
}
if err := src.Set(path, configStore); err != nil {
if err := src.Set([]byte(path), configStore); err != nil {
return nil, err
}
return src, nil
@@ -128,8 +154,8 @@ func (src *fileSrc) Get() mlog.LoggerConfiguration {
// Set updates the dsn specifying the file source and reloads.
// The file will be watched for changes and reloaded as needed,
// and all listeners notified.
func (src *fileSrc) Set(path string, configStore *Store) error {
data, err := configStore.GetFile(path)
func (src *fileSrc) Set(path []byte, configStore *Store) error {
data, err := configStore.GetFile(string(path))
if err != nil {
return err
}
@@ -143,6 +169,11 @@ func (src *fileSrc) Set(path string, configStore *Store) error {
return nil
}
// GetType returns the config source type.
func (src *fileSrc) GetType() LogConfigSrcType {
return LogConfigSrcTypeFile
}
func (src *fileSrc) set(cfg mlog.LoggerConfiguration) {
src.mutex.Lock()
defer src.mutex.Unlock()