Skip to main content

Cezzis OpenTelemetry

Python Version License: MIT PyPI version

A lightweight, production-ready Python library for OpenTelemetry observability. Simplifies tracing and logging setup with automatic OTLP exporter integration and structured service instrumentation.

Installation

Install cezzis-otel from PyPI:

pip install cezzis-otel

Or using Poetry:

poetry add cezzis-otel

Requirements

  • Python 3.12 or higher
  • OpenTelemetry Collector (optional, for remote telemetry)

Source Code

Find the source code, contribute, or report issues on GitHub:

Repository: https://github.com/mtnvencenzo/cezzis-pycore

Key Features

  • Simple OpenTelemetry Setup - One-line initialization for tracing and logging
  • OTLP Integration - Built-in OTLP exporter configuration for popular observability platforms
  • Service Resource Management - Automatic service metadata and resource attribution
  • Flexible Configuration - Comprehensive settings for all OpenTelemetry options
  • Production Ready - Built-in error handling and graceful shutdown capabilities
  • Type-Safe - Full type hints for better IDE support and code quality

Quick Start Guide

Basic Example: Simple Service Setup

Here's a minimal example to get started with OpenTelemetry in your Python service:

from cezzis_otel import OTelSettings, initialize_otel, get_logger, shutdown_otel
import logging
import time

def main():
    # Configure OpenTelemetry settings
    settings = OTelSettings(
        service_name="my-python-service",
        service_namespace="production",
        service_version="1.0.0",
        otlp_exporter_endpoint="https://api.honeycomb.io",
        otlp_exporter_auth_header="Bearer your-api-key",
        environment="production",
        instance_id="web-server-01"
    )
    
    # Initialize OpenTelemetry with one line
    initialize_otel(settings)
    
    # Get an instrumented logger
    logger = get_logger(__name__)
    
    try:
        logger.info("Service starting up")
        
        # Your application logic here
        for i in range(5):
            logger.info(f"Processing item {i}")
            time.sleep(1)
            
        logger.info("Service completed successfully")
        
    except Exception as e:
        logger.error(f"Service failed: {e}")
        raise
    finally:
        # Clean shutdown
        shutdown_otel()

if __name__ == "__main__":
    main()

Example: Local Development Setup

For local development with an OpenTelemetry Collector:

from cezzis_otel import OTelSettings, initialize_otel, get_logger, shutdown_otel

# Configure for local development
settings = OTelSettings(
    service_name="local-dev-service", 
    service_namespace="development",
    service_version="0.1.0",
    otlp_exporter_endpoint="http://localhost:4318",  # Local collector
    otlp_exporter_auth_header="",  # No auth for local
    environment="local",
    instance_id="dev-machine"
)

# Initialize and use
initialize_otel(settings)
logger = get_logger(__name__)

logger.info("Local development setup complete")
logger.debug("This will include trace context automatically")

# Don't forget cleanup
shutdown_otel()

API Reference

OTelSettings

Configuration class for OpenTelemetry setup.

Parameters:

  • service_name (str): Name of your service (required)
  • service_namespace (str): Service namespace/team (required)
  • service_version (str): Version of your service (required)
  • otlp_exporter_endpoint (str): OTLP collector endpoint URL (required)
  • otlp_exporter_auth_header (str): Authorization header for OTLP exporter (required)
  • environment (str): Environment name (e.g., "production", "staging") (required)
  • instance_id (str): Unique instance identifier (required)
  • enable_logging (bool): Enable OpenTelemetry logging (default: True)
  • enable_tracing (bool): Enable OpenTelemetry tracing (default: True)

Example:

settings = OTelSettings(
    service_name="user-api",
    service_namespace="backend-services", 
    service_version="2.1.0",
    otlp_exporter_endpoint="https://api.honeycomb.io",
    otlp_exporter_auth_header="Bearer your-api-key",
    environment="production",
    instance_id="api-server-03"
)

initialize_otel(settings, configure_tracing=None, configure_logging=None)

Initialize OpenTelemetry tracing and logging with the provided settings.

Parameters:

  • settings (OTelSettings): Configuration object for OpenTelemetry setup
  • configure_tracing (Optional[Callable]): Optional callback to customize trace provider
  • configure_logging (Optional[Callable]): Optional callback to customize log provider

