Skip to main content

Application logging and instrumentation

Project description

kuhl-haus-metrics

License PyPI Downloads Build Status CodeQL codecov GitHub issues GitHub pull requests

Overview

kuhl-haus-metrics is a Python library for application logging and instrumentation. It provides tools and utilities to help developers track application performance, collect metrics, and monitor system behavior in real-time.

Features

  • Comprehensive application logging
  • Performance metrics collection
  • Instrumentation for monitoring application behavior
  • Extensible architecture for custom metrics

Installation

You can install kuhl-haus-metrics via pip:

pip install kuhl-haus-metrics

Quick Start

Here's a simple example to get you started:

import ssl
import time
from datetime import datetime
from logging import Logger

import OpenSSL
from kuhl_haus.metrics.data.metrics import Metrics
from kuhl_haus.metrics.factories.logs import get_logger
from kuhl_haus.metrics.clients.carbon_poster import CarbonPoster


def get_tls_cert_expiration_days(hostname: str, port: int) -> int:
    raw_cert = ssl.get_server_certificate((hostname, port))
    cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, raw_cert.encode())
    expiration_time = time.mktime(datetime.strptime(cert.get_notAfter().decode(), '%Y%m%d%H%M%S%fZ').timetuple())
    current_time = time.mktime(datetime.now().timetuple())
    return int(expiration_time - current_time) // 60 // 60 // 24


logger: Logger = get_logger(log_level='INFO')
carbon_poster: CarbonPoster = CarbonPoster(server_ip="127.0.0.1", pickle_port=2004)

# hostname and port to check
host_name = "foo.com"
host_port = 443
# Initialize metrics
metrics: Metrics = Metrics(
    mnemonic="mnemonic",
    namespace="namespace",
    hostname=host_name,
    counters={
        'exceptions': 0,
        'requests': 0,
        'responses': 0,
    },
    attributes={
        "hostport": host_port
    }
)
try:
    metrics.set_counter('requests', 1)
    days_until_expiration = get_tls_cert_expiration_days(host_name, host_port)
    metrics.set_counter('responses', 1)
    metrics.attributes["days_until_expiration"] = days_until_expiration
    metrics.attributes["expires_today"] = days_until_expiration <= 1
    metrics.attributes["is_valid"] = days_until_expiration > 0
except Exception as e:
    metrics.set_counter('exceptions', 1)
    metrics.attributes['exception'] = repr(e)
    logger.exception(msg=f"unhandled exception processing {host_name}:{host_port}", exc_info=e)

carbon_poster.post_metrics(metrics=metrics.carbon)

Documentation

For detailed documentation and API reference, please visit our documentation site.

Requirements

  • Python 3.12+

Development

Setting Up Development Environment

  1. Clone the repository:

    git clone https://github.com/kuhl-haus/kuhl-haus-metrics.git
    cd kuhl-haus-metrics
    
  2. Create a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
    
  3. Install development dependencies:

    pip install -r requirements.txt
    pdm install
    

Running Tests

Tests are managed with pytest:

pdm run pytest tests -v

For test coverage:

pdm run pytest --cov=kuhl_haus.metrics --cov-report=html --cov-report=xml tests -v

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please make sure to update tests as appropriate and adhere to the project's coding standards.

License

This project is licensed under the terms of the license file included in the repository. See LICENSE.txt for details.

Support

If you encounter any issues or have questions, please open an issue on GitHub.

Acknowledgements


Built with ❤️ by the kuhl-haus team.

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

kuhl_haus_metrics-0.2.21.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

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

kuhl_haus_metrics-0.2.21-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file kuhl_haus_metrics-0.2.21.tar.gz.

File metadata

  • Download URL: kuhl_haus_metrics-0.2.21.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kuhl_haus_metrics-0.2.21.tar.gz
Algorithm Hash digest
SHA256 3fda2ff532c1e57d2db56e6ff54f24f6d225d194b1c0f8c25e94795fc57c9130
MD5 46c611a9736d1e2a0d1cfe2be6324bfd
BLAKE2b-256 11b5ed4d6931d52b29a7dd2dd24d932398a5966ef360278ff5c96b023f0a0afa

See more details on using hashes here.

Provenance

The following attestation bundles were made for kuhl_haus_metrics-0.2.21.tar.gz:

Publisher: publish-to-pypi.yml on kuhl-haus/kuhl-haus-metrics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kuhl_haus_metrics-0.2.21-py3-none-any.whl.

File metadata

File hashes

Hashes for kuhl_haus_metrics-0.2.21-py3-none-any.whl
Algorithm Hash digest
SHA256 07703853e8a4ad45577fc8c5ad49c7e81f4bc175d050dcd4c0435d7c43a79882
MD5 1a25d8f4753143895d5fc4eed1f2e1c0
BLAKE2b-256 fa253c65d9cb82b964b8d906704ddc7cdd30de3d8c003c2519ad567c2400168f

See more details on using hashes here.

Provenance

The following attestation bundles were made for kuhl_haus_metrics-0.2.21-py3-none-any.whl:

Publisher: publish-to-pypi.yml on kuhl-haus/kuhl-haus-metrics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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