async def route_metrics(graph, request: Request, call_next): response = await call_next(request) if getattr(request.state, "request_info", None): request_info: RequestInfo = request.state.request_info key = "route" tags = [ f"endpoint:{request_info.operation}", "backend_type:microcosm_fastapi", ] if request_info.status_code is not None: graph.metrics.increment( name_for( key, "call", "count", ), tags=tags + [ f"classifier:{normalize_status_code(request_info.status_code)}" ], ) if request_info.timing.get("elapsed_time"): elapsed_ms = request_info.timing["elapsed_time"] graph.metrics.histogram( name_for(key), elapsed_ms, tags=tags, ) return response
def publish(): """ Publish a metric (for testing). """ args = parse_args() statsd = create_statsd_client(args) if args.action == "increment": statsd.increment(name_for(getuser(), args.action)) elif args.action == "histogram": statsd.histogram(name_for(getuser(), args.action), 1.0) try: # wait a little to allow the delivery of the metric before we exit sleep(1.0) except KeyboardInterrupt: pass
def wrapper(*args, **kwargs): start_time = time() try: return func(*args, **kwargs) finally: end_time = time() graph.metrics.histogram( name_for(name), end_time - start_time, tags=tags, )
def wrapper(*args, **kwargs): classifier = classifier_cls(func) try: return classifier(*args, **kwargs) finally: if classifier.label is not None: graph.metrics.increment( name_for( name, classifier.label, "count", ), tags=tags, )
def test_naming_simple(self): assert_that(name_for("foo"), is_(equal_to("foo")))
def test_naming(self): assert_that(name_for("foo", "bar"), is_(equal_to("foo.bar")))