otel-metric-base


Nameotel-metric-base JSON
Version 0.1.1 PyPI version JSON
download
home_pageNone
SummaryAn OpenTelemetry metrics integration module.
upload_time2024-10-15 02:12:52
maintainerNone
docs_urlNone
authorNone
requires_python>=3.6
licenseApache-2.0
keywords opentelemetry metrics otlp python
VCS
bugtrack_url
requirements opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

```python
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**

```python
tags = {"environment": "production", "region": "us-east"}
```
### Create a counter with tags
```python
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
```python
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
```python
def get_gauge_value() -> float:
    return 42.0  # Replace with actual logic
```
### Create an observable gauge with a callback and tags
```python
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
```python
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

```python
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).

```python
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)
```



            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "otel-metric-base",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "OpenTelemetry, Metrics, OTLP, Python",
    "author": null,
    "author_email": "Brian Hartford <bh419@protonmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/b8/fd/3ffd56a91aea20dd1c422991b29328fb4b20586fb7f74cd8685a3d3b8602/otel_metric_base-0.1.1.tar.gz",
    "platform": null,
    "description": "# Otel-Metric-Base\n\n**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.\n\n## Features\n- Create counters, up-down counters, histograms, and observable metrics.\n- Easily attach tags (attributes) to the metrics for enriched observability.\n- Supports OpenTelemetry OTLP protocol for exporting metrics.\n\n## Usage\n\n```python\nfrom otel_metric_base.otel_metrics import OtelMetricBase\n\n# Initialize OtelMetrics with OTLP endpoint\notel_metrics = OtelMetricBase(otlp_endpoint=\"http://localhost:4317\")\n```\n\n### Create Synchronous Metrics\n\nYou can create counters, up-down counters, and histograms using the create_metric method. Optionally, you can pass tags (attributes) to the metrics.\n\n**Example: Create a Counter with Tags**\n\n```python\ntags = {\"environment\": \"production\", \"region\": \"us-east\"}\n```\n### Create a counter with tags\n```python\ncounter = otel_metrics.create_metric(\n    metric_type=\"counter\", \n    name=\"dynamic_counter\", \n    description=\"A dynamic counter\", \n    tags=tags\n)\n```\n\n### Add to the counter and attach the tags\n```python\ncounter.add(5, attributes=tags)\n```\nCreate Observable Metrics\nObservable metrics (like gauges, counters, and up-down counters) require a callback function that returns the current value of the metric.\n\n\n### Create a callback function that returns the gauge value\n```python\ndef get_gauge_value() -> float:\n    return 42.0  # Replace with actual logic\n```\n### Create an observable gauge with a callback and tags\n```python\nobservable_gauge_callback = otel_metrics.create_observable_callback(get_gauge_value)\notel_metrics.create_metric(\n    metric_type=\"observable_gauge\", \n    name=\"dynamic_gauge\", \n    callback=observable_gauge_callback, \n    tags=tags\n)\n```\nCreate Observable Counters and UpDownCounters\nThe same structure applies for creating observable counters and up-down counters:\n\n\n### Create an observable counter\n```python\ndef get_counter_value() -> float:\n    return 1.0\n\nobservable_counter_callback = otel_metrics.create_observable_callback(get_counter_value)\notel_metrics.create_metric(\n    metric_type=\"observable_counter\", \n    name=\"observable_counter\", \n    callback=observable_counter_callback, \n    tags=tags\n)\n```\n### Create an observable up-down counter\n\n```python\ndef get_updown_counter_value() -> float:\n    return -10.0\n\nobservable_updown_callback = otel_metrics.create_observable_callback(get_updown_counter_value)\notel_metrics.create_metric(\n    metric_type=\"observable_up_down_counter\", \n    name=\"observable_updown_counter\", \n    callback=observable_updown_callback, \n    tags=tags\n)\n```\n\n### Exporting Metrics\nThe 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).\n\n```python\nclass OtelMetricBase:\n    def __init__(\n        self, otlp_endpoint=\"http://localhost:4317\", service_name=\"otel-metrics-service\"\n    ):\n        # Initialize the Meter Provider with a dynamic service name\n        resource = Resource(attributes={\"service.name\": service_name})\n\n        # Set up the OTLP Exporter\n        otlp_exporter = OTLPMetricExporter(endpoint=otlp_endpoint, insecure=True)\n```\n\n\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "An OpenTelemetry metrics integration module.",
    "version": "0.1.1",
    "project_urls": {
        "Homepage": "https://github.com/UTXOnly/OTel-metric-base",
        "Repository": "https://github.com/UTXOnly/OTel-metric-base"
    },
    "split_keywords": [
        "opentelemetry",
        " metrics",
        " otlp",
        " python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cd75cf769f9f51301d78a64955b608230df3277a937aecfa778b3a6f03ca30f1",
                "md5": "c8ecca0efd7bc2c63f5d4547ce5a5efc",
                "sha256": "97e5a9d2ca794cf20c12adc0b89745416003617150e4737fd9dd07d71ca4523e"
            },
            "downloads": -1,
            "filename": "otel_metric_base-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c8ecca0efd7bc2c63f5d4547ce5a5efc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 8138,
            "upload_time": "2024-10-15T02:12:52",
            "upload_time_iso_8601": "2024-10-15T02:12:52.020875Z",
            "url": "https://files.pythonhosted.org/packages/cd/75/cf769f9f51301d78a64955b608230df3277a937aecfa778b3a6f03ca30f1/otel_metric_base-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b8fd3ffd56a91aea20dd1c422991b29328fb4b20586fb7f74cd8685a3d3b8602",
                "md5": "7b7fc51a92b535da8c5fc079355a33ed",
                "sha256": "70c81540aead6b86e1a070dcd40741652787abc956bd68cab2c6df351e1603bd"
            },
            "downloads": -1,
            "filename": "otel_metric_base-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "7b7fc51a92b535da8c5fc079355a33ed",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 7501,
            "upload_time": "2024-10-15T02:12:52",
            "upload_time_iso_8601": "2024-10-15T02:12:52.997321Z",
            "url": "https://files.pythonhosted.org/packages/b8/fd/3ffd56a91aea20dd1c422991b29328fb4b20586fb7f74cd8685a3d3b8602/otel_metric_base-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-15 02:12:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "UTXOnly",
    "github_project": "OTel-metric-base",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "opentelemetry-api",
            "specs": []
        },
        {
            "name": "opentelemetry-sdk",
            "specs": []
        },
        {
            "name": "opentelemetry-exporter-otlp",
            "specs": []
        }
    ],
    "lcname": "otel-metric-base"
}
        
Elapsed time: 0.41139s