libhoney Python Library

2.4.0 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

Initializes the libhoney library, creates a new event, adds various fields, and sends it to Honeycomb. It demonstrates best practices for API key handling via environment variables and proper shutdown procedures to ensure all events are flushed.

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.")

view raw JSON →