buildgrid.server.metrics_utils module

buildgrid.server.metrics_utils.create_counter_record(name: str, count: float, metadata: Dict[str, str] | None = None) MetricRecord
buildgrid.server.metrics_utils.create_gauge_record(name: str, value: float, metadata: Dict[str, str] | None = None) MetricRecord
buildgrid.server.metrics_utils.create_timer_record(name: str, duration: timedelta, metadata: Dict[str, str] | None = None) MetricRecord
buildgrid.server.metrics_utils.create_distribution_record(name: str, value: float, metadata: Dict[str, str] | None = None) MetricRecord
buildgrid.server.metrics_utils.publish_counter_metric(name: str, count: float, metadata: Dict[str, str] | None = None) None
buildgrid.server.metrics_utils.publish_gauge_metric(name: str, value: float, metadata: Dict[str, str] | None = None) None
buildgrid.server.metrics_utils.publish_timer_metric(name: str, duration: timedelta, metadata: Dict[str, str] | None = None) None
class buildgrid.server.metrics_utils.DurationMetric(metric_name: str, instance_name: str = '', instanced: bool = False)

Bases: object

Provides a decorator and a context manager to measure execution duration.

property instanced: bool
property instance_name: str
buildgrid.server.metrics_utils.generator_method_duration_metric(name: str) Callable[[Func], Func]

Helper function to publish a metric for the duration of a generator method.

This returns a decorator which publishes a duration metric which measures the execution time of the decorated generator method.

This is separate from the __call__ method of DurationMetric to keep the code in that method a bit more readable whilst still having acceptable return values, as well as to make the difference between the two approaches clear.

Usage example
class ExampleInstance:

    @generator_method_duration_metric(EXAMPLE_METHOD_DURATION_NAME)
    def example_method(self, digests, context):
        for digest in digests:
            yield self._do_something(digests)
Parameters:

name (str) – The metric name to publish the method duration under.

class buildgrid.server.metrics_utils.Counter(metric_name: str, instance_name: str | None = None)

Bases: object

Provides a generic metric counter. Optionally/Ideally used as a context manager. Example Usage:

with Counter(“count-size”) as size_counter:
for i in range(10):

size_counter.increment(i)

property count: float
property metric_name: str
property instance_name: str | None
increment(value: float = 1.0) None
publish(reset_counter: bool = True) None
class buildgrid.server.metrics_utils.ExceptionCounter(metric_name: str, *args: ~typing.Any, exceptions: ~typing.Tuple[~typing.Type[Exception], ...] = (<class 'buildgrid._exceptions.BgdError'>,), ignored_exceptions: ~typing.Tuple[~typing.Type[Exception], ...] | None = None, **kwargs: ~typing.Any)

Bases: Counter

Provides a decorator and context manager in order to count exceptions thrown in a function/method body. This class inherits from Counter, publishing a value of 1, using the base classes methods. Example Usage:

with ExceptionCounter(“test”, exceptions=(RuntimeError,), ignored_exceptions=(NotFoundError,)) as ec:

ret_val = do_work()

buildgrid.server.metrics_utils.generator_method_exception_counter(name: str, exceptions: ~typing.Tuple[~typing.Type[Exception]] = (<class 'buildgrid._exceptions.BgdError'>,), ignored_exceptions: ~typing.Tuple[~typing.Type[Exception], ...] | None = None) Callable[[Func], Func]

Helper function to publish a counter when an exception is raised by a generator method.

This returns a decorator which publishes a counter metric which measures the number of exceptions raised by the decorated generator method.

This is separate from the __call__ method of ExceptionCounter to keep the code in that method a bit more readable whilst still having acceptable return values, as well as to make the difference between the two approaches clear.

Usage example
class ExampleInstance:

    @generator_method_exception_counter(EXAMPLE_METHOD_EXCEPTION_COUNT_NAME)
    def example_method(self, digests, context):
        for digest in digests:
            yield self._do_something(digests)
Parameters:
  • name (str) – The metric name to publish the exception count under.

  • exceptions (tuple) – Tuple of Exception types to count. Defaults to BgdError.

  • ignored_exceptions (tuple) – Tuple of Exception types to ignore counting of. Defaults to None.

class buildgrid.server.metrics_utils.Distribution(metric_name: str, instance_name: str = '')

Bases: Counter

Provides a generic metric using Distribution semantics

publish(reset_counter: bool = True) None