From 8e2c38b977a379e4b9a35101db91f7978ef641bc Mon Sep 17 00:00:00 2001 From: Chris H-C Date: Tue, 11 Jan 2022 16:41:23 -0500 Subject: [PATCH] bug 1745283 Support file-level tags --- CHANGELOG.md | 2 ++ glean_parser/parser.py | 6 ++++++ glean_parser/schemas/metrics.2-0-0.schema.yaml | 10 +++++++++- tests/data/metric-with-tags.yaml | 3 +++ tests/data/tags.yaml | 5 +++++ tests/test_parser.py | 11 +++++++---- 6 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a067f13f..2ad5d7cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Support global file-level tags in metrics.yaml ([bug 1745283](https://bugzilla.mozilla.org/show_bug.cgi?id=1745283)) + ## 4.3.1 - BUGFIX: Skip tags for code generation ([#409](https://github.com/mozilla/glean_parser/pull/409)) diff --git a/glean_parser/parser.py b/glean_parser/parser.py index 7d8db00e..a6afb7d3 100644 --- a/glean_parser/parser.py +++ b/glean_parser/parser.py @@ -195,6 +195,8 @@ def _instantiate_metrics( objects, and merge them into a single tree. """ global_no_lint = content.get("no_lint", []) + global_tags = content.get("$tags", []) + assert isinstance(global_tags, list) for category_key, category_val in content.items(): if category_key.startswith("$"): @@ -243,6 +245,10 @@ def _instantiate_metrics( if metric_obj is not None: metric_obj.no_lint = list(set(metric_obj.no_lint + global_no_lint)) + if len(global_tags): + metric_obj.metadata["tags"] = list( + set(metric_obj.metadata.get("tags", []) + global_tags) + ) if isinstance(filepath, Path): metric_obj.defined_in["filepath"] = str(filepath) diff --git a/glean_parser/schemas/metrics.2-0-0.schema.yaml b/glean_parser/schemas/metrics.2-0-0.schema.yaml index 37000ec9..eb2644a9 100644 --- a/glean_parser/schemas/metrics.2-0-0.schema.yaml +++ b/glean_parser/schemas/metrics.2-0-0.schema.yaml @@ -538,7 +538,7 @@ propertyNames: - not: description: "'tags' is reserved as a category name." const: tags - - enum: ['$schema'] + - enum: ['$schema', '$tags'] properties: $schema: @@ -553,6 +553,14 @@ properties: items: type: string + $tags: + title: Tags that apply to the whole file + description: | + This denotes the list of tags that apply to all metrics in this file. + type: array + items: + type: string + additionalProperties: type: object propertyNames: diff --git a/tests/data/metric-with-tags.yaml b/tests/data/metric-with-tags.yaml index a3dfea46..7cb20d40 100644 --- a/tests/data/metric-with-tags.yaml +++ b/tests/data/metric-with-tags.yaml @@ -4,6 +4,9 @@ --- $schema: moz://mozilla.org/schemas/glean/metrics/2-0-0 +$tags: + - global_tag + telemetry: client_id: type: uuid diff --git a/tests/data/tags.yaml b/tests/data/tags.yaml index ba66eef9..e05eb9ff 100644 --- a/tests/data/tags.yaml +++ b/tests/data/tags.yaml @@ -13,3 +13,8 @@ apple: description: | A red fruit. Or green. Or a mix of it. Very good in pie. + +global_tag: + description: | + A very uninteresting global tag. + Alas, it has no colour. diff --git a/tests/test_parser.py b/tests/test_parser.py index 0101f41a..acecef83 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -110,7 +110,7 @@ def test_parser_schema_violation(): the given schemas 'gleantest.with.way.too.long.category.name' is too long 'gleantest.with.way.too.long.category.name' is not one of - ['$schema'] + ['$schema', '$tags'] """, """ ``` @@ -569,9 +569,12 @@ def test_tags(): assert errors == [] assert len(all_metrics.value) == 1 - assert all_metrics.value["telemetry"]["client_id"].metadata == { - "tags": ["banana", "apple"] - } + assert set(all_metrics.value["telemetry"]["client_id"].metadata.keys()) == set( + ["tags"] + ) + assert set(all_metrics.value["telemetry"]["client_id"].metadata["tags"]) == set( + ["banana", "apple", "global_tag"] + ) def test_custom_expires():