Skip to content

Commit

Permalink
chore(log): Restructure and cleanup logging code (influxdata#15234)
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan authored May 10, 2024
1 parent e7703ae commit 71b58dd
Show file tree
Hide file tree
Showing 55 changed files with 793 additions and 744 deletions.
4 changes: 2 additions & 2 deletions agent/accumulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/models"
"github.com/influxdata/telegraf/logger"
)

func TestAddFields(t *testing.T) {
Expand Down Expand Up @@ -156,5 +156,5 @@ func (tm *TestMetricMaker) MakeMetric(metric telegraf.Metric) telegraf.Metric {
}

func (tm *TestMetricMaker) Log() telegraf.Logger {
return models.NewLogger("TestPlugin", "test", "")
return logger.NewLogger("TestPlugin", "test", "")
}
7 changes: 3 additions & 4 deletions cmd/telegraf/cmd_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"os"
"path/filepath"

"github.com/influxdata/telegraf"
"github.com/urfave/cli/v2"

"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/logger"
"github.com/influxdata/telegraf/migrations"
"github.com/urfave/cli/v2"
)

func getConfigCommands(pluginFilterFlags []cli.Flag, outputBuffer io.Writer) []*cli.Command {
Expand Down Expand Up @@ -84,8 +84,7 @@ To migrate the file 'mysettings.conf' use
},
Action: func(cCtx *cli.Context) error {
// Setup logging
telegraf.Debug = cCtx.Bool("debug")
logConfig := logger.LogConfig{Debug: telegraf.Debug}
logConfig := logger.Config{Debug: cCtx.Bool("debug")}
if err := logger.SetupLogging(logConfig); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/telegraf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
return fmt.Errorf("unknown command %q", cCtx.Args().First())
}

err := logger.SetupLogging(logger.LogConfig{})
err := logger.SetupLogging(logger.Config{})
if err != nil {
return err
}
Expand Down Expand Up @@ -372,6 +372,7 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi

// Make sure we safely erase secrets
defer memguard.Purge()
defer logger.CloseLogging()

