def test_replace_instrument(self): with patch("diagnose.instruments.statsd") as statsd: mgr = diagnose.manager try: target1 = "diagnose.test_fixtures.a_func" mgr.specs["a_func"] = spec = { "target": target1, "instrument": { "type": "hist", "name": "a_func", "value": "arg", "event": "return", "custom": {}, }, "lifespan": 1, "lastmodified": datetime.datetime.utcnow(), "applied": {}, } mgr.apply() result = a_func(100) # The call MUST succeed. assert result == 113 # The instrument MUST have logged an entry assert statsd.method_calls == [ call.histogram("a_func", 100, tags=[]) ] # Change the probe to a different target target2 = "diagnose.test_fixtures.Thing.do" spec["target"] = target2 mgr.apply() assert (next( iter(probes.active_probes[target2].instruments.values())). name == "a_func") # The old target MUST be removed from the probes assert target1 not in probes.active_probes # Trigger the (revised) probe result = Thing().do(2) # The call MUST succeed. assert result == "<2>" # The instrument MUST have logged an entry assert statsd.method_calls == [ call.histogram("a_func", 100, tags=[]), call.histogram("a_func", 2, tags=[]), ] finally: mgr.specs.pop("a_func", None) mgr.apply()
def test_multiple_instruments(self): with patch("diagnose.instruments.statsd") as statsd: mgr = diagnose.manager try: target = "diagnose.test_fixtures.a_func" mgr.specs["a_func_end"] = { "target": target, "instrument": { "type": "hist", "name": "a_func_end", "value": "output", "event": "end", "custom": {}, }, "lifespan": 1, "lastmodified": datetime.datetime.utcnow(), "applied": {}, } mgr.specs["a_func_return"] = { "target": target, "instrument": { "type": "hist", "name": "a_func_return", "value": "result", "event": "return", "custom": {}, }, "lifespan": 1, "lastmodified": datetime.datetime.utcnow(), "applied": {}, } mgr.apply() result = a_func(78) finally: mgr.specs.pop("a_func_end", None) mgr.specs.pop("a_func_return", None) mgr.apply() # The call MUST succeed. assert result == 91 # The instruments MUST each have logged an entry assert statsd.method_calls == [ call.histogram("a_func_end", 91, tags=[]), call.histogram("a_func_return", 91, tags=[]), ]
def test_hist_instrument(self): with patch("diagnose.instruments.statsd") as statsd: with self.probe("hist", "grr", "diagnose.test_fixtures.Thing.do", "len(arg)"): result = Thing().do("ok") # The call MUST succeed. assert result == "<ok>" # The probe MUST have called for a histogram assert statsd.method_calls[0] == call.histogram("grr", 2, tags=[])
def test_hist_tags(self): NUM = 496942560 with patch("diagnose.instruments.statsd") as statsd: with self.probe( "hist", "baz", "diagnose.test_fixtures.a_func", "result", custom={"tags": "{'output': arg}"}, ): assert a_func(NUM) == NUM + 13 # The probe MUST have called for a histogram with our custom tags assert statsd.method_calls[0] == call.histogram( "baz", NUM + 13, tags=["output:%s" % NUM])