[#740] logs: Add Loki

Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
Alexander Chuprov 2023-11-13 11:41:11 +03:00 committed by Evgenii Stratonikov
parent c1ec6e33b4
commit 364f835b7e
4 changed files with 61 additions and 3 deletions

View file

@ -62,6 +62,7 @@ import (
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/state"
"git.frostfs.info/TrueCloudLab/frostfs-observability/logging/lokicore"
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
@ -72,6 +73,7 @@ import (
"github.com/panjf2000/ants/v2"
"go.etcd.io/bbolt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
)
@ -577,6 +579,12 @@ func initCfg(appCfg *config.Config) *cfg {
logPrm.SamplingHook = c.metricsCollector.LogMetrics().GetSamplingHook()
log, err := logger.NewLogger(logPrm)
fatalOnErr(err)
if loggerconfig.ToLokiConfig(appCfg).Enabled {
log.Logger = log.Logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
lokiCore := lokicore.New(core, loggerconfig.ToLokiConfig(appCfg))
return lokiCore
}))
}
c.internals = initInternals(appCfg, log)

View file

@ -1,12 +1,21 @@
package loggerconfig
import (
"os"
"time"
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-node/config"
"git.frostfs.info/TrueCloudLab/frostfs-observability/logging/lokicore/loki"
)
const (
// LevelDefault is a default logger level.
LevelDefault = "info"
subsection = "logger"
lokiSubsection = "loki"
AddressDefault = "localhost:3100"
BatchEntriesNumberDefault = 100
BatchWaitDefault = time.Second
)
// Level returns the value of "level" config parameter
@ -15,7 +24,7 @@ const (
// Returns LevelDefault if the value is not a non-empty string.
func Level(c *config.Config) string {
v := config.StringSafe(
c.Sub("logger"),
c.Sub(subsection),
"level",
)
if v != "" {
@ -24,3 +33,44 @@ func Level(c *config.Config) string {
return LevelDefault
}
// 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
}

2
go.mod
View file

@ -5,7 +5,7 @@ go 1.20
require (
git.frostfs.info/TrueCloudLab/frostfs-api-go/v2 v2.16.1-0.20231031104748-498877e378fd
git.frostfs.info/TrueCloudLab/frostfs-contract v0.18.1-0.20231102065436-9ed3845aa989
git.frostfs.info/TrueCloudLab/frostfs-observability v0.0.0-20230531082742-c97d21411eb6
git.frostfs.info/TrueCloudLab/frostfs-observability v0.0.0-20231101111734-b3ad3335ff65
git.frostfs.info/TrueCloudLab/frostfs-sdk-go v0.0.0-20231101144515-6fbe1595cb3d
git.frostfs.info/TrueCloudLab/hrw v1.2.1
git.frostfs.info/TrueCloudLab/policy-engine v0.0.0-20231101082425-5eee1a733432

BIN
go.sum

Binary file not shown.