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

feat: Add Terraform command duration to log messages #3484

Merged
merged 4 commits into from
Jun 10, 2023

Conversation

inkel
Copy link
Contributor

@inkel inkel commented Jun 4, 2023

what

This PR adds the wall clock duration of executing any Terraform command to the logs.

why

This information is useful for observability purposes.

tests

Tested the changes running Atlantis locally. Because it relies on time.Now adding tests that are not flaky is difficult.

references

@inkel inkel requested a review from a team as a code owner June 4, 2023 22:14
@github-actions github-actions bot added the go Pull requests that update Go code label Jun 4, 2023
@inkel inkel changed the title Add Terraform command duration to log messages feat: Add Terraform command duration to log messages Jun 4, 2023
@inkel inkel force-pushed the inkel/tf-cmd-log-duration branch from a081b45 to 7c60f3e Compare June 5, 2023 11:25
@@ -147,13 +149,16 @@ func (s *ShellCommandRunner) RunCommandAsync(ctx command.ProjectContext) (chan<-
// Wait for the command to complete.
err = s.cmd.Wait()

dur := time.Since(start)
Copy link
Member

Choose a reason for hiding this comment

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

Please add a unit test for this in shell_command_runner_test.go. if this doesn't exist, please create one to ensure this functionality works as expected and doesn't break in the future.

out, err := cmd.CombinedOutput()
dur := time.Since(start)
log := ctx.Log.With("duration", dur)
Copy link
Member

@nitrocode nitrocode Jun 5, 2023

Choose a reason for hiding this comment

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

@inkel
Copy link
Contributor Author

inkel commented Jun 6, 2023

Hey @nitrocode thanks for the review. I definitely agree with that it would be nice to have unit tests for this, although I'm failing to come up with "simple" ones. There are a couple things that are getting in the way:

  1. usage of time.Now; this is a known issue when trying to test things that rely on time differences. One thing that sometimes work is defining a top level var now func() time.Time = time.Now that then can be overloaded at test time just to make it return specific times and thus make any computations, however, this also uses time.Since which relies on the stdlib time.Now function, which cannot be overloaded. I suppose one option would be to not use time.Since and simply use Time.Sub, however, in that case we would be losing the checks time.Since has implemented internally.
  2. the logging infrastructure (logging.SimpleLogging) lacks an easy way to indicate where the logs should be written to. There is SimpleLogging.GetHistory, however, that is marked for removal in a TODO comment. Even if that won't be the case, the only possible check would be to try and match over the whole logs history and not particular entries. And also the returned history is only for the passed down log messages, not any contextual values.
  3. related to the one above, there is a MockSimpleLogging which should help with checking that certain calls and their arguments are being invoked, however, in that case we would end up testing implementation and not functionality. And even in this case we go back to the first point in where we can test that we are calling SimpleLogging.With with a "duration" key but then we cannot really test that the time.Duration passed is right.

I'll continue to investigate what it's possible to do, tho, because I believe having tests is really useful.

@nitrocode nitrocode added waiting-on-response Waiting for a response from the user needs tests Change requires tests labels Jun 8, 2023
@inkel
Copy link
Contributor Author

inkel commented Jun 8, 2023

Hey @nitrocode I've added a test in shell_command_runner_test.go, let me know what you think and I can port it to the other file!

@nitrocode
Copy link
Member

Nice job @inkel . Yes, please add a test for the terraform client too. These tests will help preserve the feature you're adding.

@inkel
Copy link
Contributor Author

inkel commented Jun 9, 2023

@nitrocode done!

inkel added 4 commits June 9, 2023 18:34
Closes runatlantis#3413.

Signed-off-by: Leandro López (inkel) <[email protected]>
Signed-off-by: Leandro López (inkel) <[email protected]>
@inkel inkel force-pushed the inkel/tf-cmd-log-duration branch from 2b09c44 to 19efc41 Compare June 9, 2023 21:34
@nitrocode
Copy link
Member

Thank you!

@nitrocode nitrocode removed waiting-on-response Waiting for a response from the user needs tests Change requires tests labels Jun 10, 2023
@nitrocode nitrocode added this to the v0.24.3 milestone Jun 10, 2023
@nitrocode nitrocode merged commit c709fd0 into runatlantis:main Jun 10, 2023
mtavaresmedeiros pushed a commit to mtavaresmedeiros/atlantis that referenced this pull request Jul 3, 2023
* Log duration of Terraform commands

Closes runatlantis#3413.

Signed-off-by: Leandro López (inkel) <[email protected]>

* Make log message casing consistent

Signed-off-by: Leandro López (inkel) <[email protected]>

* Add tests for passing duration to Terraform comamnds logs

Signed-off-by: Leandro López (inkel) <[email protected]>

* Add logging mocks for testing Terraform client

Signed-off-by: Leandro López (inkel) <[email protected]>

---------

Signed-off-by: Leandro López (inkel) <[email protected]>
@ndavies-om1
Copy link

this does exactly what I want! Thank you!!

ijames-gc pushed a commit to gocardless/atlantis that referenced this pull request Feb 13, 2024
* Log duration of Terraform commands

Closes runatlantis#3413.

Signed-off-by: Leandro López (inkel) <[email protected]>

* Make log message casing consistent

Signed-off-by: Leandro López (inkel) <[email protected]>

* Add tests for passing duration to Terraform comamnds logs

Signed-off-by: Leandro López (inkel) <[email protected]>

* Add logging mocks for testing Terraform client

Signed-off-by: Leandro López (inkel) <[email protected]>

---------

Signed-off-by: Leandro López (inkel) <[email protected]>
ijames-gc pushed a commit to gocardless/atlantis that referenced this pull request Feb 13, 2024
* Log duration of Terraform commands

Closes runatlantis#3413.

Signed-off-by: Leandro López (inkel) <[email protected]>

* Make log message casing consistent

Signed-off-by: Leandro López (inkel) <[email protected]>

* Add tests for passing duration to Terraform comamnds logs

Signed-off-by: Leandro López (inkel) <[email protected]>

* Add logging mocks for testing Terraform client

Signed-off-by: Leandro López (inkel) <[email protected]>

---------

Signed-off-by: Leandro López (inkel) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go Pull requests that update Go code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add plan and apply durations to atlantis log messages
3 participants