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.16.tar.gz (25.9 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.16-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for kuhl_haus_metrics-0.2.16.tar.gz
Algorithm Hash digest
SHA256 5ebb4f704b348d517470d7fbf9ad4297909f0790f0c60b3451934b031ff11a9e
MD5 eefc760e348cb451660cb78ed4135aab
BLAKE2b-256 8fea315f12313dac43e76f7e45045915e7da1f16a5dba82d5b0051e695eeb54d

See more details on using hashes here.

Provenance

The following attestation bundles were made for kuhl_haus_metrics-0.2.16.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.16-py3-none-any.whl.

File metadata

File hashes

Hashes for kuhl_haus_metrics-0.2.16-py3-none-any.whl
Algorithm Hash digest
SHA256 5fe54d059ee4502c79db51ef14c2ce6ebfecd569961c5b076c99eeb5fbaec41c
MD5 a7fcf44cf8179238896be68b13611152
BLAKE2b-256 12affb7d86809f32619340bb7ebe93a1e06ac19af17139f5e0e388d66351ea78

See more details on using hashes here.

Provenance

The following attestation bundles were made for kuhl_haus_metrics-0.2.16-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