Esempio n. 1
0
 def test_end_event_exception_in_target(self):
     probe = probes.attach_to("diagnose.test_fixtures.a_func")
     try:
         probe.start()
         probe.instruments["instrument1"] = i = ProbeTestInstrument(
             expires=datetime.datetime.utcnow() +
             datetime.timedelta(minutes=10),
             name="a_func",
             value="extra",
             event="end",
             custom=None,
         )
         with self.assertRaises(TypeError):
             a_func(None)
         self.assertEqual(i.results, [13])
     finally:
         probe.stop()
Esempio n. 2
0
 def test_return_event_locals_frame(self):
     probe = probes.attach_to("diagnose.test_fixtures.a_func")
     try:
         probe.start()
         probe.instruments["instrument1"] = ProbeTestInstrument(
             expires=datetime.datetime.utcnow() +
             datetime.timedelta(minutes=10),
             name="a_func",
             value="frame.f_back.f_code.co_name",
             custom=None,
         )
         a_func(923775)
         assert probe.instruments["instrument1"].results == [
             "test_return_event_locals_frame"
         ]
     finally:
         probe.stop()
Esempio n. 3
0
    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()
Esempio n. 4
0
 def test_return_event_exception_in_target(self):
     probe = probes.attach_to("diagnose.test_fixtures.a_func")
     try:
         probe.start()
         probe.instruments["instrument1"] = i = ProbeTestInstrument(
             expires=datetime.datetime.utcnow() +
             datetime.timedelta(minutes=10),
             name="a_func",
             value="result",
             event="return",
             custom=None,
         )
         with self.assertRaises(TypeError):
             a_func(None)
         self.assertEqual(len(i.results), 1)
         self.assertEqual(type(i.results[0]), TypeError)
         self.assertEqual(
             i.results[0].args,
             ("unsupported operand type(s) for +: 'NoneType' and 'int'", ),
         )
     finally:
         probe.stop()
Esempio n. 5
0
    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])
Esempio n. 6
0
 def test_end_event_success(self):
     probe = probes.attach_to("diagnose.test_fixtures.a_func")
     try:
         probe.start()
         probe.instruments["instrument1"] = i = ProbeTestInstrument(
             expires=datetime.datetime.utcnow() +
             datetime.timedelta(minutes=10),
             name="a_func",
             value="output",
             event="end",
             custom=None,
         )
         assert a_func(27) == 40
         assert i.results == [40]
     finally:
         probe.stop()
Esempio n. 7
0
    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=[]),
            ]
Esempio n. 8
0
 def test_end_event_exception_in_value(self):
     probe = probes.attach_to("diagnose.test_fixtures.a_func")
     try:
         errs = []
         old_handle_error = diagnose.manager.handle_error
         diagnose.manager.handle_error = lambda probe, instr: errs.append(
             sys.exc_info()[1].args[0] if sys.exc_info()[1].args[0] else "")
         probe.start()
         probe.instruments["instrument1"] = i = ProbeTestInstrument(
             expires=datetime.datetime.utcnow() +
             datetime.timedelta(minutes=10),
             name="a_func",
             value="unknown",  # Should throw NameError
             event="end",
             custom=None,
         )
         assert a_func(1000) == 1013
         assert i.results == []
         assert i.expires == i.error_expiration
         assert errs == ["name 'unknown' is not defined"]
     finally:
         diagnose.manager.handle_error = old_handle_error
         probe.stop()