Skip to content

Commit

Permalink
chore: Add metric type for Prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
fregataa committed Oct 15, 2024
1 parent b9c749b commit 263da15
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/ai/backend/common/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import itertools
import math
import numbers
import textwrap
import uuid
from abc import ABCMeta, abstractmethod
from collections import UserDict, defaultdict, namedtuple
from collections.abc import Iterable
from contextvars import ContextVar
from dataclasses import dataclass
from decimal import Decimal
Expand Down Expand Up @@ -1228,3 +1230,61 @@ class ModelServiceProfile:
),
RuntimeVariant.CMD: ModelServiceProfile(name="Predefined Image Command"),
}


class PromMetricPrimitive(enum.StrEnum):
counter = enum.auto()
gauge = enum.auto()
histogram = enum.auto()
summary = enum.auto()
untyped = enum.auto()


class PromMetric(metaclass=ABCMeta):
@abstractmethod
def metric_value_string(self, metric_name: str, primitive: PromMetricPrimitive) -> str:
pass


MetricType = TypeVar("MetricType", bound=PromMetric)


class PromMetricGroup(Generic[MetricType], metaclass=ABCMeta):
"""
Support text format to expose metric data to Prometheus.
ref: https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md
"""

def __init__(self, metrics: Iterable[MetricType]) -> None:
self.metrics = metrics

@property
@abstractmethod
def metric_name(self) -> str:
pass

@property
@abstractmethod
def description(self) -> str:
pass

@property
@abstractmethod
def metric_primitive(self) -> PromMetricPrimitive:
pass

def help_string(self) -> str:
return f"HELP {self.metric_name} {self.description}"

def type_string(self) -> str:
return f"TYPE {self.metric_name} {self.metric_primitive.value}"

def metric_string(self) -> str:
result = textwrap.dedent(f"""
{self.help_string()}
{self.type_string()}
""")
for metric in self.metrics:
val = metric.metric_value_string(self.metric_name, self.metric_primitive)
result += f"{val}\n"
return result

0 comments on commit 263da15

Please sign in to comment.