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
8 changes: 1 addition & 7 deletions glean_parser/templates/go_server.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (e {{ event|event_type_name }}) gleanEvent() gleanEvent {
// {{ metric.description|clean_string }}
type {{ metric|metric_argument_name }} struct {
{% for label in metric.ordered_labels %}
{{ label|event_extra_name }} bool // {{ label }}
{{ label|event_extra_name }} *bool `json:"{{ label }},omitempty"`
{% endfor %}
}
{% endfor %}
Expand Down Expand Up @@ -277,12 +277,6 @@ func (g GleanEventsLogger) Record{{ ping|ping_type_name }}(
{% for metric in metrics %}
{% if metric_type == 'datetime' %}
"{{ metric|metric_name }}": params.{{ metric|metric_argument_name }}.Format("2006-01-02T15:04:05.000Z"),
{% elif metric_type == 'labeled_boolean' %}
"{{ metric|metric_name }}": map[string]bool{
{% for label in metric.ordered_labels %}
"{{ label }}": params.{{ metric|metric_argument_name }}.{{ label|event_extra_name }},
{% endfor %}
},
Comment on lines -281 to -285
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This map construction was doing the functional equivalent of the json annotations

{% else %}
"{{ metric|metric_name }}": params.{{ metric|metric_argument_name }},
{% endif %}
Expand Down
1 change: 1 addition & 0 deletions tests/data/go_server_labeled_boolean_metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ telemetry:
- feature_one
- feature_two
- feature_three
- feature_four
bugs:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1234567
data_reviews:
Expand Down
26 changes: 12 additions & 14 deletions tests/test_go_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,13 @@ def test_parser_go_server_labeled_boolean(tmp_path):

# Check that the labeled_boolean struct type was generated
assert "type TelemetryFeatureFlags struct {" in content
assert "FeatureOne bool" in content
assert "FeatureTwo bool" in content
assert "FeatureThree bool" in content
assert "FeatureOne *bool" in content
assert "FeatureTwo *bool" in content
assert "FeatureThree *bool" in content

# Check that it's used in the ping struct
assert "TelemetryFeatureFlags TelemetryFeatureFlags" in content

# Check that serialization includes map creation
assert "map[string]bool{" in content
assert '"feature_one":' in content
assert '"feature_two":' in content
assert '"feature_three":' in content


def test_parser_go_server_events_only(tmp_path):
"""Test that parser works for definitions that only use events ping"""
Expand Down Expand Up @@ -399,16 +393,19 @@ def test_run_logging_labeled_boolean(tmp_path):

code = """
_ = time.Now() // satisfy Go's unused import check for "time"
t := true
f := false
logger.RecordEventsPing(
glean.RequestInfo{
UserAgent: "glean-test/1.0",
IpAddress: "127.0.0.1",
},
glean.EventsPing{
TelemetryFeatureFlags: glean.TelemetryFeatureFlags{
FeatureOne: true,
FeatureTwo: false,
FeatureThree: true,
FeatureOne: &t,
FeatureTwo: &f,
FeatureThree: nil, // valid label on metric but this request had no value for it
// FeatureFour omitted to illustrate zero value behavior
},
},
)
Expand Down Expand Up @@ -437,13 +434,14 @@ def test_run_logging_labeled_boolean(tmp_path):
output.getvalue()
)

# Check that labeled_boolean is properly serialized as a map
# Check that labeled_boolean is properly serialized
labeled_boolean_metrics = payload["metrics"]["labeled_boolean"]
assert "telemetry.feature_flags" in labeled_boolean_metrics
feature_flags = labeled_boolean_metrics["telemetry.feature_flags"]
assert feature_flags["feature_one"] is True
assert feature_flags["feature_two"] is False
assert feature_flags["feature_three"] is True
assert feature_flags.get("feature_three") is None
assert feature_flags.get("feature_four") is None


@pytest.mark.go_dependency
Expand Down