Skip to main content

Otel-Metric-Base

OtelMetricBase is a simple base class for integrating with OpenTelemetry metrics. It provides an easy way to create and manage synchronous and asynchronous metrics such as counters, up-down counters, histograms, and observable gauges, while also supporting the ability to add tags (attributes) to the metrics.

Features

  • Create counters, up-down counters, histograms, and observable metrics.
  • Easily attach tags (attributes) to the metrics for enriched observability.
  • Supports OpenTelemetry OTLP protocol for exporting metrics.

Usage

from otel_metric_base.otel_metrics import OtelMetricBase

# Initialize OtelMetrics with OTLP endpoint
otel_metrics = OtelMetricBase(otlp_endpoint="http://localhost:4317")

Create Synchronous Metrics

You can create counters, up-down counters, and histograms using the create_metric method. Optionally, you can pass tags (attributes) to the metrics.

Example: Create a Counter with Tags

tags = {"environment": "production", "region": "us-east"}

Create a counter with tags

counter = otel_metrics.create_metric(
    metric_type="counter", 
    name="dynamic_counter", 
    description="A dynamic counter", 
    tags=tags
)

Add to the counter and attach the tags

counter.add(5, attributes=tags)

Create Observable Metrics Observable metrics (like gauges, counters, and up-down counters) require a callback function that returns the current value of the metric.

Create a callback function that returns the gauge value

def get_gauge_value() -> float:
    return 42.0  # Replace with actual logic

Create an observable gauge with a callback and tags

observable_gauge_callback = otel_metrics.create_observable_callback(get_gauge_value)
otel_metrics.create_metric(
    metric_type="observable_gauge", 
    name="dynamic_gauge", 
    callback=observable_gauge_callback, 
    tags=tags
)

Create Observable Counters and UpDownCounters The same structure applies for creating observable counters and up-down counters:

Create an observable counter

def get_counter_value() -> float:
    return 1.0

observable_counter_callback = otel_metrics.create_observable_callback(get_counter_value)
otel_metrics.create_metric(
    metric_type="observable_counter", 
    name="observable_counter", 
    callback=observable_counter_callback, 
    tags=tags
)

Create an observable up-down counter

def get_updown_counter_value() -> float:
    return -10.0

observable_updown_callback = otel_metrics.create_observable_callback(get_updown_counter_value)
otel_metrics.create_metric(
    metric_type="observable_up_down_counter", 
    name="observable_updown_counter", 
    callback=observable_updown_callback, 
    tags=tags
)

Exporting Metrics

The OtelMetricBase class automatically sets up an OTLP exporter to export the metrics to the specified endpoint. Ensure you have an OpenTelemetry Collector or similar service running at the specified OTLP endpoint (http://localhost:4317 by default).

class OtelMetricBase:
    def __init__(
        self, otlp_endpoint="http://localhost:4317", service_name="otel-metrics-service"
    ):
        # Initialize the Meter Provider with a dynamic service name
        resource = Resource(attributes={"service.name": service_name})

        # Set up the OTLP Exporter
        otlp_exporter = OTLPMetricExporter(endpoint=otlp_endpoint, insecure=True)

Release files for otel-metric-base 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for otel-metric-base 0.1.1
File Size Uploaded
otel_metric_base-0.1.1.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for otel-metric-base 0.1.1
File Interpreter ABI Platform
otel_metric_base-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.6 kB

Release files / otel_metric_base-0.1.1.tar.gz

Download URL otel_metric_base-0.1.1.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
70c81540aead6b86e1a070dcd40741652787abc956bd68cab2c6df351e1603bd
BLAKE2b-256 checksum
How to use checksums
b8fd3ffd56a91aea20dd1c422991b29328fb4b20586fb7f74cd8685a3d3b8602
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / otel_metric_base-0.1.1-py3-none-any.whl

Download URL otel_metric_base-0.1.1-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
97e5a9d2ca794cf20c12adc0b89745416003617150e4737fd9dd07d71ca4523e
BLAKE2b-256 checksum
How to use checksums
cd75cf769f9f51301d78a64955b608230df3277a937aecfa778b3a6f03ca30f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page