An unofficial Python library for easy interaction with the Humio API
Project description
Humio API (unofficial lib)
This project requires
Python>=3.6.1
This is an unofficial library for interacting with Humio's API. If you're looking for the official Python Humio library it can be found here: humiolib. This library mostly exists because the official library was very basic back in 2019 when I first needed this.
Installation
pip install humiocli
Main features
- Asyncronous and syncronous streaming queries supported by
httpx
. - Queryjobs which can be polled once, or until completed.
- Chainable relative time modifiers (similar to Splunk e.g.
-1d@h-30m
). - List repository details (NOTE: normal Humio users cannot see repos without read permission).
- Easy env-variable based configuration.
- Ingest data to Humio, although you probably want to use Filebeat for anything other than one-off things to your sandbox.
- CLI companion tool available at humiocli.
- Create and update parsers.
- (PoC) An updateable timeseries, which can follow a moving timewindow using relative modifiers, optionally querying only the changed timewindow since previous update.
Usage
For convenience your Humio URL and token should be set in the environment variables HUMIO_BASE_URL
and HUMIO_TOKEN
. These can be set in ~/.config/humio/.env
and loaded by humioapi.loadenv()
.
Query repositories
Create an instance of HumioAPI to get started
import humioapi
import logging
humioapi.initialize_logging(level=logging.INFO, fmt="human")
api = humioapi.HumioAPI(**humioapi.loadenv())
repositories = api.repositories()
Iterate over syncronous streaming searches sequentially
import humioapi
import logging
humioapi.initialize_logging(level=logging.INFO, fmt="human")
api = humioapi.HumioAPI(**humioapi.loadenv())
stream = api.streaming_search(
query="log_type=trace user=someone",
repos=['frontend', 'backend', 'integration'],
start="-1week@day",
stop="now"
)
for event in stream:
print(event)
Itreate over asyncronous streaming searches in parallell, from a syncronous context
import asyncio
import humioapi
import logging
humioapi.initialize_logging(level=logging.INFO, fmt="human")
api = humioapi.HumioAPI(**humioapi.loadenv())
loop = asyncio.new_event_loop()
try:
asyncio.set_event_loop(loop)
tasks = api.async_streaming_tasks(
loop,
query="log_type=trace user=someone",
repos=['frontend', 'backend', 'integration'],
start="-1week@day",
stop="now",
concurrent_limit=10,
)
for event in humioapi.consume_async(loop, tasks):
print(event)
finally:
try:
loop.run_until_complete(loop.shutdown_asyncgens())
finally:
asyncio.set_event_loop(None)
loop.close()
Jupyter Notebook
pew new --python=python36 humioapi
# run the following commands inside the virtualenv
pip install git+https://github.com/gwtwod/humio-api.git
pip install ipykernel seaborn matplotlib
python -m ipykernel install --user --name 'python36-humioapi' --display-name 'Python 3.6 (venv humioapi)'
Start the notebook by running jupyter-notebook
and choose the newly created kernel when creating a new notebook.
Run this code to get started:
import humioapi
import logging
humioapi.initialize_logging(level=logging.INFO, fmt="human")
api = humioapi.HumioAPI(**humioapi.loadenv())
results = api.streaming_search(query='log_type=trace user=someone', repos=['frontend', 'backend'], start="@d", stop="now")
for i in results:
print(i)
To get a list of all readable repositories with names starting with 'frontend':
repos = sorted([k for k,v in api.repositories().items() if v['read_permission'] and k.startswith('frontend')])
Making a timechart (lineplot):
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
sns.set(color_codes=True)
sns.set_style('darkgrid')
results = api.streaming_search(query='log_type=stats | timechart(series=metric)', repos=['frontend'], start=start, stop=stop)
df = pd.DataFrame(results)
df['_count'] = df['_count'].astype(float)
df['_bucket'] = pd.to_datetime(df['_bucket'], unit='ms', origin='unix', utc=True)
df.set_index('_bucket', inplace=True)
df.index = df.index.tz_convert('Europe/Oslo')
df = df.pivot(columns='metric', values='_count')
sns.lineplot(data=df)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file humioapi-0.6.0.tar.gz
.
File metadata
- Download URL: humioapi-0.6.0.tar.gz
- Upload date:
- Size: 20.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.10 CPython/3.6.8 Linux/3.10.0-1127.19.1.el7.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80daa62274d271c541aff01f2c4f599f54607831113717e13a35e1f306941203 |
|
MD5 | 8fa1c34798d1722ca1d6190787c7c083 |
|
BLAKE2b-256 | 2a0c8b86f4927aec125d8c8ada15f994a6e6163557116e9f62db87951889f182 |
File details
Details for the file humioapi-0.6.0-py3-none-any.whl
.
File metadata
- Download URL: humioapi-0.6.0-py3-none-any.whl
- Upload date:
- Size: 21.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.10 CPython/3.6.8 Linux/3.10.0-1127.19.1.el7.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 352ef301ce85b7f92e244b66628ccb2427b87e1c376867505cc749369eef19c9 |
|
MD5 | d2285175a1fca2c93ca22ae0e2149a4f |
|
BLAKE2b-256 | 19baab76a7c114e572b7f4b9b88240975e178202a5ba0370828d81ea42695373 |