Example:

from cezzis_otel import initialize_otel, OTelSettings

settings = OTelSettings(...)
initialize_otel(settings)

get_logger(name, level=logging.INFO)

Get an OpenTelemetry-instrumented logger instance.

Parameters:

  • name (str): Logger name (typically __name__)
  • level (int): Logging level (default: logging.INFO)

Returns:

  • logging.Logger: Configured logger with OpenTelemetry integration

Example:

from cezzis_otel import get_logger
import logging

logger = get_logger(__name__, level=logging.DEBUG)
logger.info("This message includes trace context automatically")

shutdown_otel()

Gracefully shutdown OpenTelemetry providers and flush any pending telemetry data.

Example:

from cezzis_otel import shutdown_otel

# At application shutdown
shutdown_otel()

2. Structured Logging

Use structured logging with contextual information:

logger = get_logger(__name__)

def process_user_login(user_id, ip_address):
    logger.info(
        "User login attempt",
        extra={
            "user_id": user_id,
            "ip_address": ip_address,
            "action": "login"
        }
    )

3. Error Handling

Always log errors with proper context:

def risky_operation(data):
    try:
        result = process_data(data)
        logger.info("Operation completed successfully")
        return result
    except Exception as e:
        logger.error(
            "Operation failed", 
            extra={"data_size": len(data), "error_type": type(e).__name__},
            exc_info=True
        )
        raise

4. Resource Cleanup

Always shut down OpenTelemetry properly:

import signal
import sys
from cezzis_otel import shutdown_otel

def signal_handler(sig, frame):
    print("Shutting down gracefully...")
    shutdown_otel()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

Troubleshooting

Telemetry Not Appearing

  1. Verify OTLP endpoint is reachable: curl -v https://your-endpoint/v1/traces
  2. Check authentication headers are correct
  3. Ensure initialize_otel() is called before logging
  4. Verify the OpenTelemetry Collector is running and configured

Missing Trace Context

  • Ensure you're using get_logger() from cezzis_otel
  • Check that initialize_otel() completed successfully
  • Verify tracing is enabled: enable_tracing=True in settings

Performance Impact

  • Use appropriate log levels (avoid DEBUG in production)
  • Monitor OTLP exporter endpoint latency
  • Consider batch export intervals for high-volume applications
  • Set reasonable resource limits for trace/log providers

Contributing

We welcome contributions! Visit the GitHub repository to:

License

This project is licensed under the MIT License. See the LICENSE file for details.

Support

Acknowledgments

Built with OpenTelemetry Python, the official Python implementation of OpenTelemetry.


Happy observing! �

Download files

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

Source Distribution

cezzis_otel-0.0.25.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

cezzis_otel-0.0.25-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file cezzis_otel-0.0.25.tar.gz.

File metadata

  • Download URL: cezzis_otel-0.0.25.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for cezzis_otel-0.0.25.tar.gz
Algorithm Hash digest
SHA256 efc85e634ab81a020cf4547879841963f4b05a5cc5d57a04fa98623e5ded2327
MD5 ec62d7c04fb8c88c3636244c4f0a1ea8
BLAKE2b-256 d832df8630fec12aa09baff30b9fb4f0940cf99f4d5809ac3e46bde9689a8e77

See more details on using hashes here.

File details

Details for the file cezzis_otel-0.0.25-py3-none-any.whl.

File metadata

  • Download URL: cezzis_otel-0.0.25-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for cezzis_otel-0.0.25-py3-none-any.whl
Algorithm Hash digest
SHA256 1f84f8a6331fb6ac1fda001c78ebce014ab52d3c6791f84d7cfb3f0fecc2d0ca
MD5 28ce0715c34051db89887f471a852ee4
BLAKE2b-256 60537502b21fd566852f35e0635ee17c5907632ba217c24ad735e6d8251df263

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.25 This release

2 files

0.0.20

2 files

0.0.18

2 files

0.0.17

2 files

0.0.16

2 files

0.0.12

2 files

0.0.11

2 files

0.0.10

2 files

0.0.9

2 files

0.0.5

2 files

0.0.4

2 files

Supported by

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