Skip to content

Metrics models

provide.foundation.integrations.openobserve.metrics_models

OpenObserve metrics data models.

These models represent metrics data from OpenObserve's Prometheus-compatible API.

Classes

MetricLabels

Label names and values for a metric.

Attributes:

Name Type Description
label_names list[str]

List of label names

label_values dict[str, list[str]]

Dictionary mapping label names to list of values

Functions
from_dict classmethod
from_dict(data: dict[str, Any]) -> MetricLabels

Create from dictionary.

Parameters:

Name Type Description Default
data dict[str, Any]

Dictionary with label information

required

Returns:

Type Description
MetricLabels

MetricLabels instance

Source code in provide/foundation/integrations/openobserve/metrics_models.py
@classmethod
def from_dict(cls, data: dict[str, Any]) -> MetricLabels:
    """Create from dictionary.

    Args:
        data: Dictionary with label information

    Returns:
        MetricLabels instance
    """
    return cls(
        label_names=data.get("label_names", []),
        label_values=data.get("label_values", {}),
    )
to_dict
to_dict() -> dict[str, Any]

Convert to dictionary.

Returns:

Type Description
dict[str, Any]

Dictionary representation

Source code in provide/foundation/integrations/openobserve/metrics_models.py
def to_dict(self) -> dict[str, Any]:
    """Convert to dictionary.

    Returns:
        Dictionary representation
    """
    return {
        "label_names": self.label_names,
        "label_values": self.label_values,
    }

MetricMetadata

Metadata for a metric.

Attributes:

Name Type Description
name str

Metric name

type str

Metric type (counter, gauge, histogram, summary, etc.)

help str

Help text describing the metric

unit str

Unit of measurement

Functions
from_dict classmethod
from_dict(data: dict[str, Any]) -> MetricMetadata

Create from dictionary.

Parameters:

Name Type Description Default
data dict[str, Any]

Dictionary with metric metadata

required

Returns:

Type Description
MetricMetadata

MetricMetadata instance

Source code in provide/foundation/integrations/openobserve/metrics_models.py
@classmethod
def from_dict(cls, data: dict[str, Any]) -> MetricMetadata:
    """Create from dictionary.

    Args:
        data: Dictionary with metric metadata

    Returns:
        MetricMetadata instance
    """
    return cls(
        name=data.get("name", ""),
        type=data.get("type", ""),
        help=data.get("help", ""),
        unit=data.get("unit", ""),
    )
to_dict
to_dict() -> dict[str, Any]

Convert to dictionary.

Returns:

Type Description
dict[str, Any]

Dictionary representation

Source code in provide/foundation/integrations/openobserve/metrics_models.py
def to_dict(self) -> dict[str, Any]:
    """Convert to dictionary.

    Returns:
        Dictionary representation
    """
    return {
        "name": self.name,
        "type": self.type,
        "help": self.help,
        "unit": self.unit,
    }

MetricQueryResult

Result from a Prometheus query.

Attributes:

Name Type Description
result_type str

Type of result (vector, matrix, scalar, string)

result list[MetricSample]

List of metric samples

status str

Query status (success, error)

error_type str

Error type if query failed

error str

Error message if query failed

warnings list[str]

List of warnings

Attributes
is_success property
is_success: bool

Check if query was successful.

Returns:

Type Description
bool

True if status is "success"

sample_count property
sample_count: int

Get number of metric samples.

Returns:

Type Description
int

Number of samples in result

Functions
from_dict classmethod
from_dict(data: dict[str, Any]) -> MetricQueryResult

Create from dictionary.

Parameters:

Name Type Description Default
data dict[str, Any]

Dictionary with query result from Prometheus API

required

Returns:

Type Description
MetricQueryResult

MetricQueryResult instance

Source code in provide/foundation/integrations/openobserve/metrics_models.py
@classmethod
def from_dict(cls, data: dict[str, Any]) -> MetricQueryResult:
    """Create from dictionary.

    Args:
        data: Dictionary with query result from Prometheus API

    Returns:
        MetricQueryResult instance
    """
    # Handle OpenObserve/Prometheus API response format
    if "data" in data:
        data_section = data["data"]
        status = data.get("status", "success")
        warnings = data.get("warnings", [])
        error_type = data.get("errorType", "")
        error = data.get("error", "")

        result_type = data_section.get("resultType", "")
        result_data = data_section.get("result", [])

        samples = [MetricSample.from_dict(sample) for sample in result_data]

        return cls(
            result_type=result_type,
            result=samples,
            status=status,
            error_type=error_type,
            error=error,
            warnings=warnings,
        )

    # Fallback for other formats (e.g., error responses without data section)
    return cls(
        result_type=data.get("resultType", ""),
        result=[],
        status=data.get("status", "success"),
        error_type=data.get("errorType", ""),
        error=data.get("error", ""),
        warnings=data.get("warnings", []),
    )
to_dict
to_dict() -> dict[str, Any]

Convert to dictionary.

Returns:

Type Description
dict[str, Any]

Dictionary representation

Source code in provide/foundation/integrations/openobserve/metrics_models.py
def to_dict(self) -> dict[str, Any]:
    """Convert to dictionary.

    Returns:
        Dictionary representation
    """
    result: dict[str, Any] = {
        "status": self.status,
        "data": {
            "resultType": self.result_type,
            "result": [sample.to_dict() for sample in self.result],
        },
    }

    if self.warnings:
        result["warnings"] = self.warnings

    if self.error:
        result["errorType"] = self.error_type
        result["error"] = self.error

    return result

MetricSample

A single metric sample with labels and value.

Attributes:

Name Type Description
metric dict[str, str]

Label name-value pairs

value tuple[float, str] | None

Metric value (single value for instant queries)

values list[tuple[float, str]]

List of [timestamp, value] pairs for range queries

Functions
from_dict classmethod
from_dict(data: dict[str, Any]) -> MetricSample

Create from dictionary.

Parameters:

Name Type Description Default
data dict[str, Any]

Dictionary with sample data from Prometheus API

required

Returns:

Type Description
MetricSample

MetricSample instance

Source code in provide/foundation/integrations/openobserve/metrics_models.py
@classmethod
def from_dict(cls, data: dict[str, Any]) -> MetricSample:
    """Create from dictionary.

    Args:
        data: Dictionary with sample data from Prometheus API

    Returns:
        MetricSample instance
    """
    metric = data.get("metric", {})
    value = data.get("value")
    values = data.get("values", [])

    # Convert value to tuple if present
    value_tuple = None
    if value and isinstance(value, list) and len(value) == 2:
        value_tuple = (float(value[0]), str(value[1]))

    # Convert values list
    values_list = []
    if values:
        for v in values:
            if isinstance(v, list) and len(v) == 2:
                values_list.append((float(v[0]), str(v[1])))

    return cls(
        metric=metric,
        value=value_tuple,
        values=values_list,
    )
to_dict
to_dict() -> dict[str, Any]

Convert to dictionary.

Returns:

Type Description
dict[str, Any]

Dictionary representation

Source code in provide/foundation/integrations/openobserve/metrics_models.py
def to_dict(self) -> dict[str, Any]:
    """Convert to dictionary.

    Returns:
        Dictionary representation
    """
    result: dict[str, Any] = {"metric": self.metric}

    if self.value:
        result["value"] = list(self.value)

    if self.values:
        result["values"] = [list(v) for v in self.values]

    return result