Ejemplo n.º 1
0
    def test_send_sampling(self):
        with mock.patch('libhoney.client.Transmission') as m_xmit,\
                mock.patch('libhoney.event._should_drop') as m_sd:
            m_sd.return_value = True
            libhoney.init(writekey="wk", dataset="ds")

            # test that send() drops when should_drop is true
            ev = libhoney.Event()
            ev.add_field("foo", 1)
            ev.send()
            m_xmit.return_value.send.assert_not_called()
            m_sd.assert_called_with(1)
            ev = libhoney.Event()
            ev.add_field("foo", 1)
            ev.sample_rate = 5
            ev.send()
            m_xmit.return_value.send.assert_not_called()
            m_sd.assert_called_with(5)

            # and actually sends them along when should_drop is false
            m_sd.reset_mock()
            m_xmit.reset_mock()
            m_sd.return_value = False

            ev = libhoney.Event()
            ev.add_field("f", "g")
            ev.api_host = "myhost"
            ev.writekey = "letmewrite"
            ev.dataset = "storeme"
            ev.send()
            m_xmit.return_value.send.assert_called_with(ev)
            m_sd.assert_called_with(1)
            ev.sample_rate = 5
            ev.send()
            m_xmit.return_value.send.assert_called_with(ev)
            m_sd.assert_called_with(5)

            # test that send_presampled() does not drop
            m_sd.reset_mock()
            m_xmit.reset_mock()
            ev.send_presampled()
            m_xmit.return_value.send.assert_called_with(ev)
            m_sd.assert_not_called()

            m_sd.reset_mock()
            m_xmit.reset_mock()
            ev.sample_rate = 5
            ev.send_presampled()
            m_xmit.return_value.send.assert_called_with(ev)
            m_sd.assert_not_called()
Ejemplo n.º 2
0
    def test_flush_after_timeout(self):
        libhoney.init()
        with requests_mock.Mocker() as m:
            m.post("http://urlme/1/batch/dataset",
                   text=json.dumps(100 * [{
                       "status": 202
                   }]),
                   status_code=200,
                   request_headers={"X-Honeycomb-Team": "writeme"})

            t = transmission.Transmission(max_concurrent_batches=1,
                                          send_frequency=0.1)
            t.start()

            ev = libhoney.Event()
            ev.writekey = "writeme"
            ev.dataset = "dataset"
            ev.add_field("key", "value")
            ev.api_host = "http://urlme/"

            t.send(ev)

            time.sleep(0.2)
            resp = t.responses.get()
            assert resp["status_code"] == 202
            t.close()
Ejemplo n.º 3
0
 def test_send(self):
     with mock.patch('libhoney.client.Transmission') as m_xmit:
         libhoney.init()
         ev = libhoney.Event()
         # override inherited api_host from client
         ev.api_host = ""
         with self.assertRaises(SendError) as c1:
             ev.send()
         self.assertTrue(
             "No metrics added to event. Won't send empty event." in str(
                 c1.exception))
         ev.add_field("f", "g")
         with self.assertRaises(SendError) as c2:
             ev.send()
         self.assertTrue("No api_host for Honeycomb." in str(c2.exception))
         ev.api_host = "myhost"
         with self.assertRaises(SendError) as c2:
             ev.send()
         self.assertTrue("No writekey specified." in str(c2.exception))
         ev.writekey = "letmewrite"
         with self.assertRaises(SendError) as c2:
             ev.send()
         self.assertTrue("No dataset for Honeycomb." in str(c2.exception))
         ev.dataset = "storeme"
         ev.send()
         m_xmit.return_value.send.assert_called_with(ev)
 def export(self, spans):
     hny_data = _translate_to_hny(spans)
     for d in hny_data:
         e = libhoney.Event(data=d, client=self.client)
         e.created_at = d['start_time']
         del d['start_time']
         e.send()
     return SpanExportResult.SUCCESS
