buildgrid.server.metrics_utils module

buildgrid.server.metrics_utils.create_counter_record(name: str, count: float, metadata: Optional[Dict] = None)buildgrid.v2.monitoring_pb2.MetricRecord
buildgrid.server.metrics_utils.create_gauge_record(name: str, value: float, metadata: Optional[Dict] = None)buildgrid.v2.monitoring_pb2.MetricRecord
buildgrid.server.metrics_utils.create_timer_record(name: str, duration: datetime.timedelta, metadata=None)buildgrid.v2.monitoring_pb2.MetricRecord
buildgrid.server.metrics_utils.create_distribution_record(name: str, value: float, metadata=None)buildgrid.v2.monitoring_pb2.MetricRecord
buildgrid.server.metrics_utils.publish_counter_metric(name: str, count: float, metadata: Optional[Dict] = None)None
buildgrid.server.metrics_utils.publish_gauge_metric(name: str, value: float, metadata: Optional[Dict] = None)None
buildgrid.server.metrics_utils.publish_timer_metric(name: str, duration: datetime.timedelta, metadata: Optional[Dict] = 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
property instance_name
buildgrid.server.metrics_utils.generator_method_duration_metric(name: str)

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: Optional[str] = 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
property metric_name
property instance_name
increment(value: float = 1.0)None
publish(reset_counter=True)None
class buildgrid.server.metrics_utils.ExceptionCounter(metric_name: str, *args, exceptions: Tuple[Type[Exception], ...] = (<class 'buildgrid._exceptions.BgdError'>,), **kwargs)

Bases: buildgrid.server.metrics_utils.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,)) as ec:

ret_val = do_work()

buildgrid.server.metrics_utils.generator_method_exception_counter(name: str, exceptions: Tuple[Type[Exception]] = (<class 'buildgrid._exceptions.BgdError'>, ))

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.

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

Bases: buildgrid.server.metrics_utils.Counter

Provides a generic metric using Distribution semantics

publish(reset=True)None