예제 #1
0
    def _update_check_extras(self):
        '''Return a dictionary with all the check-specific information.'''

        exclude_check_attrs = {
            'build_job', 'current_environ', 'current_partition',
            'current_system', 'job'
        }
        if self.check is None:
            return

        for attr, val in util.attrs(self.check).items():
            if not attr.startswith('_') and attr not in exclude_check_attrs:
                self.extra[f'check_{attr}'] = val

        self.extra['check_info'] = self.check.info()

        # Treat special cases
        if self.check.current_system:
            self.extra['check_system'] = self.check.current_system.name

        if self.check.current_partition:
            cp = self.check.current_partition.fullname
            self.extra['check_partition'] = self.check.current_partition.name

            # When logging performance references, we need only those of the
            # current system
            self.extra['check_reference'] = jsonext.dumps(
                self.check.reference.scope(cp))

        if self.check.current_environ:
            self.extra['check_environ'] = self.check.current_environ.name

        if self.check.job:
            # Create extras for job attributes
            for attr, val in util.attrs(self.check.job).items():
                if not attr.startswith('_'):
                    self.extra[f'check_job_{attr}'] = val

            # Treat aliases
            self.extra['check_jobid'] = self.extra['check_job_jobid']
            if self.check.job.completion_time:
                # Here we preformat the `check_job_completion_time`, because
                # the Graylog handler does not use a formatter
                ct = self.check.job.completion_time
                ct_formatted = _format_time_rfc3339(time.localtime(ct),
                                                    '%FT%T%:z')
                self.extra['check_job_completion_time_unix'] = ct
                self.extra['check_job_completion_time'] = ct_formatted
예제 #2
0
def test_attrs():
    class B:
        z = fields.TypedField(int)

        def __init__(self, x, y):
            self.x = x
            self.y = y

    class C(B):
        def __init__(self, x, y):
            self._x = x
            self.y = y
            self.z = 3

        def foo():
            pass

        @property
        def x(self):
            return self._x

    class D(C):
        pass

    # Test undefined descriptors are not returned
    b = B(-1, 0)
    b_attrs = util.attrs(b)
    assert b_attrs['x'] == -1
    assert b_attrs['y'] == 0
    assert 'z' not in b_attrs

    c = C(1, 2)
    c_attrs = util.attrs(c)
    assert c_attrs['x'] == 1
    assert c_attrs['y'] == 2
    assert c_attrs['z'] == 3
    assert 'foo' not in c_attrs

    # Test inherited attributes
    d = D(4, 5)
    d_attrs = util.attrs(d)
    assert d_attrs['x'] == 4
    assert d_attrs['y'] == 5
    assert d_attrs['z'] == 3
    assert 'foo' not in d_attrs