Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take control over Kopia log level #2842

Merged
merged 7 commits into from
May 15, 2024
Merged

Take control over Kopia log level #2842

merged 7 commits into from
May 15, 2024

Conversation

e-sumin
Copy link
Contributor

@e-sumin e-sumin commented Apr 23, 2024

Change Overview

When building Kopia command line take LogLevel and FileLogLevel from env variables if it not specified in parameters

Pull request type

Please check the type of change your PR introduces:

  • 🚧 Work in Progress
  • 🌈 Refactoring (no functional changes, no api changes)
  • 🐹 Trivial/Minor
  • πŸ› Bugfix
  • 🌻 Feature
  • πŸ—ΊοΈ Documentation
  • πŸ€– Test

Issues

  • fixes #issue-number

Test Plan

  • πŸ’ͺ Manual
  • ⚑ Unit test
  • πŸ’š E2E

@infraq infraq added this to In Progress in Kanister Apr 23, 2024
…env variables if not specified in parameters
Copy link
Contributor

@pavannd1 pavannd1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add these flags to one of the command unit tests we have?

@e-sumin e-sumin requested a review from pavannd1 May 9, 2024 00:00
@e-sumin
Copy link
Contributor Author

e-sumin commented May 9, 2024

Is it possible to add these flags to one of the command unit tests we have?

Good idea. I've added new unit test for common args builder.

Copy link
Contributor

@hairyhum hairyhum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Kanister automation moved this from In Progress to Reviewer approved May 9, 2024
func FileLogLevel() string {
return os.Getenv(FileLogLevelVarName)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Eugen,

First off, thanks for log flags! I've missed them recently (and hard-coded locally for tests) :).

What do you think about encapsulating the defaulting logic within envvar utility funcs?
For example, we could use a generic function like nonEmptyOrDefault to handle defaults more elegantly.

Here’s an example of what this might look like:

const (
...
    DefaultLogLevel     = "info"
    DefaultFileLogLevel = "debug"
)

func nonEmptyOrDefault[T comparable](t T, def T) T {
    var empty T
    if t != empty {
        return t
    }
    return def
}

func getEnv(name, def string) string {
    return nonEmptyOrDefault(os.Getenv(name), def)
}

func LogLevel() string {
    return getEnv(LogLevelVarName, DefaultLogLevel)
}

func FileLogLevel() string {
    return getEnv(FileLogLevelVarName, DefaultFileLogLevel)
}

and after we can refactor commonArgs as:

OLD

        ...
	logLevel := cmdArgs.LogLevel
	if logLevel == "" {
		logLevel = LogLevel()
	}

	if logLevel != "" {
		c = c.AppendLoggableKV(logLevelFlag, logLevel)
	} else {
		c = c.AppendLoggableKV(logLevelFlag, LogLevelError)
	}

	fileLogLevel := cmdArgs.FileLogLevel
	if fileLogLevel == "" {
		fileLogLevel = FileLogLevel()
	}

	if fileLogLevel != "" {
		c = c.AppendLoggableKV(fileLogLevelFlag, fileLogLevel)
	}
	...

NEW

        ...
	logLevel := nonEmptyOrDefault(cmdArgs.LogLevel, LogLevel())
	c = c.AppendLoggableKV(logLevelFlag, logLevel)

	fileLogLevel := nonEmptyOrDefault(cmdArgs.FileLogLevel, FileLogLevel())
	c = c.AppendLoggableKV(fileLogLevelFlag, fileLogLevel)
	...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accepted, done.

Comment on lines 26 to 29
// LogLevelVarName is the environment variable that controls Kopia log level.
LogLevelVarName = "KOPIA_LOG_LEVEL"
// FileLogLevelVarName is the environment variable that controls Kopia file log level.
FileLogLevelVarName = "KOPIA_FILE_LOG_LEVEL"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are publishing these flags to users, IMO we shouldn't use KOPIA term. We can use generic a term like DATA_STORE. Users don't need to know about what tool we are using for data uploads. @pavannd1 @hairyhum @viveksinghggits wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I remember we discussed something like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing, renamed.

Comment on lines 27 to 29
LogLevelVarName = "KOPIA_LOG_LEVEL"
// FileLogLevelVarName is the environment variable that controls Kopia file log level.
FileLogLevelVarName = "KOPIA_FILE_LOG_LEVEL"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my understand Eugen, can you please very briefly talk about what is the difference between Log Level and File Log Level?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for @hairyhum for link.

Just to have an answer right here:

You can control how much data is written to console and log files by using flags:

--log-level - sets log level for console output (defaults to info)
--file-log-level - sets log level for file output (defaults to debug)

)

func LogLevel() string {
return os.Getenv(LogLevelVarName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this environment variable set? should we default to something if this env var is not set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In updated code, default value is provided.

@@ -40,12 +58,26 @@ func stringSliceCommand(args logsafe.Cmd) []string {
func commonArgs(cmdArgs *CommandArgs) logsafe.Cmd {
c := logsafe.NewLoggable(kopiaCommand)

if cmdArgs.LogLevel != "" {
c = c.AppendLoggableKV(logLevelFlag, cmdArgs.LogLevel)
logLevel := cmdArgs.LogLevel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think this requires change in the consumers of this function for example SnapshotCreate etc? Are we planning to do that in other PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not required.

Behavior before change:
Either level from var OR error

Behavior after change:
Either level from var OR level from env OR error

Taking level from env is needed only when debugging.
I went through the invokers and LogLevel is not set there. But if it will be set there to some value, I believe, decision of priority between env and their value should be done on their side.

@e-sumin e-sumin added the kueue label May 15, 2024
@mergify mergify bot merged commit 58b441e into master May 15, 2024
15 checks passed
Kanister automation moved this from Reviewer approved to Done May 15, 2024
@mergify mergify bot deleted the add-kopia-file-log-level branch May 15, 2024 13:47
Copy link
Contributor

@julio-lopez julio-lopez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@e-sumin
+1: LG πŸ‘ Thanks for doing this.

A couple of minor comments / questions.


var _ = check.Suite(&CommonUtilsSuite{})

func (s *CommonUtilsSuite) TestCommonArgs(c *check.C) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests do not need to use check.v1 and could be built using the support in the plain Go testing package.

Since this is (was) a new test, wouldn't it make sense to avoid check.v1 here?

}, {
setup: func() func() {
origLogLevel := os.Getenv(LogLevelVarName)
os.Setenv(LogLevelVarName, "debug")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be good to use testing.T.Setenv here to simplify the setup and make the test more robust, but that requires having access to testing.T.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging this pull request may close these issues.

None yet

7 participants