Skip to content

Commit

Permalink
Add metric framework (mars-project#2742)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongchun authored and 不涸 committed Mar 15, 2022
1 parent a79f548 commit e8e7ad7
Show file tree
Hide file tree
Showing 17 changed files with 367 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if [ -n "$WITH_CYTHON" ]; then
fi
if [ -z "$NO_COMMON_TESTS" ]; then
mkdir -p build
pytest $PYTEST_CONFIG mars/remote mars/storage mars/lib
pytest $PYTEST_CONFIG mars/remote mars/storage mars/lib mars/metric
mv .coverage build/.coverage.tileable.file

pytest $PYTEST_CONFIG --forked --ignore mars/tensor --ignore mars/dataframe \
Expand Down
14 changes: 14 additions & 0 deletions mars/metric/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
# Copyright 1999-2021 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .api import Metrics
from .api import init_metrics
41 changes: 28 additions & 13 deletions mars/metric/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Copyright 1999-2021 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from typing import Dict, Any, Optional, Tuple

from .backends.console import console_metric
from .backends.prometheus import prometheus_metric
from .backends.ray import ray_metric
Expand All @@ -14,7 +30,7 @@
}


def init_metrics(config: Dict[str, Any] = {}):
def init_metrics(config: Dict[str, Any] = None):
metric_config = config.get("metric", {}) if config else {}
global _metric_backend
_metric_backend = metric_config.get("backend", "console")
Expand All @@ -38,6 +54,9 @@ class Metrics:
"""
A factory to generate different types of metrics.
Note:
Counter, Meter and Histogram are not thread safe.
Examples
--------
>>> c1 = counter('counter1', 'A counter')
Expand All @@ -57,21 +76,17 @@ class Metrics:
"""

@staticmethod
def counter(
name, description: str = "", tag_keys: Optional[Tuple[str, ...]] = None
):
return _backends_cls[_metric_backend].CounterImpl(name, description, tag_keys)
def counter(name, description: str = "", tag_keys: Optional[Tuple[str]] = None):
return _backends_cls[_metric_backend].Counter(name, description, tag_keys)

@staticmethod
def gauge(name, description: str = "", tag_keys: Optional[Tuple[str, ...]] = None):
return _backends_cls[_metric_backend].GaugeImpl(name, description, tag_keys)
def gauge(name, description: str = "", tag_keys: Optional[Tuple[str]] = None):
return _backends_cls[_metric_backend].Gauge(name, description, tag_keys)

@staticmethod
def meter(name, description: str = "", tag_keys: Optional[Tuple[str, ...]] = None):
return _backends_cls[_metric_backend].MeterImpl(name, description, tag_keys)
def meter(name, description: str = "", tag_keys: Optional[Tuple[str]] = None):
return _backends_cls[_metric_backend].Meter(name, description, tag_keys)

@staticmethod
def histogram(
name, description: str = "", tag_keys: Optional[Tuple[str, ...]] = None
):
return _backends_cls[_metric_backend].HistogramImpl(name, description, tag_keys)
def histogram(name, description: str = "", tag_keys: Optional[Tuple[str]] = None):
return _backends_cls[_metric_backend].Histogram(name, description, tag_keys)
13 changes: 13 additions & 0 deletions mars/metric/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 1999-2021 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
13 changes: 13 additions & 0 deletions mars/metric/backends/console/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 1999-2021 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
34 changes: 27 additions & 7 deletions mars/metric/backends/console/console_metric.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
# Copyright 1999-2021 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from typing import Optional, Dict, Tuple

from ..metric import Counter, Gauge, Histogram, Meter, Metric
from ..metric import (
AbstractMetric,
AbstractCounter,
AbstractGauge,
AbstractHistogram,
AbstractMeter,
)

logger = logging.getLogger(__name__)

