Python: Labeled boolean and labeled string metric type

This commit is contained in:
Michael Droettboom 2020-03-09 18:11:24 -04:00
Родитель a30e58cebe
Коммит 059bd11b0a
7 изменённых файлов: 134 добавлений и 4 удалений

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

@ -9,7 +9,7 @@
* Python:
* **Breaking Change**: The `glean.util` and `glean.hardware` modules, which
were unintentionally public, have been made private.
* The timing distribution and memory distribution metric types are now supported in Python ([#762](https://github.com/mozilla/glean/pull/762), [#763](https://github.com/mozilla/glean/pull/763))
* The timing distribution, memory distribution, string list, labeled boolean and labeled string metric types are now supported in Python ([#762](https://github.com/mozilla/glean/pull/762), [#763](https://github.com/mozilla/glean/pull/763), [#765](https://github.com/mozilla/glean/pull/765), [#766](https://github.com/mozilla/glean/pull/766))
# v25.1.0 (2020-02-26)

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

@ -129,4 +129,4 @@ assert 1 == metrics.stability.crash_count.test_get_num_recorded_errors(
* Kotlin API docs [`LabeledMetricType`](../../../javadoc/glean/mozilla.telemetry.glean.private/-labeled-metric-type/index.html), [`CounterMetricType`](../../../javadoc/glean/mozilla.telemetry.glean.private/-counter-metric-type/index.html)
* Swift API docs: [`LabeledMetricType`](../../../swift/Classes/LabeledMetricType.html), [`CounterMetricType`](../../../swift/Classes/CounterMetricType.html)
* [Python API docs](../../../python/glean/metrics/labeled.html)
* Python API docs: [`LabeledMetricBase`](../../../python/glean/metrics/labeled.html), [`CounterMetricType`](../../../python/glean/metrics/counter.html)

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

@ -62,6 +62,29 @@ XCTAssert(Login.errorsByStage["server_auth"].testHasValue())
</div>
<div data-lang="Python" class="tab">
```Python
from glean import load_metrics
metrics = load_metrics("metrics.yaml")
metrics.login.errors_by_stage["server_auth"].set("Invalid password")
```
There are test APIs available too:
```Python
# Was anything recorded?
assert metrics.login.errors_by_stage["server_auth"].test_has_value()
# Were there any invalid labels?
assert 1 == metrics.login.errors_by_stage.test_get_num_recorded_errors(
ErrorType.INVALID_LABEL
)
```
</div>
{{#include ../../tab_footer.md}}
## Limits
@ -89,3 +112,4 @@ XCTAssert(Login.errorsByStage["server_auth"].testHasValue())
* Kotlin API docs: [`LabeledMetricType`](../../../javadoc/glean/mozilla.telemetry.glean.private/-labeled-metric-type/index.html), [`StringMetricType`](../../../javadoc/glean/mozilla.telemetry.glean.private/-string-metric-type/index.html)
* Swift API docs: [`LabeledMetricType`](../../../swift/Classes/LabeledMetricType.html), [`StringMetricType`](../../../swift/Classes/StringMetricType.html)
* Python API docs: [`LabeledMetricBase`](../../../python/glean/metrics/labeled.html), [`StringMetricType`](../../../python/glean/metrics/string.html)

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

@ -34,7 +34,9 @@ _TYPE_MAPPING = {
"counter": metrics.CounterMetricType,
"datetime": metrics.DatetimeMetricType,
"event": metrics.EventMetricType,
"labeled_boolean": metrics.LabeledBooleanMetricType,
"labeled_counter": metrics.LabeledCounterMetricType,
"labeled_string": metrics.LabeledStringMetricType,
"memory_unit": metrics.MemoryDistributionMetricType,
"ping": metrics.PingType,
"string": metrics.StringMetricType,

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

@ -13,7 +13,11 @@ from .counter import CounterMetricType
from .datetime import DatetimeMetricType
from .event import EventMetricType, RecordedEventData
from .experiment import RecordedExperimentData
from .labeled import LabeledCounterMetricType
from .labeled import (
LabeledBooleanMetricType,
LabeledCounterMetricType,
LabeledStringMetricType,
)
from .lifetime import Lifetime
from .memory_distribution import MemoryDistributionMetricType
from .memoryunit import MemoryUnit
@ -31,7 +35,9 @@ __all__ = [
"CounterMetricType",
"DatetimeMetricType",
"EventMetricType",
"LabeledBooleanMetricType",
"LabeledCounterMetricType",
"LabeledStringMetricType",
"Lifetime",
"MemoryDistributionMetricType",
"MemoryUnit",

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

@ -8,8 +8,10 @@ from typing import Callable, List, Optional, Set, Type
from .. import _ffi
from .boolean import BooleanMetricType
from .counter import CounterMetricType
from .lifetime import Lifetime
from .string import StringMetricType
from ..testing import ErrorType
@ -122,6 +124,15 @@ class LabeledMetricBase(abc.ABC):
)
class LabeledBooleanMetricType(LabeledMetricBase):
_submetric_type = BooleanMetricType
_metric_type_instantiator = _ffi.lib.glean_new_labeled_boolean_metric
_submetric_type_instantiator = _ffi.lib.glean_labeled_boolean_metric_get
_test_get_num_recorded_errors_ffi = (
_ffi.lib.glean_labeled_boolean_test_get_num_recorded_errors
)
class LabeledCounterMetricType(LabeledMetricBase):
_submetric_type = CounterMetricType
_metric_type_instantiator = _ffi.lib.glean_new_labeled_counter_metric
@ -131,4 +142,17 @@ class LabeledCounterMetricType(LabeledMetricBase):
)
__all__ = ["LabeledCounterMetricType"]
class LabeledStringMetricType(LabeledMetricBase):
_submetric_type = StringMetricType
_metric_type_instantiator = _ffi.lib.glean_new_labeled_string_metric
_submetric_type_instantiator = _ffi.lib.glean_labeled_string_metric_get
_test_get_num_recorded_errors_ffi = (
_ffi.lib.glean_labeled_string_test_get_num_recorded_errors
)
__all__ = [
"LabeledBooleanMetricType",
"LabeledCounterMetricType",
"LabeledStringMetricType",
]

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

@ -60,6 +60,80 @@ def test_labeled_counter_type(ping_schema_url):
)
def test_labeled_boolean_type(ping_schema_url):
labeled_boolean_metric = metrics.LabeledBooleanMetricType(
disabled=False,
category="telemetry",
lifetime=Lifetime.APPLICATION,
name="labeled_boolean_metric",
send_in_pings=["metrics"],
)
labeled_boolean_metric["label1"].set(True)
labeled_boolean_metric["label2"].set(False)
assert labeled_boolean_metric["label1"].test_has_value()
assert labeled_boolean_metric["label1"].test_get_value()
assert labeled_boolean_metric["label2"].test_has_value()
assert not labeled_boolean_metric["label2"].test_get_value()
json_content = Glean.test_collect(_builtins.pings.metrics)
assert 0 == validate_ping.validate_ping(
io.StringIO(json_content), sys.stdout, schema_url=ping_schema_url
)
tree = json.loads(json_content)
assert tree["metrics"]["labeled_boolean"]["telemetry.labeled_boolean_metric"][
"label1"
]
assert not tree["metrics"]["labeled_boolean"]["telemetry.labeled_boolean_metric"][
"label2"
]
def test_labeled_string_type(ping_schema_url):
labeled_string_metric = metrics.LabeledStringMetricType(
disabled=False,
category="telemetry",
lifetime=Lifetime.APPLICATION,
name="labeled_string_metric",
send_in_pings=["metrics"],
)
labeled_string_metric["label1"].set("foo")
labeled_string_metric["label2"].set("bar")
assert labeled_string_metric["label1"].test_has_value()
assert "foo" == labeled_string_metric["label1"].test_get_value()
assert labeled_string_metric["label2"].test_has_value()
assert "bar" == labeled_string_metric["label2"].test_get_value()
json_content = Glean.test_collect(_builtins.pings.metrics)
assert 0 == validate_ping.validate_ping(
io.StringIO(json_content), sys.stdout, schema_url=ping_schema_url
)
tree = json.loads(json_content)
assert (
"foo"
== tree["metrics"]["labeled_string"]["telemetry.labeled_string_metric"][
"label1"
]
)
assert (
"bar"
== tree["metrics"]["labeled_string"]["telemetry.labeled_string_metric"][
"label2"
]
)
def test_other_label_with_predefined_labels(ping_schema_url):
labeled_counter_metric = metrics.LabeledCounterMetricType(
disabled=False,