Skip to main content

An OpenTelemetry metrics integration module.

Project description

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)

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

otel_metric_base-0.1.1.tar.gz (7.5 kB view hashes)

Uploaded Source

Built Distribution

otel_metric_base-0.1.1-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page