Ejemplo n.º 5
0
 def test_send(self):
     with mock.patch('libhoney.client.Transmission') as m_xmit:
         libhoney.init()
         ev = libhoney.Event()
         # override inherited api_host from client
         ev.api_host = ""
         ev.add_field("f", "g")
         ev.api_host = "myhost"
         ev.writekey = "letmewrite"
         ev.dataset = "storeme"
         ev.send()
         m_xmit.return_value.send.assert_called_with(ev)
def send_honeycomb(report, config):
    """
    Sends the report to Honeycomb.

    :param report: The report to be sent.
    :param config: The IOpipe agent configuration.
    """

    try:
        ev = libhoney.Event()
        ev.add(report)
        ev.send()
    except Exception as e:
        logger.info('Error sending report to Honeycomb: %s' % e)
Ejemplo n.º 7
0
    def test_str(self):
        libhoney.init()
        ev = libhoney.Event()
        ev.add_field("obj", {"a": 1})
        ev.add_field("string", "a:1")
        ev.add_field("number", 5)
        ev.add_field("boolean", True)
        ev.add_field("null", None)

        serialized = str(ev)
        self.assertTrue('"obj": {"a": 1}' in serialized)
        self.assertTrue('"string": "a:1"' in serialized)
        self.assertTrue('"number": 5' in serialized)
        self.assertTrue('"boolean": true' in serialized)
        self.assertTrue('"null": null' in serialized)
Ejemplo n.º 8
0
    def test_send_gzip(self):
        libhoney.init()
        with requests_mock.Mocker() as m:
            m.post("http://urlme/1/batch/datame",
                   text=json.dumps([{
                       "status": 202
                   }]),
                   status_code=200,
                   request_headers={"X-Honeycomb-Team": "writeme"})

            t = transmission.Transmission(block_on_send=True)
            t.start()
            ev = libhoney.Event()
            ev.writekey = "writeme"
            ev.dataset = "datame"
            ev.api_host = "http://urlme/"
            ev.metadata = "metadaaata"
            ev.sample_rate = 3
            ev.created_at = datetime.datetime(2013, 1, 1, 11, 11, 11)
            ev.add_field("key", "asdf")
            t.send(ev)

            # sending is async even with the mock so block until it happens
            resp_received = False
            while not resp_received:
                resp = t.responses.get()
                if resp is None:
                    break

                self.assertEqual(resp["status_code"], 202)
                self.assertEqual(resp["metadata"], "metadaaata")
                resp_received = True

            for req in m.request_history:
                # verify gzip payload is sane by decompressing and checking contents
                self.assertEqual(req.headers['Content-Encoding'], 'gzip',
                                 "content encoding should be gzip")
                gz = gzip.GzipFile(fileobj=io.BytesIO(req.body), mode='rb')
                # json.load in python 3.5 doesn't like binary files, so we can't pass
                # the gzip stream directly to it
                uncompressed = gz.read().decode()
                data = json.loads(uncompressed)
                self.assertEqual(data[0]['samplerate'], 3)
                self.assertEqual(data[0]['data']['key'], 'asdf')
Ejemplo n.º 9
0
    def test_send_global(self):
        ''' ensure that events sent using the global libhoney config work '''
        with client.Client(writekey="mykey", dataset="something") as c:
            # explicitly use a different object for Transmission than is
            # defined in setUp, to ensure we aren't using the global
            # xmit in libhoney
            c.xmit = mock.Mock()

            libhoney.init(writekey="someotherkey", dataset="somethingelse")
            ev = libhoney.Event()
            self.assertEqual(ev.writekey, "someotherkey")
            self.assertEqual(ev.dataset, "somethingelse")
            ev.add_field("global", "event")
            ev.send()
            # test our assumption about what's actually mocked
            self.assertEqual(libhoney.state.G_CLIENT.xmit, self.tx)
            # check that we used the global xmit
            self.tx.send.assert_called_with(ev)
            # check that the client xmit was not used
            self.assertFalse(c.xmit.send.called)
