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()
def test_new_builder(self): libhoney.init() # new builder, no arguments b = libhoney.Builder() self.assertEqual(b._fields._data, {}) self.assertEqual(b._fields._dyn_fields, set()) # new builder, passed in data and dynfields expected_data = {"aa": 1} expected_dyn_fns = set([sample_dyn_fn]) b = libhoney.Builder(expected_data, expected_dyn_fns) self.assertEqual(b._fields._data, expected_data) self.assertEqual(b._fields._dyn_fields, expected_dyn_fns) # new builder, inherited data and dyn_fields libhoney.state.G_CLIENT.fields._data = expected_data libhoney.state.G_CLIENT.fields._dyn_fields = expected_dyn_fns b = libhoney.Builder() self.assertEqual(b._fields._data, expected_data) self.assertEqual(b._fields._dyn_fields, expected_dyn_fns) # new builder, merge inherited data and dyn_fields and arguments def sample_dyn_fn2(): return 5 expected_data = {"aa": 1, "b": 2} expected_dyn_fns = set([sample_dyn_fn, sample_dyn_fn2]) b = libhoney.Builder({"b": 2}, [sample_dyn_fn2]) self.assertEqual(b._fields._data, expected_data) self.assertEqual(b._fields._dyn_fields, expected_dyn_fns)
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 test_add(self): libhoney.init() b = libhoney.Builder() expected_data = {"a": 1, "b": 3} b.add(expected_data) self.assertEqual(b._fields._data, expected_data) expected_data.update({"c": 3, "d": 4}) b.add({"c": 3, "d": 4}) self.assertEqual(b._fields._data, expected_data)
def test_add_dynamic_field(self): libhoney.init() b = libhoney.Builder() expected_dyn_fns = set() self.assertEqual(b._fields._dyn_fields, expected_dyn_fns) b.add_dynamic_field(sample_dyn_fn) expected_dyn_fns.add(sample_dyn_fn) self.assertEqual(b._fields._dyn_fields, expected_dyn_fns) with self.assertRaises(TypeError): b.add_dynamic_field("foo")
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 test_grouping(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"}) m.post("http://urlme/1/batch/alt_dataset", text=json.dumps(100 * [{ "status": 202 }]), status_code=200, request_headers={"X-Honeycomb-Team": "writeme"}) t = transmission.Transmission(max_concurrent_batches=1, gzip_enabled=False) t.start() builder = libhoney.Builder() builder.writekey = "writeme" builder.dataset = "dataset" builder.api_host = "http://urlme/" for i in range(100): ev = builder.new_event() ev.created_at = datetime.datetime(2013, 1, 1, 11, 11, 11) ev.add_field("key", i) t.send(ev) builder.dataset = "alt_dataset" for i in range(100): ev = builder.new_event() 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 resp_count += 1 assert resp_count == 200 assert ({h.url for h in m.request_history} == { "http://urlme/1/batch/dataset", "http://urlme/1/batch/alt_dataset" })
def test_new_event(self): libhoney.init() b = libhoney.Builder() b.sample_rate = 5 expected_data = {"a": 1, "b": 3} b.add(expected_data) ev = b.new_event() self.assertEqual(b._fields, ev._fields) self.assertEqual(ev.sample_rate, 5) ev.add_field("3", "c") self.assertNotEqual(b._fields, ev._fields) # move to event testing when written self.assertEqual(json.loads(str(ev)), {"a": 1, "3": "c", "b": 3})
def test_clone_builder(self): libhoney.init() b = libhoney.Builder() b.dataset = "newds" b.add_field("e", 9) b.add_dynamic_field(sample_dyn_fn) c = b.clone() self.assertEqual(b._fields, c._fields) c.add_field("f", 10) b.add_field("g", 11) self.assertEqual(b._fields._data, {"e": 9, "g": 11}) self.assertEqual(c._fields._data, {"e": 9, "f": 10}) self.assertEqual(c.dataset, "newds")
def test_add_field(self): libhoney.init() b = libhoney.Builder() expected_data = {} self.assertEqual(b._fields._data, expected_data) b.add_field("foo", 4) expected_data["foo"] = 4 self.assertEqual(b._fields._data, expected_data) b.add_field("bar", "baz") expected_data["bar"] = "baz" self.assertEqual(b._fields._data, expected_data) b.add_field("foo", 6) expected_data["foo"] = 6 self.assertEqual(b._fields._data, expected_data)
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()
def __init__( self, writekey=None, dataset=None, service_name=None, sample_fraction=0, transport=sync.SyncTransport, ): libhoney.init(writekey=writekey, dataset=dataset, debug=True) self.builder = libhoney.Builder() self.service_name = service_name self.sample_fraction = sample_fraction self.transport = transport(self)
def test_add_field(self): libhoney.init() expected_data = {} self.assertEqual(libhoney.state.G_CLIENT.fields._data, expected_data) self.assertTrue(libhoney.state.G_CLIENT.fields.is_empty()) libhoney.add_field("foo", 4) expected_data["foo"] = 4 self.assertEqual(libhoney.state.G_CLIENT.fields._data, expected_data) self.assertFalse(libhoney.state.G_CLIENT.fields.is_empty()) libhoney.add_field("bar", "baz") expected_data["bar"] = "baz" self.assertEqual(libhoney.state.G_CLIENT.fields._data, expected_data) libhoney.add_field("foo", 6) expected_data["foo"] = 6 self.assertEqual(libhoney.state.G_CLIENT.fields._data, expected_data)
def post_setup(self, iopipe): # if there's iopipe config, override our defaults. for c in ['writekey', 'dataset', 'sample_rate', 'api_host']: if c in iopipe.config: self.config[c] = iopipe.config[c] # default sample rate to 1 try: self.config['sample_rate'] = int(self.config['sample_rate']) except ValueError: self.config['sample_rate'] = 1 if self.config['sample_rate'] < 1: self.config['sample_rate'] = 1 libhoney.init(**self.config)
def test_add_dynamic_field(self): libhoney.init() expected_dyn_fns = set() self.assertEqual(libhoney.state.G_CLIENT.fields._dyn_fields, expected_dyn_fns) libhoney.add_dynamic_field(sample_dyn_fn) expected_dyn_fns.add(sample_dyn_fn) self.assertEqual(libhoney.state.G_CLIENT.fields._dyn_fields, expected_dyn_fns) # adding a second time should still only have one element libhoney.add_dynamic_field(sample_dyn_fn) self.assertEqual(libhoney.state.G_CLIENT.fields._dyn_fields, expected_dyn_fns) with self.assertRaises(TypeError): libhoney.add_dynamic_field("foo")
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)
def test_add_dynamic_field(self): libhoney.init() expected_dyn_fns = set() self.assertEqual( libhoney.state.G_CLIENT.fields._dyn_fields, expected_dyn_fns) libhoney.add_dynamic_field(sample_dyn_fn) expected_dyn_fns.add(sample_dyn_fn) self.assertEqual( libhoney.state.G_CLIENT.fields._dyn_fields, expected_dyn_fns) # adding a second time should still only have one element libhoney.add_dynamic_field(sample_dyn_fn) self.assertEqual( libhoney.state.G_CLIENT.fields._dyn_fields, expected_dyn_fns) with self.assertRaises(TypeError): libhoney.add_dynamic_field("foo")
def test_init(self): ft = FakeTransmitter(3) with mock.patch('libhoney.client.Transmission') as m_xmit: m_xmit.return_value = ft libhoney.init(writekey="wk", dataset="ds", sample_rate=3, api_host="uuu", max_concurrent_batches=5, block_on_response=True) self.assertEqual(libhoney.state.G_CLIENT.writekey, "wk") self.assertEqual(libhoney.state.G_CLIENT.dataset, "ds") self.assertEqual(libhoney.state.G_CLIENT.api_host, "uuu") self.assertEqual(libhoney.state.G_CLIENT.sample_rate, 3) self.assertEqual(libhoney.state.G_CLIENT.xmit, ft) self.assertEqual(libhoney.state.G_CLIENT._responses, None) m_xmit.assert_called_with( block_on_response=True, block_on_send=False, max_concurrent_batches=5, user_agent_addition='', debug=False, )
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')
def test_grouping(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"}) m.post("http://urlme/1/batch/alt_dataset", text=json.dumps(100 * [{"status": 202}]), status_code=200, request_headers={"X-Honeycomb-Team": "writeme"}) t = transmission.Transmission(max_concurrent_batches=1) t.start() builder = libhoney.Builder() builder.writekey = "writeme" builder.dataset = "dataset" builder.api_host = "http://urlme/" for i in range(100): ev = builder.new_event() ev.created_at = datetime.datetime(2013, 1, 1, 11, 11, 11) ev.add_field("key", i) t.send(ev) builder.dataset = "alt_dataset" for i in range(100): ev = builder.new_event() 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 resp_count += 1 assert resp_count == 200 assert ({h.url for h in m.request_history} == {"http://urlme/1/batch/dataset", "http://urlme/1/batch/alt_dataset"})
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)
def test_init(self): ft = FakeTransmitter(3) with mock.patch('libhoney.client.Transmission') as m_xmit: m_xmit.return_value = ft libhoney.init(writekey="wk", dataset="ds", sample_rate=3, api_host="uuu", max_concurrent_batches=5, block_on_response=True) self.assertEqual(libhoney.state.G_CLIENT.writekey, "wk") self.assertEqual(libhoney.state.G_CLIENT.dataset, "ds") self.assertEqual(libhoney.state.G_CLIENT.api_host, "uuu") self.assertEqual(libhoney.state.G_CLIENT.sample_rate, 3) self.assertEqual(libhoney.state.G_CLIENT.xmit, ft) self.assertEqual(libhoney.state.G_CLIENT._responses, None) m_xmit.assert_called_with(block_on_response=True, block_on_send=False, max_concurrent_batches=5, user_agent_addition='')
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()
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
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
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)
import subprocess import tempfile from queue import Queue from threading import Thread import json from typing import Optional import telegram from telegram.ext import Dispatcher, MessageHandler, CommandHandler, InlineQueryHandler, Filters, CallbackContext from PIL import Image, ImageDraw, ImageFont import libhoney from flask import Flask, render_template, request app = Flask(__name__) libhoney.init(writekey=os.environ.get('HONEYCOMB_KEY'), dataset="crabravebot", debug=True) font = ImageFont.truetype("assets/fonts/NotoSans-Regular.ttf", 48) logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) @dataclass class Style: """Crab rave style""" id: str name: str
from flask import Flask import libhoney app = Flask(__name__) libhoney.init(writekey='abcabc123123', dataset='my flask app') from app import views
def read_responses(resp_queue): '''read responses from the libhoney queue, print them out.''' while True: resp = resp_queue.get() if resp is None: break status = "sending event with metadata {} took {}ms and got response code {} with message \"{}\"".format( resp["metadata"], resp["duration"], resp["status_code"], resp["body"].rstrip()) print status if __name__ == "__main__": libhoney.init(writekey=writekey, dataset=dataset, max_concurrent_batches=1) resps = libhoney.responses() t = threading.Thread(target=read_responses, args=(resps, )) t.start() # attach fields to top-level instance libhoney.add_field("version", "3.4.5") libhoney.add_dynamic_field(num_threads) # sends an event with "version", "num_threads", and "status" fields libhoney.send_now({"status": "starting run"}) run_fact(1, 2, libhoney.Builder({"range": "low"})) run_fact(31, 32, libhoney.Builder({"range": "high"})) # sends an event with "version", "num_threads", and "status" fields libhoney.send_now({"status": "ending run"})
def post_setup(self, iopipe): libhoney.init(**self.config)
app = Flask(__name__) app.logger.addHandler(logging.StreamHandler(sys.stderr)) app.logger.setLevel(logging.DEBUG) db_user = "******" db_pass = "" db_host = os.getenv("DB_HOST") db_name = "example-python-api" app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://{}:{}@{}:3306/{}".format( db_user, db_pass, db_host, db_name) app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False db = SQLAlchemy(app) libhoney.init(writekey=os.getenv("HONEYCOMB_WRITEKEY"), dataset="examples.python-api") libhoney_builder = libhoney.Builder() class Todo(db.Model): id = db.Column(db.Integer, primary_key=True) description = db.Column(db.String(200), nullable=False) completed = db.Column(db.Boolean(False), nullable=False) due = db.Column(db.DateTime()) def serialize(self): return { "id": self.id, "description": self.description, "completed": self.completed, "due": self.due,
def __init__(self): libhoney.init(writekey=os.environ["HONEY_WRITE_KEY"], dataset=os.environ["HONEY_DATASET"])
def test_add_dynamic_field(self): libhoney.init() ed = set([sample_dyn_fn]) libhoney.add_dynamic_field(sample_dyn_fn) self.assertEqual(libhoney.state.G_CLIENT.fields._dyn_fields, ed)
def test_add(self): libhoney.init() ed = {"whomp": True} libhoney.add(ed) self.assertEqual(libhoney.state.G_CLIENT.fields._data, ed)