def test2(): ''' Use with context manager ''' with cat.Transaction("Transaction", "test2") as trans: trans.set_status(cat.CAT_ERROR) cat.log_event("h", "v") trans.add_data("a=1") trans.add_data("b=2")
def serve(method, args): cat.log_event("hook", "receive") with cat.Transaction("serve", method) as t: try: t.add_data(json.dumps(args)) cat.log_event("hook", "before1") cat.log_event("hook", "before2") cat.log_event("hook", "before3") res = func(method, args) finally: cat.metric("rpc-count").count() cat.metric("rpc-duration").duration(100) cat.log_event("hook", "after") return res
def test2(): ''' Use via context manager ''' def do_something(): import random if random.random() < 0.1: raise Exception("error occured!") with cat.Transaction("Trans", "T2") as t: cat.log_event("Event", "E2") try: do_something() except Exception: t.set_status(cat.CAT_ERROR) t.add_data("context-manager") t.add_data("foo", "bar")
def test1(): # 正常用法,默认是成功状态 cat.log_event("Event", "E1") # 可以提供第三个参数,只要是非 0 的 string 类型都是失败 cat.log_event("Event", "E2", cat.CAT_ERROR) cat.log_event("Event", "E3", "failed") # 可以提供第四个参数,增加 debug 信息 cat.log_event("Event", "E4", "failed", "some debug info")
def log_event(space, name): name = '%s/%s' % (space, name) cat.log_event(space, name)