Ejemplo n.º 10
0
    def test_batching(self):
        libhoney.init()
        with requests_mock.Mocker() as m:
            m.post("http://urlme/1/batch/datame",
                   text=json.dumps(200 * [{
                       "status": 202
                   }]),
                   status_code=200,
                   request_headers={"X-Honeycomb-Team": "writeme"})

            t = transmission.Transmission()
            t.start()
            for i in range(300):
                ev = libhoney.Event()
                ev.writekey = "writeme"
                ev.dataset = "datame"
                ev.api_host = "http://urlme/"
                ev.metadata = "metadaaata"
                ev.sample_rate = 3
                ev.created_at = datetime.datetime(2013, 1, 1, 11, 11, 11)
                ev.add_field("key", i)
                t.send(ev)
            t.close()

            resp_count = 0
            while not t.responses.empty():
                resp = t.responses.get()
                if resp is None:
                    break
                assert resp["status_code"] == 202
                assert resp["metadata"] == "metadaaata"
                resp_count += 1
            assert resp_count == 300

            for req in m.request_history:
                body = req.json()
                for event in body:
                    assert event["time"] == "2013-01-01T11:11:11Z"
                    assert event["samplerate"] == 3
Ejemplo n.º 11
0
def get_repos():
    ev = libhoney.Event()
    username = request.args.get("username")
    ev.add_field("username",username)

    api_url = "https://api.github.com/users/{}/repos".format(username)
    with ev.timer("gitub_request_ms"):
        resp = requests.get(api_url)

    label_dict = {"method": "GET",
                  "endpoint": "/get-repos",
                  "code": resp.status_code}
    ev.add(label_dict)

    prom_counter.labels(**label_dict).inc()

    resp_dict = resp.json()
    ev.add_field("num_repos", len(resp_dict))
    ev.send()

    repos = [x["name"] for x in resp_dict]

    return jsonify(repos=repos)
Ejemplo n.º 12
0
def make_video(text: str, style_id: str):
    ev = libhoney.Event()
    ev.add_field('type', 'bake')
    ev.add_field('text', text)
    ev.add_field('style', style_id)
    ev.send()
    style = [s for s in STYLES if s.id == style_id][0]
    with Image.open(style.image) as image:
        size = image.size
    overlay = Image.new('RGBA', size, (0, 0, 0, 0))
    render_text(text, overlay)
    with tempfile.TemporaryDirectory() as tmp:
        overlay_file = Path(tmp) / 'overlay.png'
        overlay.save(overlay_file)
        result_file = Path(tmp) / 'result.mp4'
        subprocess.run([
            'ffmpeg', '-hide_banner', '-v', 'warning', '-i',
            str(style.video), '-i',
            str(overlay_file), '-filter_complex', 'overlay=x=0:y=0', '-c:v',
            'libx264', '-preset', 'superfast', '-crf', '27', '-f', 'mp4',
            '-c:a', 'copy', '-y',
            str(result_file)
        ]).check_returncode()
        return result_file.read_bytes()
Ejemplo n.º 13
0
    def test_timer(self):
        libhoney.init()

        class fakeDate:
            def setNow(self, time):
                self.time = time

            def now(self):
                return self.time

            def utcnow(self):
                return self.time

        with mock.patch('libhoney.event.datetime') as m_datetime:
            fakeStart = datetime.datetime(2016, 1, 2, 3, 4, 5, 6)
            fakeEnd = fakeStart + datetime.timedelta(milliseconds=5)
            fd = fakeDate()
            fd.setNow(fakeStart)
            m_datetime.datetime = fd
            ev = libhoney.Event()
            with ev.timer("howlong"):
                fd.setNow(fakeEnd)
            self.assertEqual(ev._fields._data, {"howlong": 5})
            self.assertEqual(ev.created_at, fakeStart)