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

refactor: Improved logger usage #1373

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

Venefilyn
Copy link
Member

This refactor of the usage of our logging is to get away from custom
functions that causes attribute errors in editors and IDEs. This will
also standarize the usage of logger and the loglevels.

In future PRs we can include changes to include prepare, rollback, and
final specific message styles.

Jira Issues:

Depends on

Checklist

  • PR has been tested manually in a VM (either author or reviewer)
  • Jira issue has been made public if possible
  • [RHELC-] or [HMS-] is part of the PR title
  • GitHub label has been added to help with Release notes
  • PR title explains the change from the user's point of view
  • Code and tests are documented properly
  • The commits are squashed to as few commits as possible (without losing data)
  • When merged: Jira issue has been updated to Release Pending if relevant

convert2rhel/logger.py Fixed Show fixed Hide fixed
@@ -136,6 +136,7 @@ def main_locked():
initialize_file_logging("convert2rhel.log", logger_module.LOG_DIR)

try:
logger_module.ConversionStage.set_stage("prepare")
Copy link
Member

Choose a reason for hiding this comment

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

What about using ConversionPhase for this? I think it could be really good to map the phases in that class to the ConversionStage you created

Copy link
Member Author

@Venefilyn Venefilyn Sep 12, 2024

Choose a reason for hiding this comment

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

@r0x0d That's a good point. Though that made me dig down a rabbit hole of refactoring to be a bit nicer to use for more scenarios. What do you think of this?

class ConversionPhase:
    """Conversion phase to hold name and logging-friendly name.
    """

    def __init__(self, name, log_name=None):  # type: (str, str|None) -> None
        self.name = name
        self.log_name = log_name

    def __str__(self):
        return self.log_name

    def __repr__(self):
        return self.name

    def __hash__(self) -> int:
        return hash(self.name)


class ConversionPhases(dict):
    """During conversion we will be in different states depending on
    where we are in the execution. This class establishes the different phases
    that we have as well as what the current phase is set to.
    """
    _phases = [
        ConversionPhase(name="POST_CLI", log_name="Prepare"),
        ConversionPhase(name="PRE_PONR_CHANGES", log_name="Prepare")
    ]

    current_phase = None  # type: ConversionPhase|None

    def __getitem__(self, key):  # type: (str) -> ConversionPhase|None
        return next(phase for phase in self._phases if phase.name == key )

    @classmethod
    def set_stage(cls, phase):  # type: (str) -> None
        if phase is None or phase in cls._phases:
            cls.current_phase = cls._phases[phase]
        raise NotImplementedError("The stage {} is not implemented in the ConversionStage class".format(phase))

test = ConversionPhases()

print(test["POST_CLI"].__repr__)
$ python test.py
<bound method ConversionPhase.__repr__ of POST_CLI>

Copy link
Member

Choose a reason for hiding this comment

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

Hmmm, this work as well, but it is easy to introduce an error if we miss the correct naming in other parts of the code or introduce a new phase and forget about it.

I was thinking in something more simple, like this:

class ConversionPhase:
    POST_CLI = 1
    # PONR means Point Of No Return
    PRE_PONR_CHANGES = 2
    # Phase to exit the Analyze SubCommand early
    ANALYZE_EXIT = 3
    POST_PONR_CHANGES = 4

class ConversionStage:
    stages = {
        ConversionPhase.PRE_PONR_CHANGES: "Prepare",
        ConversionPhase.POST_PONR_CHANGES: "Final",
        ...
    }
    current = None  # type: str|None

    @classmethod
    def set_stage(cls, stage):  # type: (str) -> None
        if stage == None or stage in cls.stages:
            cls.current = cls.stages[stage]
        raise NotImplementedError("The stage {} is not implemented in the ConversionStage class".format(stage))

Then, when we initialize the ConversionStage, we could just simply:

logger_module.ConversionStage.set_stage(ConversionPhase.PRE_PONR_CHANGES)

And then we have it mapped everywhere that PRE_PONR_CHANGES matches the Prepare or something else. The interface will still look clean enough in the new class, and we will make sure that if someone changes the meaning for any of those phases, we will catch somewhere as well.

Since our use case is so small for now, I think that reusing the phases from ConversionPhase should be good enough.

Copy link
Member

Choose a reason for hiding this comment

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

I think our ConversionPhase class should hold the information where we are in the process at all times, being our source of truth for many different operations. It is true that logging phases may differ from execution phases, but if that's really the case, then we could come up with an intermediate class that will hold the logging phases for us, tied to the conversion phase if necessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah I didn't see this as I got hyperfocused on overcomplicating the solution... but anyhow I did change it now to be a bit safer but in a way that means we still rely on strings. But given exceptions being raised we should catch it in tests if we do a mistake

# Phase to exit the Analyze SubCommand early
ANALYZE_EXIT = ConversionPhase(name="ANALYZE_EXIT")
POST_PONR_CHANGES = ConversionPhase(name="POST_PONR_CHANGES", log_name="Convert")
ROLLBACK = (ConversionPhase(name="ROLLBACK", log_name="Rollback"),)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ROLLBACK = (ConversionPhase(name="ROLLBACK", log_name="Rollback"),)
ROLLBACK = ConversionPhase(name="ROLLBACK", log_name="Rollback")

Copy link

codecov bot commented Sep 18, 2024

Codecov Report

Attention: Patch coverage is 79.72973% with 30 lines in your changes missing coverage. Please review.

