Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Python server: Support optional metrics ([#828](https://github.com/mozilla/glean_parser/pull/828))

## 18.2.0

- New lint: Warn against event metrics on the `metrics` or `baseline` ping ([#823](https://github.com/mozilla/glean_parser/pull/823))
Expand Down
37 changes: 23 additions & 14 deletions glean_parser/templates/python_server.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,34 @@ class {{ ping|camelize }}ServerEventLogger:

def record(
self,
user_agent: str,
ip_address: str,
user_agent: str | None = None,
ip_address: str | None = None,
{% for metric_type, metrics in metrics_by_type.items() %}
{% if metric_type != 'event' %}
{% for metric in metrics %}
{{ metric.category }}_{{ metric.name }}: {{ metric.type|py_metric_type }},
{{ metric.category }}_{{ metric.name }}: {{ metric.type|py_metric_type }} | None = None,
{% endfor %}
{% endif %}
{% endfor %}
events: list[dict[str, Any]]
events: list[dict[str, Any]] | None = None,
Comment thread
relud marked this conversation as resolved.
) -> None:
now = datetime.now(timezone.utc)
timestamp = now.isoformat()
events = [] if events is None else events
for event in events:
event["timestamp"] = int(1000.0 * now.timestamp()) # Milliseconds since epoch
event_payload = {
"metrics": {
{% for metric_type, metrics in metrics_by_type.items() %}
{% if metric_type != 'event' %}
"{{ metric_type }}": {
{% for metric in metrics %}
"{{ metric.category }}.{{ metric.name }}": {{ metric.category }}_{{ metric.name }},
{% endfor %}
key: value
for key, value in [
{% for metric in metrics %}
("{{ metric.category }}.{{ metric.name }}", {{ metric.category }}_{{ metric.name }}),
{% endfor %}
]
if value is not None
},
{% endif %}
{% endfor %}
Expand Down Expand Up @@ -117,17 +122,17 @@ class {{ ping|camelize }}ServerEventLogger:
{% for event in metrics_by_type["event"] %}
def {{ event|record_event_function_name }}(
self,
user_agent: str,
ip_address: str,
user_agent: str | None = None,
ip_address: str | None = None,
Comment thread
relud marked this conversation as resolved.
{% for metric_type, metrics in metrics_by_type.items() %}
{% if metric_type != 'event' %}
{% for metric in metrics %}
{{ metric.category }}_{{ metric.name }}: {{ metric.type|py_metric_type }},
{{ metric.category }}_{{ metric.name }}: {{ metric.type|py_metric_type }} | None = None,
{% endfor %}
{% endif %}
{% endfor %}
{% for extra, metadata in event.extra_keys.items() %}
{{ extra }}: {{ metadata.type|py_metric_type }},
{{ extra }}: {{ metadata.type|py_metric_type }} | None = None,
{% endfor %}
) -> None:
"""
Expand Down Expand Up @@ -156,9 +161,13 @@ class {{ ping|camelize }}ServerEventLogger:
"name": "{{ event.name }}",
{% if event.extra_keys %}
"extra": {
{% for extra, metadata in event.extra_keys.items() %}
"{{ extra }}": str({{ extra }}){% if 'bool' == metadata.type|py_metric_type %}.lower(){% endif %},
{% endfor %}
key: value
for key, value in [
{% for extra, metadata in event.extra_keys.items() %}
("{{ extra }}", str({{ extra }}){% if 'bool' == metadata.type|py_metric_type %}.lower(){% endif %}),
{% endfor %}
]
if value is not None
},
{% endif %}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/test-py/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
object_state="some_object_state",
linking=True,
)

logger.record_backend_object_update()

logger.record()
47 changes: 24 additions & 23 deletions tests/test_python_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,30 @@ def test_logging(tmp_path):
"utf-8"
)

logged_output = json.loads(logged_output)
fields = logged_output["Fields"]
payload = fields["payload"]

# validate that ping envelope contains all the required fields
assert "glean-server-event" == logged_output["Type"]
assert "accounts_backend" == fields["document_namespace"]
assert "events" == fields["document_type"]
assert "1" == fields["document_version"]
assert "Mozilla/5.0 ..." == fields["user_agent"]

schema_url = (
"https://raw.githubusercontent.com/mozilla-services/"
"mozilla-pipeline-schemas/main/"
"schemas/glean/glean/glean.1.schema.json"
)

# validate that ping payload is valid against glean schema
input = io.StringIO(payload)
output = io.StringIO()
assert validate_ping.validate_ping(input, output, schema_url=schema_url) == 0, (
output.getvalue()
)
for log_line in logged_output.splitlines():
json_line = json.loads(log_line)
fields = json_line["Fields"]
payload = fields["payload"]

# validate that ping envelope contains all the required fields
assert "glean-server-event" == json_line["Type"]
assert "accounts_backend" == fields["document_namespace"]
assert "events" == fields["document_type"]
assert "1" == fields["document_version"]
assert fields["user_agent"] in ("Mozilla/5.0 ...", None)

schema_url = (
"https://raw.githubusercontent.com/mozilla-services/"
"mozilla-pipeline-schemas/main/"
"schemas/glean/glean/glean.1.schema.json"
)

# validate that ping payload is valid against glean schema
input = io.StringIO(payload)
output = io.StringIO()
assert validate_ping.validate_ping(input, output, schema_url=schema_url) == 0, (
output.getvalue()
)


def test_parser_python_server_metrics_unsupported_type(tmp_path, capsys):
Expand Down