libhoney Python Library
libhoney is a Python library for sending structured events to Honeycomb, an observability platform for debugging software in production. It is a low-level library designed for direct interaction with Honeycomb's Events API. The library is actively maintained, with a recent major release (2.4.0) in March 2024, and maintains a regular release cadence.
Warnings
- breaking Python 2.7 support was dropped in v2.0.0. If you are on an older Python 2.x environment, you must upgrade Python or use an older libhoney version.
- breaking The minimum supported Python version was raised to 3.5 in v2.0.0, and further to 3.7 in v2.3.0. Versions of Python older than 3.7 are no longer supported.
- gotcha Events are sent asynchronously in batches. Not calling `libhoney.close()` upon application shutdown can result in buffered events not being sent to Honeycomb, leading to data loss.
- gotcha For new applications requiring tracing, Honeycomb recommends using OpenTelemetry Python SDK instead of libhoney for its standardized, vendor-agnostic, and future-proof approach to telemetry (traces, logs, and metrics).
- gotcha Honeycomb API keys have different types and permissions. 'Classic-flavored' ingest keys are now explicitly supported in v2.4.0, but general ingest keys can have permissions like 'Can create datasets'. Ensure your API key has the necessary permissions and is the correct type for your use case.
- gotcha Responses from Honeycomb (e.g., status codes) are placed in an internal queue. If this queue is not actively read from, responses may be dropped if the queue becomes full. By default, sending threads are not blocked by the response queue being full.
- deprecated The `send_now` method was deprecated in favor of `new_event().send()`, which batches events for more efficient transmission.
Install
-
pip install libhoney
Imports
- libhoney
import libhoney
Quickstart
import libhoney
import os
import time
# Initialize libhoney with your API key and dataset name.
# It's recommended to retrieve these from environment variables.
libhoney.init(
writekey=os.environ.get('HONEYCOMB_API_KEY', 'YOUR_API_KEY'),
dataset=os.environ.get('HONEYCOMB_DATASET', 'my-python-app'),
service_name='my-python-app'
)
try:
# Create a new event
ev = libhoney.new_event()
# Add fields to the event
ev.add_field('event_type', 'example_event')
ev.add_field('request.method', 'GET')
ev.add_field('request.path', '/api/v1/data')
ev.add_field('duration_ms', 123.45)
ev.add_field('user.id', 'user-123')
# Send the event asynchronously
ev.send()
print("Event sent successfully (asynchronously).")
# For demonstration, wait a bit for asynchronous send
time.sleep(0.1)
finally:
# It's crucial to call libhoney.close() on application shutdown
# to ensure all buffered events are sent.
libhoney.close()
print("libhoney closed, all events flushed.")