return app.Run(args)
}
Expand All @@ -383,8 +384,7 @@ func main() {
agent := Telegraf{}
pprof := NewPprofServer()
c := config.NewConfig()
err := runApp(os.Args, os.Stdout, pprof, c, &agent)
if err != nil {
if err := runApp(os.Args, os.Stdout, pprof, c, &agent); err != nil {
log.Fatalf("E! %s", err)
}
}
9 changes: 4 additions & 5 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,13 @@ func (t *Telegraf) runAgent(ctx context.Context, c *config.Config, reloadConfig
}

// Setup logging as configured.
telegraf.Debug = c.Agent.Debug || t.debug
logConfig := logger.LogConfig{
Debug: telegraf.Debug,
logConfig := logger.Config{
Debug: c.Agent.Debug || t.debug,
Quiet: c.Agent.Quiet || t.quiet,
LogTarget: c.Agent.LogTarget,
Logfile: c.Agent.Logfile,
RotationInterval: c.Agent.LogfileRotationInterval,
RotationMaxSize: c.Agent.LogfileRotationMaxSize,
RotationInterval: time.Duration(c.Agent.LogfileRotationInterval),
RotationMaxSize: int64(c.Agent.LogfileRotationMaxSize),
RotationMaxArchives: c.Agent.LogfileRotationMaxArchives,
LogWithTimezone: c.Agent.LogWithTimezone,
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/telegraf/telegraf_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (t *Telegraf) runAsWindowsService() error {
return err
}
} else {
err = logger.SetupLogging(logger.LogConfig{LogTarget: logger.LogTargetEventlog})
err = logger.SetupLogging(logger.Config{LogTarget: "eventlog"})
if err != nil {
return err
}
Expand Down
23 changes: 12 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
logging "github.com/influxdata/telegraf/logger"
"github.com/influxdata/telegraf/models"
"github.com/influxdata/telegraf/persister"
"github.com/influxdata/telegraf/plugins/aggregators"
Expand Down Expand Up @@ -54,6 +55,9 @@ var (

// Password specified via command-line
Password Secret

// telegrafVersion contains the parsed semantic Telegraf version
telegrafVersion *semver.Version = semver.New("0.0.0-unknown")
)

// Config specifies the URL/user/password for the database that telegraf
Expand Down Expand Up @@ -86,7 +90,6 @@ type Config struct {
// like the other plugins because they need to be garbage collected (See issue #11809)

Deprecations map[string][]int64
version *semver.Version

Persister *persister.Persister

Expand Down Expand Up @@ -136,11 +139,9 @@ func NewConfig() *Config {
}

// Handle unknown version
version := internal.Version
if version == "" || version == "unknown" {
version = "0.0.0-unknown"
if internal.Version != "" && internal.Version != "unknown" {
telegrafVersion = semver.New(internal.Version)
}
c.version = semver.New(version)

tomlCfg := &toml.Config{
NormFieldName: toml.DefaultConfig.NormFieldName,
Expand Down Expand Up @@ -535,7 +536,7 @@ func (c *Config) LoadConfigData(data []byte) error {

// Warn when explicitly setting the old snmp translator
if c.Agent.SnmpTranslator == "netsnmp" {
models.PrintOptionValueDeprecationNotice(telegraf.Warn, "agent", "snmp_translator", "netsnmp", telegraf.DeprecationInfo{
PrintOptionValueDeprecationNotice("agent", "snmp_translator", "netsnmp", telegraf.DeprecationInfo{
Since: "1.25.0",
RemovalIn: "2.0.0",
Notice: "Use 'gosmi' instead",
Expand Down Expand Up @@ -871,7 +872,7 @@ func (c *Config) addSecretStore(name string, table *ast.Table) error {
return err
}

logger := models.NewLogger("secretstores", name, "")
logger := logging.NewLogger("secretstores", name, "")
models.SetLoggerOnPlugin(store, logger)

if err := store.Init(); err != nil {
Expand Down Expand Up @@ -1359,7 +1360,7 @@ func (c *Config) buildFilter(plugin string, tbl *ast.Table) (models.Filter, erro
var oldPass []string
c.getFieldStringSlice(tbl, "pass", &oldPass)
if len(oldPass) > 0 {
models.PrintOptionDeprecationNotice(telegraf.Warn, plugin, "pass", telegraf.DeprecationInfo{
PrintOptionDeprecationNotice(plugin, "pass", telegraf.DeprecationInfo{
Since: "0.10.4",
RemovalIn: "2.0.0",
Notice: "use 'fieldinclude' instead",
Expand All @@ -1369,7 +1370,7 @@ func (c *Config) buildFilter(plugin string, tbl *ast.Table) (models.Filter, erro
var oldFieldPass []string
c.getFieldStringSlice(tbl, "fieldpass", &oldFieldPass)
if len(oldFieldPass) > 0 {
models.PrintOptionDeprecationNotice(telegraf.Warn, plugin, "fieldpass", telegraf.DeprecationInfo{
PrintOptionDeprecationNotice(plugin, "fieldpass", telegraf.DeprecationInfo{
Since: "1.29.0",
RemovalIn: "2.0.0",
Notice: "use 'fieldinclude' instead",
Expand All @@ -1381,7 +1382,7 @@ func (c *Config) buildFilter(plugin string, tbl *ast.Table) (models.Filter, erro
var oldDrop []string
c.getFieldStringSlice(tbl, "drop", &oldDrop)
if len(oldDrop) > 0 {
models.PrintOptionDeprecationNotice(telegraf.Warn, plugin, "drop", telegraf.DeprecationInfo{
PrintOptionDeprecationNotice(plugin, "drop", telegraf.DeprecationInfo{
Since: "0.10.4",
RemovalIn: "2.0.0",
Notice: "use 'fieldexclude' instead",
Expand All @@ -1391,7 +1392,7 @@ func (c *Config) buildFilter(plugin string, tbl *ast.Table) (models.Filter, erro
var oldFieldDrop []string
c.getFieldStringSlice(tbl, "fielddrop", &oldFieldDrop)
if len(oldFieldDrop) > 0 {
models.PrintOptionDeprecationNotice(telegraf.Warn, plugin, "fielddrop", telegraf.DeprecationInfo{
PrintOptionDeprecationNotice(plugin, "fielddrop", telegraf.DeprecationInfo{
Since: "1.29.0",
RemovalIn: "2.0.0",
Notice: "use 'fieldexclude' instead",
Expand Down
9 changes: 5 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
logging "github.com/influxdata/telegraf/logger"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/models"
"github.com/influxdata/telegraf/persister"
Expand Down Expand Up @@ -637,7 +638,7 @@ func TestConfig_SerializerInterfaceNewFormat(t *testing.T) {
formatCfg := &cfg
formatCfg.DataFormat = format

logger := models.NewLogger("serializers", format, "test")
logger := logging.NewLogger("serializers", format, "test")

var serializer telegraf.Serializer
if creator, found := serializers.Serializers[format]; found {
Expand Down Expand Up @@ -729,7 +730,7 @@ func TestConfig_SerializerInterfaceOldFormat(t *testing.T) {
formatCfg := &cfg
formatCfg.DataFormat = format

logger := models.NewLogger("serializers", format, "test")
logger := logging.NewLogger("serializers", format, "test")

var serializer serializers.Serializer
if creator, found := serializers.Serializers[format]; found {
Expand Down Expand Up @@ -835,7 +836,7 @@ func TestConfig_ParserInterface(t *testing.T) {

expected := make([]telegraf.Parser, 0, len(formats))
for _, format := range formats {
logger := models.NewLogger("parsers", format, "parser_test_new")
logger := logging.NewLogger("parsers", format, "parser_test_new")

creator, found := parsers.Parsers[format]
require.Truef(t, found, "No parser for format %q", format)
Expand Down Expand Up @@ -1041,7 +1042,7 @@ func TestConfig_ProcessorsWithParsers(t *testing.T) {

expected := make([]telegraf.Parser, 0, len(formats))
for _, format := range formats {
logger := models.NewLogger("parsers", format, "processors_with_parsers")
logger := logging.NewLogger("parsers", format, "processors_with_parsers")

creator, found := parsers.Parsers[format]
require.Truef(t, found, "No parser for format %q", format)
Expand Down
Loading

0 comments on commit 71b58dd

Please sign in to comment.