Allow inline JSON in config.json for advanced logging config (#20954)

* Allow embedded JSON in config.json for AdvancedLoggingConfig

* fix escaped JSON case

* Add unit test cases for escaped JSON

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Doug Lauder
2022-09-09 07:51:17 -04:00
коммит произвёл GitHub
родитель e14fd1d955
Коммит 5211d5de15
9 изменённых файлов: 98 добавлений и 72 удалений

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

@@ -4,9 +4,11 @@
package config
import (
"bytes"
"encoding/json"
"errors"
"path/filepath"
"strconv"
"strings"
"sync"
@@ -22,16 +24,23 @@ 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)
// Close cleans up resources.
Close() error
}
func IsEmptyDSN(dsn json.RawMessage) bool {
if len(dsn) == 0 || bytes.Equal(dsn, []byte("{}")) || bytes.Equal(dsn, []byte("\"\"")) {
return true
}
return false
}
// 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 == "" {
// file, JSON, or database.
func NewLogConfigSrc(dsn json.RawMessage, configStore *Store) (LogConfigSrc, error) {
if len(dsn) == 0 {
return nil, errors.New("dsn should not be empty")
}
@@ -39,17 +48,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 +83,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 +96,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
}
@@ -112,7 +132,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 +148,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
}