Humio Python SDK

0.2.6 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `HumioClient` using environment variables for credentials and then perform a streaming query to retrieve aggregated event counts. Replace `HUMIO_BASE_URL`, `HUMIO_REPOSITORY`, and `HUMIO_USER_TOKEN` with your Humio instance details.

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

view raw JSON →