Project coverage is 96.07%. Comparing base (0cad386) to head (03f291c).
Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
convert2rhel/phase.py 57.69% 18 Missing and 4 partials ⚠️
convert2rhel/main.py 82.35% 2 Missing and 4 partials ⚠️
convert2rhel/logger.py 66.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1373      +/-   ##
==========================================
- Coverage   96.58%   96.07%   -0.51%     
==========================================
  Files          71       72       +1     
  Lines        5090     5151      +61     
  Branches      880      893      +13     
==========================================
+ Hits         4916     4949      +33     
- Misses         98      119      +21     
- Partials       76       83       +7     
Flag Coverage Δ
centos-linux-7 91.57% <78.47%> (-0.46%) ⬇️
centos-linux-8 92.43% <79.16%> (-0.47%) ⬇️
centos-linux-9 92.56% <79.72%> (-0.47%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Venefilyn Venefilyn added the tests/sanity PR ready to run the sanity test suit. Equivalent to `/packit test --labels sanity`. label Sep 19, 2024
@has-bot
Copy link
Member

has-bot commented Sep 19, 2024

/packit test --labels sanity


Comment generated by an automation.

@Venefilyn
Copy link
Member Author

/packit test --labels sanity

2 similar comments
@Venefilyn
Copy link
Member Author

/packit test --labels sanity

@Venefilyn
Copy link
Member Author

/packit test --labels sanity

@Venefilyn Venefilyn force-pushed the refactor/logger-stages branch 2 times, most recently from 00ec140 to e7f77f1 Compare September 20, 2024 14:32
@Venefilyn
Copy link
Member Author

/packit test --labels sanity

@Venefilyn
Copy link
Member Author

/packit build

@Venefilyn
Copy link
Member Author

/packit test --labels sanity

@Venefilyn Venefilyn marked this pull request as ready for review September 23, 2024 09:31
@Venefilyn Venefilyn requested a review from a team as a code owner September 23, 2024 09:31
@Venefilyn
Copy link
Member Author

/packit test --labels sanity

"""

POST_CLI = ConversionPhase(name="POST_CLI")
PREPARE = ConversionPhase(name="PREPARE", log_name="Prepare")
Copy link
Member

Choose a reason for hiding this comment

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

I just wonder, when we are changing the logging and the final is renamed to convert (which makes sense to me) - what about changing the Prepare to Analysis (or something like that)? I think it would be nice to have it matching the subcommands (analyze and convert).

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed and I had that before, but I wanted minimal changes in this PR to the actual logs so wanted it to remain the same. We can change it in a follow-up PR but requires a bit more discussion IMO

Copy link
Member

Choose a reason for hiding this comment

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

Okay. Maybe if we want minimal changes, then I would change the name of the post_conversion to the Final to be consistent

@hosekadam
Copy link
Member

/packit test --labels sanity

Copy link
Member

@hosekadam hosekadam left a comment

Choose a reason for hiding this comment

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

Looks good, just wait till the infrastructure problems are resolved and try the sanity tests.

@hosekadam
Copy link
Member

/packit test --labels sanity

Signed-off-by: Freya Gustavsson <freya@venefilyn.se>
This refactors the logging stages that we output when we log in specific
files depending on what we are doing. Meaning that any changes that
has to do with preparing or rollback will now output the stage within
the logger formatter rather than leaving it up to each class

E.g. any logs with "Prepare: text" or "Rollback: text" will now be
handled at the log-level

Signed-off-by: Freya Gustavsson <freya@venefilyn.se>
Signed-off-by: Freya Gustavsson <freya@venefilyn.se>
Signed-off-by: Freya Gustavsson <freya@venefilyn.se>
Signed-off-by: Freya Gustavsson <freya@venefilyn.se>
This removes all the different task log calls that include the stage
it should be in and instead relies on the logger to take care of it

e.g. `.task("Convert: ...")` will now have `Convert:` removed

Signed-off-by: Freya Gustavsson <freya@venefilyn.se>
ConversionPhases is now moved outside of utils module so that it doesn't
cause a circular import with logger module

Signed-off-by: Freya Gustavsson <freya@venefilyn.se>
Signed-off-by: Freya Gustavsson <freya@venefilyn.se>
Signed-off-by: Freya Gustavsson <freya@spytec.se>
Signed-off-by: Freya Gustavsson <freya@venefilyn.se>
Signed-off-by: Freya Gustavsson <freya@venefilyn.se>
Signed-off-by: Freya Gustavsson <freya@venefilyn.se>
We didn't know what phase we were on before going into rollback. With
this change it now keeps a (soft) linear history where each phase points
to the previous one
@Venefilyn
Copy link
Member Author

/packit test --labels sanity

1 similar comment
@Venefilyn
Copy link
Member Author

/packit test --labels sanity

@Venefilyn Venefilyn added kind/refactor and removed skip/changelog If it should be excluded from changelog or Release notes. Such as infra, reverted PRs, etc. labels Oct 18, 2024
@Venefilyn
Copy link
Member Author

/packit test --labels sanity

1 similar comment
@Venefilyn
Copy link
Member Author

/packit test --labels sanity

@Venefilyn
Copy link
Member Author

/packit test

@Venefilyn Venefilyn requested a review from a team as a code owner October 19, 2024 18:12
@Venefilyn
Copy link
Member Author

/packit test

1 similar comment
@Venefilyn
Copy link
Member Author

/packit test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/refactor merge-after-tests-ok tests/sanity PR ready to run the sanity test suit. Equivalent to `/packit test --labels sanity`.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants