Skip to main content

Unified wrapper for Elven logs interception and OpenTelemetry instrumentation in Python.

Project description

Unified Observability Py

Pacote unificado para juntar:

  • elven-logs-interceptor-python
  • elven-opentelemetry-instrumentation-py

com uma experiência única de bootstrap, config por env e API manual em Python.

Instalação

Base:

pip install elven-unified-observability-py

Com extras completos do logger:

pip install "elven-unified-observability-py[full]"

Para desenvolvimento:

pip install -e ".[dev]"

O que a lib entrega

  • init(...) manual com config Python-first
  • init_from_env() para bootstrap por ambiente
  • logger, tracer e metrics reexportados como proxies seguros
  • preload zero-code via sitecustomize
  • CLI elven-unified-observability -- <command>
  • shutdown e flush coordenados
  • fallback LOKI_* para LOGS_*
  • aliases legados ENABLE_LOGGING, ENABLE_METRICS, ENABLE_TRACING

Quick Start Manual

from elven_unified_observability import init, logger, metrics, tracer

handle = init(
    {
        "service": {
            "serviceName": "billing-service",
            "serviceVersion": "1.2.3",
            "environment": "production",
        },
        "logging": {
            "transport": {
                "url": "https://loki.example.com/loki/api/v1/push",
                "tenantId": "tenant-a",
                "authToken": "token",
                "compression": "gzip",
            },
            "interceptConsole": True,
        },
        "telemetry": {
            "metrics": {"exportIntervalMillis": 15000},
            "privacy": {"redactDbStatement": True},
        },
        "enableLogging": True,
        "enableMetrics": True,
        "enableTracing": True,
    }
)

logger.info("service started", {"service": "billing-service"})
metrics.increment("bootstrap.success", 1)
tracer.with_span("startup", lambda span: span.set_attribute("boot.ready", True))

handle.force_flush()
handle.shutdown()

Quick Start Zero-Code

  1. Crie um .env com a identidade do serviço e os endpoints.
  2. Rode a app com a CLI:
elven-unified-observability -- python app.py

Também funciona com:

elven-unified-observability -- uvicorn app:app
elven-unified-observability -- celery -A worker.app worker
elven-unified-observability -- python -m my_service

Como a precedence funciona

  1. Config explícita passada para init(...)
  2. Env da wrapper (UO_*)
  3. Env dos componentes (LOGS_*, LOKI_*, OTEL_*)
  4. Defaults das libs-base

Variáveis da wrapper

Variável Descrição
UO_ENABLED Liga/desliga tudo por default
UO_ENABLE_LOGGING Override unificado para logs
UO_ENABLE_METRICS Override unificado para métricas
UO_ENABLE_TRACING Override unificado para tracing
UO_STRICT Falha o bootstrap se qualquer componente falhar
UO_LOAD_DOTENV Controla leitura de .env no preload/CLI
UO_DOTENV_PATH Caminho alternativo para o .env

Aliases legados suportados:

  • ENABLE_LOGGING
  • ENABLE_METRICS
  • ENABLE_TRACING

Identidade unificada

A identidade do serviço é sempre derivada nesta ordem:

  1. OTEL_SERVICE_NAME
  2. LOGS_APP_NAME ou LOKI_APP_NAME
  3. default unknown-service

Versão:

  1. OTEL_SERVICE_VERSION
  2. LOGS_APP_VERSION ou LOKI_APP_VERSION
  3. default 1.0.0

Ambiente:

  1. OTEL_DEPLOYMENT_ENVIRONMENT
  2. LOGS_ENVIRONMENT ou LOKI_ENVIRONMENT
  3. ENVIRONMENT
  4. APP_ENV
  5. default development

Logging

Prefixo principal: LOGS_*

Fallback suportado: LOKI_*

Exemplos mais usados:

LOGS_URL=https://loki.example.com/loki/api/v1/push
LOGS_TENANT=my-tenant
LOGS_TOKEN=secret
LOGS_COMPRESSION=gzip
LOGS_BUFFER_MAX_SIZE=500
LOGS_FILTER_LEVELS=info,warn,error
LOGS_INTERCEPT_CONSOLE=true
LOGS_ENABLE_METRICS=true

LOGS_ENABLE_METRICS controla apenas as métricas internas do logger. Se ele não estiver definido, o pacote usa o valor unificado de UO_ENABLE_METRICS.

Telemetry

A wrapper reaproveita os OTEL_* já suportados pela lib-base.

Mais comuns:

OTEL_SERVICE_NAME=billing-service
OTEL_SERVICE_VERSION=1.2.3
OTEL_DEPLOYMENT_ENVIRONMENT=production
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_TRACES_EXPORTER=otlp
OTEL_METRICS_EXPORTER=otlp
OTEL_METRIC_EXPORT_INTERVAL=15000
OTEL_PRIVACY_REDACT_DB_STATEMENT=true
OTEL_INSTR_FASTAPI=true
OTEL_INSTR_REQUESTS=true

Framework examples

FastAPI

elven-unified-observability -- uvicorn app:app --host 0.0.0.0 --port 8000

Flask

elven-unified-observability -- flask --app app run

Celery

elven-unified-observability -- celery -A worker.app worker -l info

API pública

  • init(config=None)
  • init_from_env(strict=None)
  • load_config_from_env(env=None)
  • get_handle()
  • is_initialized()
  • force_flush()
  • shutdown()
  • logger
  • tracer
  • metrics

Build e publish

Validação local:

ruff check .
pyright
pytest
python -m build
python -m twine check dist/*

Publish local:

./publish.sh --repository testpypi --dry-run
./publish.sh --repository testpypi
./publish.sh --repository pypi

Tokens esperados:

  • TEST_PYPI_API_TOKEN
  • PYPI_API_TOKEN

Segurança

  • Não versione .env real
  • Não grave token em arquivo
  • Use secrets de CI ou variáveis de ambiente locais

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

elven_unified_observability_py-0.1.0.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

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

elven_unified_observability_py-0.1.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file elven_unified_observability_py-0.1.0.tar.gz.

File metadata

File hashes

Hashes for elven_unified_observability_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bf903eeba1c1a0be1f210679ce032d25564a3a3e83d170de203b6c331c22f9be
MD5 6a8b54dd00219644e2c653b7950195e6
BLAKE2b-256 f93fa092ba832390b29705a68a60d060a196f87d240eb3c7e6261f0a86056fab

See more details on using hashes here.

File details

Details for the file elven_unified_observability_py-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for elven_unified_observability_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7006867d79ad4ed7461a878a617d884ee1fd2af7ac1e1e86bf585980d92635f
MD5 cda41650a7a3959e38298e72001b6d15
BLAKE2b-256 da841c498725f850508aca282c06e268479012852bc48e941849d41b37086b2e

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