Skip to content
New issue

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

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

Already on GitHub? # to your account

[go server] replace panics with returned errors #777

Merged
merged 9 commits into from
Nov 22, 2024

Conversation

dmueller
Copy link
Contributor

@dmueller dmueller commented Nov 20, 2024

problem

It is idiomatic in go to return errors rather than panic. Panics can cause applications to exit, which is not the desired result if a glean log can't be emitted for any reason.

solution

Switch the exported functions to return an error. This enables applications that use the glean server implementation to inspect that error return value and take whatever action they deem appropriate.

A couple other things changed in the go server template

  • made tabs consistently used in the file
  • switched variable declarations to use := syntax

testing

make test-full
also added tests for Writer = nil and Writer = io.Discard to verify logs are not printed in those scenarios

Pull Request checklist

  • Quality: This PR builds and tests run cleanly
    • make test runs without emitting any warnings
    • make lint runs without emitting any errors
  • Tests: This PR includes thorough tests or an explanation of why it does not
  • Changelog: This PR includes a changelog entry to CHANGELOG.md or an explanation of why it does not need one
    • Any breaking changes to language binding APIs are noted explicitly

{% for extra, metadata in event.extra_keys.items() %}
{{ extra|event_extra_name }} {{ metadata.type|go_metric_type }} // {{ metadata.description|clean_string }}
{% endfor %}
{% for extra, metadata in event.extra_keys.items() %}
Copy link
Contributor Author

@dmueller dmueller Nov 20, 2024

Choose a reason for hiding this comment

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

This template was mixed tabs & spaces, so all the whitespace change is to get everything on tabs

Copy link

Choose a reason for hiding this comment

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

Comment on lines 253 to 286
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' %}
requestInfo RequestInfo,
params {{ ping|ping_type_name }},
) error {
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 %}
}

events := []gleanEvent{}
{% if metrics_by_type['event'] %}
if params.Event != nil {
{% else %}
"{{ metric|metric_name }}": params.{{ metric|metric_argument_name }},
{% endif %}
{% endfor %}
},
{% endif %}
{% endfor %}
}

events := []gleanEvent{}
{% if metrics_by_type['event'] %}
if params.Event != nil {
events = append(events, params.Event.gleanEvent())
}
{% endif %}
g.record("{{ ping }}", requestInfo, metrics, events)
{% endif %}
return g.record("{{ ping }}", requestInfo, metrics, events)
}

// Record and submit `{{ ping }}` ping omitting user request info
func (g GleanEventsLogger) Record{{ ping|ping_type_name}}WithoutUserInfo(
params {{ ping|ping_type_name}},
) {
g.Record{{ ping|ping_type_name }}(defaultRequestInfo, params)
params {{ ping|ping_type_name}},
) error {
return g.Record{{ ping|ping_type_name }}(defaultRequestInfo, params)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These two generated methods got their signature changed to return error, and the corresponding return statement

@@ -4,6 +4,7 @@ import (
"glean/glean"
"os"
"time"
/* IMPORTS */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added this to help facilitate the tests that used io.Discard / not setting a Writer on the glean logger. Those confirm that no output is produced with io.Discard and that an error is returned if the Writer is nil

@dmueller dmueller changed the title dmueller/golang remove panics [go server] replace panics with returned errors Nov 20, 2024
@dmueller dmueller marked this pull request as ready for review November 20, 2024 22:53
@dmueller dmueller requested review from akkomar and a team as code owners November 20, 2024 22:53
@dmueller dmueller requested review from chutten and removed request for a team November 20, 2024 22:53
{% for extra, metadata in event.extra_keys.items() %}
{{ extra|event_extra_name }} {{ metadata.type|go_metric_type }} // {{ metadata.description|clean_string }}
{% endfor %}
{% for extra, metadata in event.extra_keys.items() %}
Copy link

Choose a reason for hiding this comment

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


print(tmpl_code)
Copy link

Choose a reason for hiding this comment

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

Stray print?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TY, that was from getting the new tests working

Copy link

@efixler efixler left a comment

Choose a reason for hiding this comment

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

nice

Copy link
Contributor

@chutten chutten left a comment

Choose a reason for hiding this comment

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

I'm not a Go expert, but what I understand of these changes looks good to me. (The stray print should probably go, though. But it's test-only, so I'm not fussed if it makes it in)

Copy link
Collaborator

@akkomar akkomar left a comment

Choose a reason for hiding this comment

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

+1 for error handling approach.

I hate to be starting this discussion (lol) but this repo generally uses spaces, not tabs, so these files will have them both mixed eventually. I know Go's recommendation is to use tabs, but IIUC gofmt should address that. Is this change useful on your side when working with generated file or is it mostly about consistency across files here?

@dmueller
Copy link
Contributor Author

dmueller commented Nov 21, 2024

+1 for error handling approach.

I hate to be starting this discussion (lol) but this repo generally uses spaces, not tabs, so these files will have them both mixed eventually. I know Go's recommendation is to use tabs, but IIUC gofmt should address that. Is this change useful on your side when working with generated file or is it mostly about consistency across files here?

Mostly for consistency within the file itself, go fmt will fix it up. Given the jinja template is for producing go code, it seems like that template being close to the output language's formatting is a good thing. Is there a reason to make the jinja templates all use spaces?

@akkomar
Copy link
Collaborator

akkomar commented Nov 22, 2024

Is there a reason to make the jinja templates all use spaces?

Per https://github.com/mozilla/glean_parser/blob/main/.editorconfig#L5-L6 space is default indentation character for these files.

@dmueller
Copy link
Contributor Author

I didn't know about the editorconfig code and am using vscode without a plugin for it, so that's good to know about!

@akkomar akkomar merged commit 785b47e into mozilla:main Nov 22, 2024
8 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants