[Python] Migrate quantity metric to UniFFI

This commit is contained in:
Jan-Erik Rediger 2022-02-14 11:52:13 +01:00 коммит произвёл Jan-Erik Rediger
Родитель 0525846ef8
Коммит 5d0557b033
2 изменённых файлов: 44 добавлений и 175 удалений

Просмотреть файл

@ -1,135 +0,0 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from typing import List, Optional
from .. import _ffi
from .._dispatcher import Dispatcher
from ..testing import ErrorType
from .lifetime import Lifetime
class QuantityMetricType:
"""
This implements the developer facing API for recording quantity metrics.
Instances of this class type are automatically generated by
`glean.load_metrics`, allowing developers to record values that were
previously registered in the metrics.yaml file.
The quantity API only exposes the `QuantityMetricType.set` method, which
takes care of validating the input data and making sure that limits are
enforced.
"""
def __init__(
self,
disabled: bool,
category: str,
lifetime: Lifetime,
name: str,
send_in_pings: List[str],
):
self._disabled = disabled
self._send_in_pings = send_in_pings
self._handle = _ffi.lib.glean_new_quantity_metric(
_ffi.ffi_encode_string(category),
_ffi.ffi_encode_string(name),
_ffi.ffi_encode_vec_string(send_in_pings),
len(send_in_pings),
lifetime.value,
disabled,
)
def __del__(self):
if getattr(self, "_handle", 0) != 0:
_ffi.lib.glean_destroy_quantity_metric(self._handle)
def set(self, value: int) -> None:
"""
Set a quantity value.
Args:
value (int): The value to set. Must be non-negative.
"""
if self._disabled:
return
@Dispatcher.launch
def set():
_ffi.lib.glean_quantity_set(self._handle, value)
def test_has_value(self, ping_name: Optional[str] = None) -> bool:
"""
Tests whether a value is stored for the metric for testing purposes
only.
Args:
ping_name (str): (default: first value in send_in_pings) The name
of the ping to retrieve the metric for.
Returns:
has_value (bool): True if the metric value exists.
"""
if ping_name is None:
ping_name = self._send_in_pings[0]
return bool(
_ffi.lib.glean_quantity_test_has_value(
self._handle, _ffi.ffi_encode_string(ping_name)
)
)
def test_get_value(self, ping_name: Optional[str] = None) -> int:
"""
Returns the stored value for testing purposes only.
Args:
ping_name (str): (default: first value in send_in_pings) The name
of the ping to retrieve the metric for.
Returns:
value (int): value of the stored metric.
"""
if ping_name is None:
ping_name = self._send_in_pings[0]
if not self.test_has_value(ping_name):
raise ValueError("metric has no value")
return _ffi.lib.glean_quantity_test_get_value(
self._handle, _ffi.ffi_encode_string(ping_name)
)
def test_get_num_recorded_errors(
self, error_type: ErrorType, ping_name: Optional[str] = None
) -> int:
"""
Returns the number of errors recorded for the given metric.
Args:
error_type (ErrorType): The type of error recorded.
ping_name (str): (default: first value in send_in_pings) The name
of the ping to retrieve the metric for.
Returns:
num_errors (int): The number of errors recorded for the metric for
the given error type.
"""
if ping_name is None:
ping_name = self._send_in_pings[0]
return _ffi.lib.glean_quantity_test_get_num_recorded_errors(
self._handle,
error_type.value,
_ffi.ffi_encode_string(ping_name),
)
__all__ = ["QuantityMetricType"]

Просмотреть файл

@ -2,113 +2,117 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import pytest
from glean import metrics
from glean.metrics import Lifetime
from glean.metrics import Lifetime, CommonMetricData
from glean import testing
def test_the_api_saves_to_its_storage_engine():
# Define a quantity metric, which will be stored in "store1"
quantity_metric = metrics.QuantityMetricType(
disabled=False,
category="telemetry",
lifetime=Lifetime.APPLICATION,
name="quantity_metric",
send_in_pings=["store1"],
CommonMetricData(
disabled=False,
category="telemetry",
lifetime=Lifetime.APPLICATION,
name="quantity_metric",
send_in_pings=["store1"],
dynamic_label=None,
)
)
assert quantity_metric.test_has_value() is False
assert quantity_metric.test_get_value() is None
quantity_metric.set(1)
# Check that the metric was properly recorded
assert quantity_metric.test_has_value() is True
assert 1 == quantity_metric.test_get_value()
quantity_metric.set(10)
# Check that the metric was properly overwritten
assert quantity_metric.test_has_value() is True
assert 10 == quantity_metric.test_get_value()
def test_disabled_quantities_must_not_record_data():
# Define a quantity metric, which will be stored in "store1"
quantity_metric = metrics.QuantityMetricType(
disabled=True,
category="telemetry",
lifetime=Lifetime.APPLICATION,
name="quantity_metric",
send_in_pings=["store1"],
CommonMetricData(
disabled=True,
category="telemetry",
lifetime=Lifetime.APPLICATION,
name="quantity_metric",
send_in_pings=["store1"],
dynamic_label=None,
)
)
# Attempt to increment the quantity
quantity_metric.set(1)
# Check that nothing was recorded
assert quantity_metric.test_has_value() is False
assert quantity_metric.test_get_value() is None
def test_get_value_throws_value_error_if_nothing_is_stored():
# Define a quantity metric, which will be stored in "store1"
quantity_metric = metrics.QuantityMetricType(
disabled=True,
category="telemetry",
lifetime=Lifetime.APPLICATION,
name="quantity_metric",
send_in_pings=["store1"],
CommonMetricData(
disabled=True,
category="telemetry",
lifetime=Lifetime.APPLICATION,
name="quantity_metric",
send_in_pings=["store1"],
dynamic_label=None,
)
)
with pytest.raises(ValueError):
quantity_metric.test_get_value()
assert not quantity_metric.test_get_value()
def test_api_saves_to_secondary_pings():
# Define a quantity metric, which will be stored in "store1" and "store2"
quantity_metric = metrics.QuantityMetricType(
disabled=False,
category="telemetry",
lifetime=Lifetime.APPLICATION,
name="quantity_metric",
send_in_pings=["store1", "store2"],
CommonMetricData(
disabled=False,
category="telemetry",
lifetime=Lifetime.APPLICATION,
name="quantity_metric",
send_in_pings=["store1", "store2"],
dynamic_label=None,
)
)
quantity_metric.set(1)
# Check that the metric was properly recorded on the second ping
assert quantity_metric.test_has_value("store2")
assert 1 == quantity_metric.test_get_value("store2")
quantity_metric.set(10)
# Check that the metric was properly overwritten on the second ping
assert quantity_metric.test_has_value("store2")
assert 10 == quantity_metric.test_get_value("store2")
def test_negative_values_are_not_counted():
# Define a quantity metric, which will be stored in "store1"
quantity_metric = metrics.QuantityMetricType(
disabled=False,
category="telemetry",
lifetime=Lifetime.APPLICATION,
name="quantity_metric",
send_in_pings=["store1"],
CommonMetricData(
disabled=False,
category="telemetry",
lifetime=Lifetime.APPLICATION,
name="quantity_metric",
send_in_pings=["store1"],
dynamic_label=None,
)
)
quantity_metric.set(1)
# Check that the metric was properly recorded
assert quantity_metric.test_has_value("store1")
assert 1 == quantity_metric.test_get_value("store1")
quantity_metric.set(-10)
# Check that the quantity was NOT recorded
assert quantity_metric.test_has_value("store1")
assert 1 == quantity_metric.test_get_value("store1")
assert 1 == quantity_metric.test_get_num_recorded_errors(
testing.ErrorType.INVALID_VALUE