1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
# 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 mach.decorators import Command, CommandArgument
TODO_FILL_DESCRIPTION = "TODO: Fill in this description. You _can_ use **markdown**."
GENERATED_BY_DESCRIPTION = (
"This {metric} was generated to correspond to the Legacy Telemetry {kind} {name}."
)
DESCRIPTION_INDENT = " "
LIST_INDENT = " - "
BUG_URL_TEMPLATE = "https://bugzil.la/{}"
GLEAN_METRIC_TEMPLATE = """
{name}:
type: {metric_type}
description: >
{multiline_description}
bugs:{bugs_alias}{bugs_list}
data_reviews:{data_alias}{bugs_list}
notification_emails:{emails_alias}{emails_list}
expires: {expiry}
{extra}telemetry_mirror: {legacy_enum}
""".strip(
"\n"
)
EXTRA_KEY_DESCRIPTION_INDENT = DESCRIPTION_INDENT + " "
VALUE_EXTRA_DESCRIPTION = "The `value` of the event. Mirrors to the Legacy Telemetry event's `value` parameter."
EXTRA_KEY_TEMPLATE = """
{name}:
description: >
{description}
type: string
""".strip(
"\n"
)
@Command(
"gifft",
category="misc",
description="Generate a Glean metric definition for a given Legacy Telemetry probe. Only supports Events and Scalars.",
)
@CommandArgument(
"telemetry_probe_name", help="Telemetry probe name (e.g. readermode.view)"
)
def mach_gifft(command_context, telemetry_probe_name):
from os import path
telemetrydir = path.join(
command_context.topsrcdir, "toolkit", "components", "telemetry"
)
import re
def to_snake_case(camel):
return re.sub("([A-Z]+)", r"_\1", camel).lower().replace("__", "_").strip("_")
import itertools
import sys
sys.path.append(path.join(telemetrydir, "build_scripts"))
import textwrap
from mozparsers import parse_events, parse_scalars
events = parse_events.load_events(path.join(telemetrydir, "Events.yaml"), True)
for e in events:
if e.category + "." + e.name == telemetry_probe_name:
# There are four levels of identification in Legacy Telemetry event
# definitions: category, name/family, method, and object.
# If not present, the `method` property will supply the name/family.
# GIFFT will mirror to a specific identified C++ enum, which means
# we need to generate Glean events for every combination of method
# and object.
category = e.category
emails_alias = bugs_alias = data_alias = extra_alias = ""
print(f"{to_snake_case(category)}:")
for m, o in itertools.product(e.methods, e.objects):
legacy_name = category + "." + m + "#" + o
name = m + "_" + o
description = e._definition.get("description", TODO_FILL_DESCRIPTION)
multiline_description = textwrap.fill(
description,
width=80 - len(DESCRIPTION_INDENT),
initial_indent=DESCRIPTION_INDENT,
subsequent_indent=DESCRIPTION_INDENT,
)
multiline_description += "\n"
multiline_description += textwrap.fill(
GENERATED_BY_DESCRIPTION.format(
metric="event", kind="event", name=legacy_name
),
width=80 - len(DESCRIPTION_INDENT),
initial_indent=DESCRIPTION_INDENT,
subsequent_indent=DESCRIPTION_INDENT,
)
alias_prefix = category.replace(".", "_") + f"_{m}_"
if bugs_alias:
bugs_list = ""
else:
bugs_alias = f"{alias_prefix}bugs"
data_alias = f"{alias_prefix}data_reviews"
bugs_list = "\n" + textwrap.indent(
"\n".join(
map(
lambda b: BUG_URL_TEMPLATE.format(b),
e._definition.get("bug_numbers", []),
)
),
LIST_INDENT,
)
if emails_alias:
emails_list = ""
else:
emails_alias = f"{alias_prefix}emails"
emails_list = "\n" + textwrap.indent(
"\n".join(e._definition.get("notification_emails", [])),
LIST_INDENT,
)
# expiry_version is a string like `"123.0a1"` or `"never"`,
# but Glean wants a number like `123` or `never`.
expiry = e.expiry_version.strip('"').split(".")[0]
if extra_alias:
extra_keys = ""
else:
extra_alias = f"{alias_prefix}extra"
multiline_extra_description = textwrap.fill(
VALUE_EXTRA_DESCRIPTION,
width=80 - len(EXTRA_KEY_DESCRIPTION_INDENT),
initial_indent=EXTRA_KEY_DESCRIPTION_INDENT,
subsequent_indent=EXTRA_KEY_DESCRIPTION_INDENT,
)
extra_keys = "\n" + EXTRA_KEY_TEMPLATE.format(
name="value", description=multiline_extra_description
)
for key_name, key_description in e._definition.get(
"extra_keys", {}
).items():
extra_keys += "\n"
extra_keys += EXTRA_KEY_TEMPLATE.format(
name=key_name,
description=textwrap.indent(
key_description, EXTRA_KEY_DESCRIPTION_INDENT
),
)
legacy_enum = (
parse_events.convert_to_cpp_identifier(category, ".") + "_"
)
legacy_enum += parse_events.convert_to_cpp_identifier(m, "_") + "_"
legacy_enum += parse_events.convert_to_cpp_identifier(o, "_")
def generate_alias(list, alias):
if len(e.methods) == 1 and len(e.objects) == 1:
return ""
if list:
return f" &{alias}"
else:
return f" *{alias}"
print(
GLEAN_METRIC_TEMPLATE.format(
name=to_snake_case(name),
metric_type="event",
multiline_description=multiline_description,
bugs_alias=generate_alias(bugs_list, bugs_alias),
bugs_list=bugs_list,
data_alias=generate_alias(bugs_list, data_alias),
emails_alias=generate_alias(emails_list, emails_alias),
emails_list=emails_list,
expiry=expiry,
extra=f"extra_keys:{generate_alias(extra_keys, extra_alias)}{extra_keys}\n ",
legacy_enum=legacy_enum,
)
)
print() # We want a newline between event definitions.
break
scalars = parse_scalars.load_scalars(path.join(telemetrydir, "Scalars.yaml"))
for s in scalars:
if s.category + "." + s.name == telemetry_probe_name:
category = s.category
name = s.name
legacy_name = category + "." + name
description = s._definition.get("description", TODO_FILL_DESCRIPTION)
multiline_description = textwrap.fill(
description,
width=80 - len(DESCRIPTION_INDENT),
initial_indent=DESCRIPTION_INDENT,
subsequent_indent=DESCRIPTION_INDENT,
)
multiline_description += "\n"
multiline_description += textwrap.fill(
GENERATED_BY_DESCRIPTION.format(
name=legacy_name, metric="metric", kind="scalar"
),
width=80 - len(DESCRIPTION_INDENT),
initial_indent=DESCRIPTION_INDENT,
subsequent_indent=DESCRIPTION_INDENT,
)
bugs_list = "\n" + textwrap.indent(
"\n".join(
map(
lambda b: BUG_URL_TEMPLATE.format(b),
s._definition.get("bug_numbers", []),
)
),
LIST_INDENT,
)
emails_list = "\n" + textwrap.indent(
"\n".join(s._definition.get("notification_emails", [])),
LIST_INDENT,
)
# expires is a string like `"123.0a1"` or `"never"`,
# but Glean wants a number like `123` or `never`.
expiry = s.expires.strip('"').split(".")[0]
metric_type = s.kind
if metric_type == "uint":
if s.keyed:
metric_type = "labeled_counter"
else:
# The caller will replace "counter" with "quantity" in the output when needed.
metric_type = "counter"
elif s.keyed:
if metric_type == "boolean":
metric_type = "labeled_boolean"
elif metric_type == "string":
metric_type = "labeled_string"
extra = ""
if s.keys:
extra = (
"labels:\n"
+ "\n".join(map(lambda key: " - " + key, s.keys))
+ "\n "
)
print(f"{to_snake_case(category)}:")
print(
GLEAN_METRIC_TEMPLATE.format(
name=to_snake_case(name),
metric_type=metric_type,
multiline_description=multiline_description,
bugs_alias="",
bugs_list=bugs_list,
data_alias="",
emails_alias="",
emails_list=emails_list,
extra=extra,
expiry=expiry,
legacy_enum=s.enum_label,
)
)
print() # We want a newline between metric definitions.
|