Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend go_server outputter to support custom pings #758

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Bugfix: Remove unused keyword argument from exception ([#755](https://github.com/mozilla/glean_parser/pull/755))
- Add Go log outputter support for custom pings (`go_server`) ([#758](https://github.com/mozilla/glean_parser/pull/758))

## 15.0.1

Expand Down
42 changes: 20 additions & 22 deletions glean_parser/go_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
The generated code creates the following:
* Two methods for logging an Event metric
one with and one without user request info specified
* Two methods for logging each custom ping
one with and one without user request info specified
* Both event and ping pairs of methods, if the events ping and custom pings are both defined
dmueller marked this conversation as resolved.
Show resolved Hide resolved
"""

from collections import defaultdict
Expand All @@ -32,7 +35,11 @@

# Adding a metric here will require updating the `generate_metric_type` function
# and require adjustments to `metrics` variables the the template.
SUPPORTED_METRIC_TYPES = ["string", "quantity", "event", "datetime"]
SUPPORTED_METRIC_TYPES = ["string", "quantity", "event", "datetime", "boolean"]


def generate_ping_type_name(ping_name: str) -> str:
return f"Ping{util.Camelize(ping_name)}"
akkomar marked this conversation as resolved.
Show resolved Hide resolved


def generate_event_type_name(metric: metrics.Metric) -> str:
Expand Down Expand Up @@ -87,6 +94,7 @@ def output_go(
template = util.get_jinja2_template(
"go_server.jinja2",
filters=(
("ping_type_name", generate_ping_type_name),
("event_type_name", generate_event_type_name),
("event_extra_name", generate_extra_name),
("metric_name", generate_metric_name),
Expand All @@ -96,15 +104,6 @@ def output_go(
),
)

PING_METRIC_ERROR_MSG = (
" Server-side environment is simplified and only supports the events ping type."
+ " You should not be including pings.yaml with your parser call"
+ " or referencing any other pings in your metric configuration."
)
if "pings" in objs:
print("❌ Ping definition found." + PING_METRIC_ERROR_MSG)
return

# Go through all metrics in objs and build a map of
# ping->list of metric categories->list of metrics
# for easier processing in the template.
Expand All @@ -120,29 +119,28 @@ def output_go(
+ " metric type."
)
continue

for ping in metric.send_in_pings:
if ping != "events":
(
print(
"❌ Non-events ping reference found."
+ PING_METRIC_ERROR_MSG
+ f"Ignoring the {ping} ping type."
)
)
continue
metrics_by_type = ping_to_metrics[ping]
metrics_list = metrics_by_type.setdefault(metric.type, [])
metrics_list.append(metric)

if "event" not in ping_to_metrics["events"]:
print("❌ No event metrics found...at least one event metric is required")
PING_METRIC_ERROR_MSG = (
" Server-side environment is simplified and this"
+ " parser doesn't generate individual metric files. Make sure to pass all"
+ " your ping and metric definitions in a single invocation of the parser."
)
if not ping_to_metrics:
print("❌ No pings with metrics found." + PING_METRIC_ERROR_MSG)
return

extension = ".go"
filepath = output_dir / ("server_events" + extension)
with filepath.open("w", encoding="utf-8") as fd:
fd.write(
template.render(
parser_version=__version__, events_ping=ping_to_metrics["events"]
parser_version=__version__,
akkomar marked this conversation as resolved.
Show resolved Hide resolved
pings=ping_to_metrics,
events_ping=ping_to_metrics["events"]
)
)
80 changes: 64 additions & 16 deletions glean_parser/templates/go_server.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (g GleanEventsLogger) createPing(documentType string, config RequestInfo, p
}
}

// method called by each event method.
// method called by each event or custom ping method.
// construct the ping, wrap it in the envelope, and print to stdout
func (g GleanEventsLogger) record(
documentType string,
Expand Down Expand Up @@ -159,19 +159,67 @@ func (g GleanEventsLogger) record(
}
fmt.Println(string(envelopeJson))
}
{# struct & methods for submitting custom pings #}
dmueller marked this conversation as resolved.
Show resolved Hide resolved
{% for ping, metrics_by_type in pings.items() %}
{% if ping != "events" %}

type {{ ping|ping_type_name }} struct {
{% for metric_type, metrics in metrics_by_type.items() %}
{# TODO: if custom pings need to support including any event metric #}
{% if metric_type != 'event' %}
{% for metric in metrics %}
{{ metric|metric_argument_name }} {{ metric_type|go_metric_type}} // {{ metric.description|clean_string }}
{% endfor %}
{% endif %}
{% endfor %}
}

// Record and submit a {{ ping|ping_type_name }} custom Ping
dmueller marked this conversation as resolved.
Show resolved Hide resolved
func (g GleanEventsLogger) Record{{ ping|ping_type_name }}(
requestInfo RequestInfo,
params {{ ping|ping_type_name }},
) {
var metrics = metrics{
{% for metric_type, metrics in metrics_by_type.items() %}
{% if metric_type != 'event' %}
"{{ metric_type }}": {
{% for metric in metrics %}
{% if metric_type == 'datetime' %}
"{{ metric|metric_name }}": params.{{ metric|metric_argument_name }}.Format("2006-01-02T15:04:05.000Z"),
{% else %}
"{{ metric|metric_name }}": params.{{ metric|metric_argument_name }},
{% endif %}
{% endfor %}
},
{% endif %}
{% endfor %}
}
g.record("{{ ping }}", requestInfo, metrics, []gleanEvent{})
}

// Record and submit a {{ ping|ping_type_name }} custom Ping omitting user request info
dmueller marked this conversation as resolved.
Show resolved Hide resolved
func (g GleanEventsLogger) Record{{ ping|ping_type_name}}WithoutUserInfo(
params {{ ping|ping_type_name}},
) {
g.Record{{ ping|ping_type_name }}(defaultRequestInfo, params)
}
{% endif %}
{% endfor %}
{% if events_ping %}{# defined, not empty, not false #}
{# struct & methods for submitting "events" ping #}
{% for event in events_ping["event"] %}

type {{ event|event_type_name }} struct {
{% for metric_type, metrics in events_ping.items() %}
{% if metric_type != 'event' %}
{% for metric in metrics %}
{{ metric|metric_argument_name }} {{ metric.type|go_metric_type }} // {{ metric.description|clean_string }}
{% endfor %}
{% endif %}
{% endfor %}
{% for extra, metadata in event.extra_keys.items() %}
{{ extra|event_extra_name }} {{ metadata.type|go_metric_type }} // {{ metadata.description|clean_string }}
{% endfor %}
{% for metric_type, metrics in events_ping.items() %}
{% if metric_type != 'event' %}
{% for metric in metrics %}
{{ metric|metric_argument_name }} {{ metric.type|go_metric_type }} // {{ metric.description|clean_string }}
{% endfor %}
{% endif %}
{% endfor %}
{% for extra, metadata in event.extra_keys.items() %}
{{ extra|event_extra_name }} {{ metadata.type|go_metric_type }} // {{ metadata.description|clean_string }}
{% endfor %}
}

// Record and submit an {{ event|event_type_name }} event.
Expand All @@ -182,17 +230,17 @@ func (g GleanEventsLogger) Record{{ event|event_type_name }}(
) {
var metrics = metrics{
{% for metric_type, metrics in events_ping.items() %}
{% if metric_type != 'event' %}
{% if metric_type != 'event' %}
"{{ metric_type }}": {
{% for metric in metrics %}
{% if metric_type =='datetime' %}
{% if metric_type == 'datetime' %}
"{{ metric|metric_name }}": params.{{ metric|metric_argument_name }}.Format("2006-01-02T15:04:05.000Z"),
{% else %}
"{{ metric|metric_name }}": params.{{ metric|metric_argument_name }},
{% endif %}
{% endfor %}
},
{% endif %}
{% endif %}
{% endfor %}
}
var extraKeys = map[string]string{
Expand Down Expand Up @@ -221,9 +269,9 @@ func (g GleanEventsLogger) Record{{ event|event_type_name }}(
// Record and submit an {{ event|event_type_name }} event omitting user request info
// {{ event.description|clean_string }}
func (g GleanEventsLogger) Record{{ event|event_type_name }}WithoutUserInfo(
params {{ event|event_type_name }},
params {{ event|event_type_name }},
) {
g.Record{{ event|event_type_name }}(defaultRequestInfo, params)
}

{% endfor %}
{% endif %}
67 changes: 67 additions & 0 deletions tests/data/go_server_custom_ping_only_metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Any copyright is dedicated to the Public Domain.
# https://creativecommons.org/publicdomain/zero/1.0/

---
$schema: moz://mozilla.org/schemas/glean/metrics/2-0-0

metric:
name:
type: string
description: |
Test string metric
lifetime: application
send_in_pings:
- server-telemetry-scenario-one
notification_emails:
- [email protected]
bugs:
- TBD
data_reviews:
- TBD
expires: never

request_count:
type: quantity
unit: ad
description: |
Test quantity metric
lifetime: application
send_in_pings:
- server-telemetry-scenario-one
notification_emails:
- [email protected]
bugs:
- TBD
data_reviews:
- TBD
expires: never

request_datetime:
type: datetime
description: |
Test datetime metric
lifetime: application
send_in_pings:
- server-telemetry-scenario-one
notification_emails:
- [email protected]
bugs:
- TBD
data_reviews:
- TBD
expires: never

request_bool:
type: boolean
description: >
boolean
lifetime: application
send_in_pings:
- server-telemetry-scenario-one
notification_emails:
- [email protected]
bugs:
- TBD
data_reviews:
- TBD
expires: never
17 changes: 17 additions & 0 deletions tests/data/go_server_custom_ping_only_pings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Any copyright is dedicated to the Public Domain.
# https://creativecommons.org/publicdomain/zero/1.0/

---
$schema: moz://mozilla.org/schemas/glean/pings/2-0-0

server-telemetry-scenario-one:
description: |
Backend custom ping description
include_client_id: false
send_if_empty: false
bugs:
- TBD
data_reviews:
- TBD
notification_emails:
- [email protected]
97 changes: 97 additions & 0 deletions tests/data/go_server_events_and_custom_ping_metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Any copyright is dedicated to the Public Domain.
# https://creativecommons.org/publicdomain/zero/1.0/

---
$schema: moz://mozilla.org/schemas/glean/metrics/2-0-0

metric:
name:
type: string
description: |
Test string metric
lifetime: application
send_in_pings:
- events
- server-telemetry-scenario-one
notification_emails:
- [email protected]
bugs:
- TBD
data_reviews:
- TBD
expires: never

request_count:
type: quantity
unit: ad
description: |
Test quantity metric
lifetime: application
send_in_pings:
- events
- server-telemetry-scenario-one
notification_emails:
- [email protected]
bugs:
- TBD
data_reviews:
- TBD
expires: never

request_datetime:
type: datetime
description: |
Test datetime metric
lifetime: application
send_in_pings:
- events
- server-telemetry-scenario-one
notification_emails:
- [email protected]
bugs:
- TBD
data_reviews:
- TBD
expires: never

request_bool:
type: boolean
description: >
boolean
lifetime: application
send_in_pings:
- events
- server-telemetry-scenario-one
notification_emails:
- [email protected]
bugs:
- TBD
data_reviews:
- TBD
expires: never

backend:
test_event:
type: event
description: >
test event
bugs:
- TBD
data_reviews:
- TBD
notification_emails:
- [email protected]
expires: never
extra_keys:
event_field_string:
description: >
A string extra field
type: string
event_field_quantity:
description: >
A quantity extra field
type: quantity
event_field_bool:
description: >
A boolean extra field
type: boolean
Loading