Expand All @@ -13,7 +33,7 @@ def __init__(
self._name = name
self._description = description
self._tag_keys = tag_keys
self._value = None
self._value = 0

def update(self, value: float = 1.0, tags: Optional[Dict[str, str]] = None):
self._value = value
Expand All @@ -30,7 +50,7 @@ def value(self):
return self._value


class ConsoleMetric(Metric):
class ConsoleMetricMixin(AbstractMetric):
@property
def value(self):
return self._metric.value
Expand All @@ -42,17 +62,17 @@ def _record(self, value=1, tags: Optional[Dict[str, str]] = None):
self._metric.update(value, tags)


class CounterImpl(ConsoleMetric, Counter):
class Counter(ConsoleMetricMixin, AbstractCounter):
pass


class GaugeImpl(ConsoleMetric, Gauge):
class Gauge(ConsoleMetricMixin, AbstractGauge):
pass


class MeterImpl(ConsoleMetric, Meter):
class Meter(ConsoleMetricMixin, AbstractMeter):
pass


class HistogramImpl(ConsoleMetric, Histogram):
class Histogram(ConsoleMetricMixin, AbstractHistogram):
pass
13 changes: 13 additions & 0 deletions mars/metric/backends/console/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 1999-2021 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
51 changes: 41 additions & 10 deletions mars/metric/backends/console/tests/test_console_metric.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,63 @@
from ..console_metric import CounterImpl, GaugeImpl, MeterImpl, HistogramImpl
# Copyright 1999-2021 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ..console_metric import Counter, Gauge, Meter, Histogram


def test_counter():
c = CounterImpl("test_counter", "A test counter", ("service", "tenant"))
c.record(1, {"service": "mars", "tenant": "test"})
c.record(2, {"service": "mars", "tenant": "test"})
c = Counter("test_counter", "A test counter", ("service", "tenant"))
assert c.name == "test_counter"
assert c.description == "A test counter"
assert c.tag_keys == ("service", "tenant")
assert c.type == "counter"
c.record(1, {"service": "mars", "tenant": "test"})
c.record(2, {"service": "mars", "tenant": "test"})
assert c.value == 3


def test_gauge():
g = GaugeImpl("test_gauge", "A test gauge")
g.record(1)
g = Gauge("test_gauge", "A test gauge")
assert g.name == "test_gauge"
assert g.description == "A test gauge"
assert g.tag_keys == ()
assert g.type == "gauge"
g.record(1)
assert g.value == 1
g.record(2)
assert g.value == 2


def test_meter():
m = MeterImpl("test_meter")
m.record(1)
m = Meter("test_meter")
assert m.name == "test_meter"
assert m.description == ""
assert m.tag_keys == ()
assert m.type == "meter"
m.record(1)
assert m.value == 0
m.record(2001)
assert m.value > 0


def test_histogram():
h = HistogramImpl("test_histogram")
h.record(1)
h = Histogram("test_histogram")
assert h.name == "test_histogram"
assert h.description == ""
assert h.tag_keys == ()
assert h.type == "histogram"
h.record(1)
assert h.value == 0
for i in range(2002):
h.record(1)
assert h.value > 0
63 changes: 28 additions & 35 deletions mars/metric/backends/metric.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
# Copyright 1999-2021 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import time

from abc import ABC, abstractmethod
from threading import Lock
from typing import Dict, Optional, Tuple

_THRESHOLD = 2000
_RECORDED_INTERVAL_SECS = 1


class MutexValue(object):
"""A float protected by a mutex."""

def __init__(self):
self._value = 0.0
self._lock = Lock()

def inc(self, amount):
with self._lock:
self._value += amount

def set(self, value):
with self._lock:
self._value = value

def get(self):
with self._lock:
return self._value


class Metric(ABC):
class AbstractMetric(ABC):
"""Base class of metrics."""

_type = None
Expand Down Expand Up @@ -74,7 +67,7 @@ def _record(self, value: float = 1.0, tags: Optional[Dict[str, str]] = None):
pass


class Counter(Metric, ABC):
class AbstractCounter(AbstractMetric):
"""A counter records the counts of events."""

_type = "counter"
Expand All @@ -83,14 +76,14 @@ def __init__(
self, name: str, description: str = "", tag_keys: Optional[Tuple[str]] = None
):
super().__init__(name, description, tag_keys)
self._count = MutexValue()
self._count = 0

def record(self, value=1, tags: Optional[Dict[str, str]] = None):
self._count.inc(value)
self._record(self._count.get(), tags)
self._count += value
self._record(self._count, tags)


class Gauge(Metric, ABC):
class AbstractGauge(AbstractMetric):
"""A gauge represents a single numerical value that can be
arbitrarily set.
"""
Expand All @@ -101,7 +94,7 @@ def record(self, value=1, tags: Optional[Dict[str, str]] = None):
self._record(value, tags)


class Meter(Metric, ABC):
class AbstractMeter(AbstractMetric):
"""A meter measures the rate at which a set of events occur."""

_type = "meter"
Expand All @@ -110,22 +103,22 @@ def __init__(
self, name: str, description: str = "", tag_keys: Optional[Tuple[str]] = None
):
super().__init__(name, description, tag_keys)
self._count = MutexValue()
self._count = 0
self._last_time = time.time()

def record(self, value=1, tags: Optional[Dict[str, str]] = None):
self._count.inc(value)
self._count += value
now = time.time()
past = now - self._last_time
if self._count.get() >= _THRESHOLD or past >= _RECORDED_INTERVAL_SECS:
qps = self._count.get() / past
if self._count >= _THRESHOLD or past >= _RECORDED_INTERVAL_SECS:
qps = self._count / past
self._record(qps, tags)
self._last_time = now
self._count.set(0)
self._count = 0


class Histogram(Metric, ABC):
"""A Histogram measures the distribution of values in a stream of data."""
class AbstractHistogram(AbstractMetric):
"""A histogram measures the distribution of values in a stream of data."""

_type = "histogram"

Expand Down
Loading

0 comments on commit e8e7ad7

Please sign in to comment.