|
| 1 | +# Licensed to Modin Development Team under one or more contributor license agreements. |
| 2 | +# See the NOTICE file distributed with this work for additional information regarding |
| 3 | +# copyright ownership. The Modin Development Team licenses this file to you under the |
| 4 | +# Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 | +# compliance with the License. You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software distributed under |
| 10 | +# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific language |
| 12 | +# governing permissions and limitations under the License. |
| 13 | + |
| 14 | +from typing import Union |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +import modin.logging |
| 19 | +import modin.pandas as pd |
| 20 | +from modin.config import MetricsMode |
| 21 | +from modin.logging.metrics import ( |
| 22 | + _metric_handlers, |
| 23 | + add_metric_handler, |
| 24 | + clear_metric_handler, |
| 25 | + emit_metric, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +class FakeTelemetryClient: |
| 30 | + |
| 31 | + def __init__(self): |
| 32 | + self._metrics = {} |
| 33 | + self._metric_handler = None |
| 34 | + |
| 35 | + def metric_handler_fail(self, name: str, value: Union[int, float]): |
| 36 | + raise KeyError("Poorly implemented metric handler") |
| 37 | + |
| 38 | + def metric_handler_pass(self, name: str, value: Union[int, float]): |
| 39 | + self._metrics[name] = value |
| 40 | + |
| 41 | + |
| 42 | +@modin.logging.enable_logging |
| 43 | +def func(do_raise): |
| 44 | + if do_raise: |
| 45 | + raise ValueError() |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture() |
| 49 | +def metric_client(): |
| 50 | + MetricsMode.enable() |
| 51 | + client = FakeTelemetryClient() |
| 52 | + yield client |
| 53 | + clear_metric_handler(client._metric_handler) |
| 54 | + MetricsMode.disable() |
| 55 | + |
| 56 | + |
| 57 | +def test_metrics_api_timings(metric_client): |
| 58 | + assert len(_metric_handlers) == 0 |
| 59 | + metric_client._metric_handler = metric_client.metric_handler_pass |
| 60 | + add_metric_handler(metric_client._metric_handler) |
| 61 | + assert len(_metric_handlers) == 1 |
| 62 | + assert _metric_handlers[0] == metric_client._metric_handler |
| 63 | + func(do_raise=False) |
| 64 | + assert len(metric_client._metrics) == 1 |
| 65 | + assert metric_client._metrics["modin.pandas-api.func"] is not None |
| 66 | + assert metric_client._metrics["modin.pandas-api.func"] > 0.0 |
| 67 | + |
| 68 | + |
| 69 | +def test_df_metrics(metric_client): |
| 70 | + metric_client._metric_handler = metric_client.metric_handler_pass |
| 71 | + add_metric_handler(metric_client._metric_handler) |
| 72 | + df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) |
| 73 | + df.sum() |
| 74 | + assert len(metric_client._metrics) == 54 |
| 75 | + assert metric_client._metrics["modin.pandas-api.dataframe.sum"] is not None |
| 76 | + assert metric_client._metrics["modin.pandas-api.dataframe.sum"] > 0.0 |
| 77 | + |
| 78 | + |
| 79 | +def test_metrics_handler_fails(metric_client): |
| 80 | + assert len(metric_client._metrics) == 0 |
| 81 | + metric_client._metric_handler = metric_client.metric_handler_fail |
| 82 | + add_metric_handler(metric_client._metric_handler) |
| 83 | + assert len(_metric_handlers) == 1 |
| 84 | + func(do_raise=False) |
| 85 | + assert len(_metric_handlers) == 0 |
| 86 | + assert len(metric_client._metrics) == 0 |
| 87 | + |
| 88 | + |
| 89 | +def test_emit_name_enforced(): |
| 90 | + MetricsMode.enable() |
| 91 | + with pytest.raises(KeyError): |
| 92 | + emit_metric("Not::A::Valid::Metric::Name", 1.0) |
| 93 | + |
| 94 | + |
| 95 | +def test_metrics_can_be_opt_out(metric_client): |
| 96 | + MetricsMode.enable() |
| 97 | + assert len(metric_client._metrics) == 0 |
| 98 | + metric_client._metric_handler = metric_client.metric_handler_pass |
| 99 | + add_metric_handler(metric_client._metric_handler) |
| 100 | + # If Metrics are disabled after the addition of a handler |
| 101 | + # no metrics are emitted |
| 102 | + MetricsMode.disable() |
| 103 | + assert len(_metric_handlers) == 1 |
| 104 | + func(do_raise=False) |
| 105 | + assert len(metric_client._metrics) == 0 |
0 commit comments