class MyActor: def __init__(self, name): self._curr_count = 0 self.counter = Counter( "num_requests", description="Number of requests processed by the actor.", tag_keys=("actor_name", )) self.counter.set_default_tags({"actor_name": name}) self.gauge = Gauge( "curr_count", description="Current count held by the actor. Goes up and down.", tag_keys=("actor_name", )) self.gauge.set_default_tags({"actor_name": name}) self.histogram = Histogram("request_latency", description="Latencies of requests in ms.", boundaries=[0.1, 1], tag_keys=("actor_name", )) self.histogram.set_default_tags({"actor_name": name}) def process_request(self, num): start = time.time() self._curr_count += num # Increment the total request count. self.counter.inc() # Update the gauge to the new value. self.gauge.set(self._curr_count) # Record the latency for this request in ms. self.histogram.observe(1000 * (time.time() - start)) return self._curr_count
def test_custom_metrics_info(metric_mock): # Make sure .info public method works. histogram = Histogram( "hist", description="hist", boundaries=[1.0, 2.0], tag_keys=("a", "b")) assert histogram.info["name"] == "hist" assert histogram.info["description"] == "hist" assert histogram.info["boundaries"] == [1.0, 2.0] assert histogram.info["tag_keys"] == ("a", "b") assert histogram.info["default_tags"] == {} histogram.set_default_tags({"a": "a"}) assert histogram.info["default_tags"] == {"a": "a"}