Skip to main content

TinyDB-based document storage for simple telemetry/logging with readable sync, in-memory async and serialized async modes.

Project description

metricallize

Uma forma simples de armazenar telemetria/logs em “banco de documentos” usando TinyDB, com 3 modos de persistência:

  • readable_sync: JSON legível (escreve no disco de forma direta)
  • in_memory_async: JSON legível com cache em memória (escreve em batch, no flush/close)
  • serialized_async: arquivo binário/comprimido (mais rápido/menor, escreve em background; exige flush/close)

Instalação

pip install metricallize

Para usar o modo serialized_async (serialização/compressão):

pip install "metricallize[serialized_async]"

Conceitos

  • Você cria um DocumentStorage
  • Você acessa qualquer table com storage.db.table("nome")
  • O decorator storage.capture(table="...") registra uma “execução” (status/duração/erro) nessa table

Campos automáticos gravados pelo capture:

  • ts: YYYY-MM-DD HH:MM:SS.xxx (UTC)
  • started_at: YYYY-MM-DD HH:MM:SS.xxx (UTC)
  • finished_at: YYYY-MM-DD HH:MM:SS.xxx (UTC)
  • name: module.qualname da função
  • status: ok ou error
  • duration: HH:MM:SS
  • inputs: parâmetros serializados (com limites)
  • output: retorno serializado (com limites)
  • memory_before, memory_after, memory_peak, memory_delta: G.M.K (base 1024, K com 3 dígitos)
  • Em caso de erro: exc_type, exc_msg

Path e versionamento (por mês)

Se você passar path como diretório (sem extensão), o arquivo é criado automaticamente em:

<path>/storage/YYYY_MM/day_DD.json (modos legíveis)
<path>/storage/YYYY_MM/day_DD.db (modo serialized)

Você pode controlar o nome base com name="...".

Uso: readable_sync (JSON legível)

from pathlib import Path
from metricallize import DocumentStorage

storage_dir = Path.cwd() / "data"
storage = DocumentStorage(path=storage_dir, mode="readable_sync", name="logger")

logger = storage.db.table("loggers")
logger.insert({"event": "startup"})

Uso: in_memory_async (cache em memória, JSON legível)

from pathlib import Path
from metricallize import DocumentStorage

storage_dir = Path.cwd() / "data"
storage = DocumentStorage(
    path=storage_dir,
    mode="in_memory_async",
    name="trace",
    write_cache_size=100_000,
)

trace = storage.db.table("trace")
trace.insert({"event": "step"})

storage.flush()
storage.close()

Uso: serialized_async (binário, assíncrono)

from pathlib import Path
from metricallize import DocumentStorage

storage_dir = Path.cwd() / "data"
storage = DocumentStorage(path=storage_dir, mode="serialized_async", name="trace")

trace = storage.db.table("trace")
trace.insert({"event": "step"})

storage.flush()
storage.close()

Decorator: capture

from pathlib import Path
from metricallize import DocumentStorage

storage = DocumentStorage(path=Path.cwd() / "data", mode="readable_sync")
table_name = "trace"

@storage.capture(table=table_name, capture_memory_mb=True)
def soma(a: int, b: int) -> int:
    return a + b

@storage.capture(table=table_name)
def falha() -> None:
    raise RuntimeError("boom")

soma(1, 2)
try:
    falha()
except RuntimeError:
    pass

# Se quiser reduzir overhead:
# @storage.capture(table=table_name, capture_memory_mb=False, capture_inputs=False, capture_output=False)

Publicação (TestPyPI e PyPI)

Build:

python -m pip install --upgrade build twine
python -m build

TestPyPI:

python -m twine upload --repository testpypi dist/*

PyPI:

python -m twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metricallize-0.1.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

metricallize-0.1.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file metricallize-0.1.1.tar.gz.

File metadata

  • Download URL: metricallize-0.1.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for metricallize-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5db11655b143f1cfa10abf7345cd75d15c5120861861a3653cf90c8736e767c3
MD5 88a635a37a792da633c47f5fa37c22b1
BLAKE2b-256 ddbeef58f42520ea5a7ec40f84c3fec04c3467c263a766e58278f5d2da0d8c69

See more details on using hashes here.

File details

Details for the file metricallize-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: metricallize-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for metricallize-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 be8ef34df3961e94159950b80e9168f301b0276260594fef4a4d894830244bd9
MD5 97d12e6a4d6112017b9cfaf3d65e0b3b
BLAKE2b-256 9e63299583adf58074118cb743d06b052d49b515f8c676d8429ccaf3433d0337

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page