Humio Python SDK
The `humiolib` library is an official Python SDK for interacting with Humio's web API, enabling easy integration for log ingestion and query execution directly from Python. It aims to provide an opinionated and user-friendly wrapper over the raw API. The current version is 0.2.6, and the project is actively maintained by Humio ApS employees, with releases occurring irregularly but consistently since 2020.
Warnings
- breaking Version 0.2.0 introduced a breaking change with a complete update to the API interface. Code written for versions prior to 0.2.0 will likely not be compatible.
- gotcha When performing static queries that return large amounts of data, the `streaming_query` method is preferred over `create_queryjob` and subsequent polling, as `streaming_query` opens a direct streaming socket connection, which is generally more efficient for bulk results.
- gotcha Live query jobs created via `create_queryjob` persist on the Humio instance for 1 hour after the last poll. It is recommended to explicitly delete them when they are no longer in use to free up resources.
- gotcha While the `HumioClient` can be used for ingesting data, the `HumioIngestClient` is specifically designed for data ingestion and is the recommended client for this purpose, potentially offering better performance or specialized methods.
- gotcha An open issue (GitHub #26) indicates that `humiolib` might not sanitize URLs in some cases, which could potentially lead to unexpected behavior or security concerns if untrusted input is used in URL construction.
Install
-
pip install humiolib
Imports
- HumioClient
from humiolib.HumioClient import HumioClient
- HumioIngestClient
from humiolib.HumioClient import HumioIngestClient
- QueryJob
from humiolib.QueryJob import QueryJob
Quickstart
import os
from humiolib.HumioClient import HumioClient
HUMIO_BASE_URL = os.environ.get("HUMIO_BASE_URL", "https://cloud.humio.com")
HUMIO_REPOSITORY = os.environ.get("HUMIO_REPOSITORY", "sandbox")
HUMIO_USER_TOKEN = os.environ.get("HUMIO_USER_TOKEN", "")
if not HUMIO_USER_TOKEN:
print("Please set the HUMIO_USER_TOKEN environment variable.")
exit(1)
try:
client = HumioClient(
base_url=HUMIO_BASE_URL,
repository=HUMIO_REPOSITORY,
user_token=HUMIO_USER_TOKEN
)
# Perform a streaming query for events in the last hour
query_string = "timechart(span=1h) count()"
print(f"Executing query: '{query_string}' in repository '{HUMIO_REPOSITORY}'")
events_generator = client.streaming_query(
query_string=query_string,
start="1h@h", # Last hour, aligned to the hour
end="now"
)
for event in events_generator:
print(event)
except Exception as e:
print(f"An error occurred: {e}")