def test_patch_property(self): old_prop = Thing.exists with self.probe("test", "quantile", "diagnose.test_fixtures.Thing.exists", "result") as p: assert Thing().exists is True assert p.instruments.values()[0].results == [True] assert Thing.exists is not old_prop assert Thing().exists is True assert Thing.exists is old_prop
def test_call_event_args(self): with self.probe("test", "do", "diagnose.test_fixtures.Thing.do", "args", event="call") as p: t = Thing() result = t.do("ok") assert result == "<ok>" # The probe MUST have logged an entry assert p.instruments.values()[0].results == [(t, "ok")]
def test_basic_sensor(self): assert probes.active_probes.get( "diagnose.test_fixtures.Thing.do") is None with sensor("diagnose.test_fixtures.Thing.do") as s: assert s.log == [] Thing().do("ok") assert s.results == ["<ok>"] assert probes.active_probes.get( "diagnose.test_fixtures.Thing.do") is None with sensor("diagnose.test_fixtures.Thing.do") as s: Thing().do("tagless") s.custom["tags"] = '{"foo": "bar"}' Thing().do("tagged") assert s.log == [([], "<tagless>"), (["foo:bar"], "<tagged>")] assert probes.active_probes.get( "diagnose.test_fixtures.Thing.do") is None
def test_probe_check_call(self): def only_some_users(probe, instrument, *args, **kwargs): valid_ids = instrument.custom.get("valid_ids", None) return kwargs.get("user_id") in valid_ids with patch("diagnose.manager.check_call", only_some_users): with self.probe( "test", "blurg", "diagnose.test_fixtures.Thing.do", "result", custom={"valid_ids": [1, 2, 3]}, ) as p: assert Thing().do("ok", user_id=2) == "<ok>" assert p.instruments.values()[0].results == ["<ok>"] assert Thing().do("not ok", user_id=10004) == "<not ok>" assert p.instruments.values()[0].results == ["<ok>"]
def test_return_event_result(self): with self.probe("test", "do", "diagnose.test_fixtures.Thing.do", "result") as p: result = Thing().do("ok") assert result == "<ok>" # The probe MUST have logged an entry assert p.instruments.values()[0].results == ["<ok>"]
def test_patch_wrapped_function_end_event(self): probe = probes.attach_to("diagnose.test_fixtures.Thing.add5") try: probe.start() instr = ProbeTestInstrument("deco", "arg1", event="end") probe.instruments["deco"] = instr Thing().add5(13) assert instr.results == [113] finally: probe.stop()
def test_return_event_elapsed(self): with self.probe("test", "do", "diagnose.test_fixtures.Thing.do", "elapsed") as p: start = time.time() result = Thing().do("ok") elapsed = time.time() - start assert result == "<ok>" # The probe MUST have logged an entry assert p.instruments.values()[0].results[0] < elapsed
def test_incr_instrument(self): with patch("diagnose.instruments.statsd") as statsd: with self.probe("incr", "klee", "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 increment assert statsd.method_calls[0] == call.increment("klee", 2, tags=[])
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_log_instrument(self): with patch("diagnose.instruments.LogInstrument.out", StringIO()) as out: with self.probe("log", "foo", "diagnose.test_fixtures.Thing.do", "len(arg)"): result = Thing().do("ok") # The call MUST succeed. assert result == "<ok>" # The probe MUST have logged an entry assert out.getvalue() == "Probe (foo)[tags=[]] = 2\n"
def test_hist_instrument_err_in_eval(self): errs = [] with patch( "diagnose.manager.handle_error", lambda probe, instrument=None: errs.append(sys.exc_info()[1]), ): with self.probe("hist", "hmm", "diagnose.test_fixtures.Thing.do", "::len(arg)"): result = Thing().do("ok") # The call MUST succeed. assert result == "<ok>" # The manager MUST have handled the error. assert [e.msg for e in errs] == ["invalid syntax"]
def test_call_event_locals(self): with self.probe( "test", "do", "diagnose.test_fixtures.Thing.do", "sorted(locals().keys())", event="call", ) as p: result = Thing().do("ok") assert result == "<ok>" # The probe MUST have logged an entry assert p.instruments.values()[0].results == [[ "arg", "args", "frame", "kwargs", "now", "self", "start" ]]
def test_call_event_elapsed(self): with self.probe("test", "do", "diagnose.test_fixtures.Thing.do", "elapsed", event="call") as p: errs = [] p.instruments.values()[0].handle_error = lambda probe: errs.append( sys.exc_info()[1].args[0] if sys.exc_info()[1].args else "") result = Thing().do("ok") assert result == "<ok>" # The probe MUST NOT have logged an entry... assert p.instruments.values()[0].results == [] # ...but the instrument MUST have handled the error: assert errs == ["name 'elapsed' is not defined"]
def test_incr_nonnumeric(self): errs = [] with patch( "diagnose.manager.handle_error", lambda probe, instrument=None: errs.append(sys.exc_info()[1]), ): with patch("diagnose.instruments.statsd"): with self.probe("incr", "klee", "diagnose.test_fixtures.Thing.do", "arg"): result = Thing().do("ok") # The call MUST succeed. assert result == "<ok>" # The manager MUST have handled the error. assert [e.args for e in errs ] == [("Cannot send non-numeric metric: ok", )]
def test_patch_staticmethod(self): with self.probe("test", "quantile", "diagnose.test_fixtures.Thing.static", "result") as p: assert Thing().static() == 15 assert p.instruments.values()[0].results == [15]