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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file otel_metric_base-0.1.1.tar.gz
.
File metadata
- Download URL: otel_metric_base-0.1.1.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70c81540aead6b86e1a070dcd40741652787abc956bd68cab2c6df351e1603bd |
|
MD5 | 7b7fc51a92b535da8c5fc079355a33ed |
|
BLAKE2b-256 | b8fd3ffd56a91aea20dd1c422991b29328fb4b20586fb7f74cd8685a3d3b8602 |
File details
Details for the file otel_metric_base-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: otel_metric_base-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97e5a9d2ca794cf20c12adc0b89745416003617150e4737fd9dd07d71ca4523e |
|
MD5 | c8ecca0efd7bc2c63f5d4547ce5a5efc |
|
BLAKE2b-256 | cd75cf769f9f51301d78a64955b608230df3277a937aecfa778b3a6f03ca30f1 |