Decorators

base

This module contains a Metlog decorator base class and some additional helper code. The primary reason for these abstractions is ‘deferred configuration’. Decorators are evaluated by Python at import time, but often the configuration needed for a Metlog client, which might negate (or change) the behavior of a Metlog decorator, isn’t available until later, after some config parsing code has executed. This code provides a mechanism to have a function get wrapped in one way (or not at all) when the decorator is originally evaluated, but then to be wrapped differently once the config has loaded and the desired final behavior has been established.

class metlog.decorators.base.MetlogDecorator(*args, **kwargs)[source]

This is a base class for Metlog decorators, designed to support ‘rebinding’ of the actual decorator method once Metlog configuration has actually been loaded. The first time the decorated function is invoked, the predicate method will be called. If the result is True, then metlog_call (intended to be implemented by subclasses) will be used as the decorator. If the predicate returns False, then _invoke (which by default does nothing but call the wrapped function) will be used as the decorator.

metlog_call(*args, **kwargs)[source]

Actual metlog activity happens here. Implemented by subclasses.

predicate()[source]

Called during the rebind process. True return value will rebind such that self.metlog_call becomes the decorator function, False will rebind such that self._invoke becomes the decorator function.

set_fn(fn)[source]

Sets the function and stores the full dotted notation fn name for later use.

Parameters:fn – Actual function that we are decorating.

util

metlog.decorators.util.return_fq_name(func, klass=None)[source]

Resolve a fully qualified name for a function or method

stats

class metlog.decorators.stats.incr_count(*args, **kwargs)[source]

Lazily decorate any callable w/ a wrapper that will increment a metlog counter whenever the callable is invoked.

class metlog.decorators.stats.timeit(*args, **kwargs)[source]

Lazily decorate any callable with a metlog timer.