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

Add log hander attributes for retrieving the OS user and group #337

Merged
merged 4 commits into from
Jun 21, 2018

Conversation

victorusu
Copy link
Contributor

This PR attempts to add the username and group to perf logging.
This is especially good for grafana logging.

This should capture the name and primary group of the user that has
executed reframe.

REMARKS:

  • capturing the username and group is not portable across OSes
  • The fields were updated inside the _update_check_extras, but they
    could be set during the initialization of the LoggerAdapter.

This PR attempts to add the username and group to perf logging.
This is especially good for grafana logging.

This should capture the name and primary group of the user that has
executed reframe.

REMARKS:
* capturing the username and group is not portable across OSes
* The fields were updated inside the `_update_check_extras`, but they
could be set during the initialization of the `LoggerAdapter`.
@@ -343,6 +343,9 @@ def __init__(self, logger=None, check=None):
'check_perf_ref': None,
'check_perf_lower_thres': None,
'check_perf_upper_thres': None,
'check_perf_upper_thres': None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is the check_perf_upper_thres two times here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

copy and paste issues... sorry! 😯

@vkarak vkarak added this to the ReFrame sprint 2018w23 milestone Jun 20, 2018
@codecov-io
Copy link

codecov-io commented Jun 20, 2018

Codecov Report

Merging #337 into master will decrease coverage by 0.03%.
The diff coverage is 76.47%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #337      +/-   ##
==========================================
- Coverage   91.13%   91.09%   -0.04%     
==========================================
  Files          68       68              
  Lines        8231     8244      +13     
==========================================
+ Hits         7501     7510       +9     
- Misses        730      734       +4
Impacted Files Coverage Δ
reframe/frontend/printer.py 86.66% <100%> (ø) ⬆️
reframe/core/logging.py 84.24% <100%> (+0.05%) ⬆️
reframe/frontend/cli.py 78.57% <100%> (ø) ⬆️
reframe/utility/os_ext.py 90% <69.23%> (-2.38%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4185d5f...10e3694. Read the comment docs.

@@ -378,6 +380,24 @@ def _update_check_extras(self):
if self.check.job:
self.extra['check_jobid'] = self.check.job.jobid

try:
import pwd
self.extra['username'] = pwd.getpwuid(os.geteuid()).pw_name
Copy link
Contributor

Choose a reason for hiding this comment

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

@vkarak is it better to get the euid or the uid?

Copy link
Contributor

@vkarak vkarak left a comment

Choose a reason for hiding this comment

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

I think the implementation is overly complicated.

@@ -343,6 +343,8 @@ def __init__(self, logger=None, check=None):
'check_perf_ref': None,
'check_perf_lower_thres': None,
'check_perf_upper_thres': None,
'username': None,
'group': None,
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest those being osuser and osgroup.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also we need to document those in the public documentation.

try:
import getpass
self.extra['username'] = getpass.getuser()
except:
Copy link
Contributor

Choose a reason for hiding this comment

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

Bare except clauses are disallowed by PEP8. Either be more precise or catch Exception or BaseException (not this case).

gid = pwd.getpwnam(self.extra['username']).pw_gid
self.extra['group'] = grp.getgrgid(gid).gr_name
except:
pass
Copy link
Contributor

Choose a reason for hiding this comment

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

The whole thing here is overly complicated. I suggest the following:

try:
    user = getpass.getuser()
except Exception:
    user = '<unknown>'

try:
    group = grp.getgrgid(os.getgid()).gr_name
except KeyError:
    group = '<unknown>'

self.extra['osuser'] = user
self.extra['osgroup'] = group

The getpass() does already use pwd under the hood if necessary.

We would the extreme way proposed here only if we had actually to care about effective and real user ids. But (a) I don't think we have to and (b) if that was the case, this would have to go in utility.os_ext.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also @victorusu, I indeed think this should not go in the _update_check_extras. It should be in the LoggerAdapter, but the I suggest moving this query to the os_ext.utility as osuser() and osgroup() functions. These functions should return the user and group name or None, so that inside the LoggerAdapter you would do the following:

                'osuser': os_ext.osuser() or '<unknown>',
                'osgroup': os_ext.osgroup() or '<unknown>',

Copy link
Contributor

Choose a reason for hiding this comment

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

There are a couple of places in the framework that we are using os.environ['USER'] to retrieve the current user. We should replace these with the new utility function. Sorry, you asked for it! 😛

@vkarak vkarak self-assigned this Jun 21, 2018
@vkarak vkarak changed the title Adding username and group to perf logging Add log hander attributes for retrieving the OS user and group Jun 21, 2018
@vkarak vkarak merged commit 167e020 into master Jun 21, 2018
@vkarak vkarak deleted the feature/add-username-usergroup-to-perf-logger branch June 21, 2018 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants