Some checks failed
Build / Build Components (push) Has been cancelled
OCI image / Build container images (push) Has been cancelled
Pre-commit hooks / Pre-commit (push) Has been cancelled
Tests and linters / Lint (push) Has been cancelled
Tests and linters / Tests (push) Has been cancelled
Tests and linters / Tests with -race (push) Has been cancelled
Tests and linters / Staticcheck (push) Has been cancelled
Tests and linters / gopls check (push) Has been cancelled
Tests and linters / Run gofumpt (push) Has been cancelled
Vulncheck / Vulncheck (push) Has been cancelled
Change-Id: Ia2a79d6cb2a5eb263fb2e6db3f9cf9f2a7d57118 Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
118 lines
2.8 KiB
Go
118 lines
2.8 KiB
Go
package loggerconfig
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-node/config"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/logging/lokicore/loki"
|
|
)
|
|
|
|
const (
|
|
// LevelDefault is a default logger level.
|
|
LevelDefault = "info"
|
|
DestinationDefault = logger.DestinationStdout
|
|
subsection = "logger"
|
|
lokiSubsection = "loki"
|
|
AddressDefault = "localhost:3100"
|
|
BatchEntriesNumberDefault = 100
|
|
BatchWaitDefault = time.Second
|
|
)
|
|
|
|
// Level returns the value of "level" config parameter
|
|
// from "logger" section.
|
|
//
|
|
// Returns LevelDefault if the value is not a non-empty string.
|
|
func Level(c *config.Config) string {
|
|
v := config.StringSafe(
|
|
c.Sub(subsection),
|
|
"level",
|
|
)
|
|
if v != "" {
|
|
return v
|
|
}
|
|
|
|
return LevelDefault
|
|
}
|
|
|
|
// Destination returns the value of "destination" config parameter
|
|
// from "logger" section.
|
|
//
|
|
// Returns DestinationDefault if the value is not a non-empty string.
|
|
func Destination(c *config.Config) string {
|
|
v := config.StringSafe(
|
|
c.Sub(subsection),
|
|
"destination",
|
|
)
|
|
if v != "" {
|
|
return v
|
|
}
|
|
|
|
return DestinationDefault
|
|
}
|
|
|
|
// Timestamp returns the value of "timestamp" config parameter
|
|
// from "logger" section.
|
|
//
|
|
// Returns false if the value isn't specified.
|
|
func Timestamp(c *config.Config) bool {
|
|
return config.BoolSafe(c.Sub(subsection), "timestamp")
|
|
}
|
|
|
|
// Tags returns the value of "tags" config parameter from "logger" section.
|
|
func Tags(c *config.Config) [][]string {
|
|
var res [][]string
|
|
sub := c.Sub(subsection).Sub("tags")
|
|
for i := 0; ; i++ {
|
|
s := sub.Sub(strconv.FormatInt(int64(i), 10))
|
|
names := config.StringSafe(s, "names")
|
|
if names == "" {
|
|
break
|
|
}
|
|
res = append(res, []string{names, config.StringSafe(s, "level")})
|
|
}
|
|
return res
|
|
}
|
|
|
|
// ToLokiConfig extracts loki config.
|
|
func ToLokiConfig(c *config.Config) loki.Config {
|
|
hostname, _ := os.Hostname()
|
|
return loki.Config{
|
|
Enabled: config.BoolSafe(c.Sub(subsection).Sub(lokiSubsection), "enabled"),
|
|
BatchWait: getBatchWait(c),
|
|
BatchEntriesNumber: getBatchEntriesNumber(c),
|
|
Endpoint: getEndpoint(c),
|
|
Labels: map[string]string{
|
|
"hostname": hostname,
|
|
},
|
|
}
|
|
}
|
|
|
|
func getBatchWait(c *config.Config) time.Duration {
|
|
v := config.DurationSafe(c.Sub(subsection).Sub(lokiSubsection), "max_batch_delay")
|
|
if v > 0 {
|
|
return v
|
|
}
|
|
|
|
return BatchWaitDefault
|
|
}
|
|
|
|
func getBatchEntriesNumber(c *config.Config) int {
|
|
v := config.IntSafe(c.Sub(subsection).Sub(lokiSubsection), "max_batch_size")
|
|
if v > 0 {
|
|
return int(v)
|
|
}
|
|
|
|
return BatchEntriesNumberDefault
|
|
}
|
|
|
|
func getEndpoint(c *config.Config) string {
|
|
v := config.StringSafe(c.Sub(subsection).Sub(lokiSubsection), "endpoint")
|
|
if v != "" {
|
|
return v
|
|
}
|
|
|
|
return AddressDefault
|
|
}
|