Rewrite TelemetryScalars' internal_LogScalarError to use nsPrintfCString

RESOLVED FIXED in Firefox 61

Status

()

P3
normal
RESOLVED FIXED
a year ago
11 months ago

People

(Reporter: chutten, Assigned: mcalabrese85, Mentored)

Tracking

(Blocks: 1 bug)

Trunk
mozilla61
Points:
---

Firefox Tracking Flags

(firefox61 fixed)

Details

(Whiteboard: [lang=C++][good-first-bug])

Attachments

(1 attachment, 1 obsolete attachment)

(Reporter)

Description

a year ago
internal_LogScalarError does some boring string construction that might be easier to maintain with if we use the built-in nsPrintfCString
Make it mentored!
Flags: needinfo?(alessio.placitelli)
Priority: -- → P3
In [1] we convert error enums to string messages that are printed to the browser console. However, some constants are hardcoded in the text messages adding friction to future changes.

We need to change these locations [2] to use |nsPrintfCString| to print the relevant constants instead.

- |kMaximumStringValueLength| for the maximum string length;
- |kMaximumKeyStringLength| for the key length;
- |kMaximumNumberOfKeys| for the maximum number of keys;

Please note that |nsPrintfCString| returns a narrow string, while |errorMessage| is a wide string. We might need to use |AppendUTF8toUTF16| to concatenate the output of nsPrintfCstring.

To test that this works, we should:

> ./mach test toolkit/components/telemetry/test/unit

And then also manually validate the output by:

- ./mach run, to open Firefox
- go to about:telemetry page
- open the web developer tools
- open the browser console
- issue the |Services.telemetry.scalarSet("telemetry.test.string_kind", "aaaaaaaaaaaaaaaaaaaaaaaaaaasssdasdasdasdadasdasdaasdasd")| and check that the error message is correctly printed to the browser console

A similar process can be used to test the other cases.

[1] - https://searchfox.org/mozilla-central/rev/f860c2bf00576959b07e2d9619c7b5088b0005be/toolkit/components/telemetry/TelemetryScalar.cpp#888-941
[2] - https://searchfox.org/mozilla-central/rev/f860c2bf00576959b07e2d9619c7b5088b0005be/toolkit/components/telemetry/TelemetryScalar.cpp#919,925,928
Mentor: alessio.placitelli, chutten
Flags: needinfo?(alessio.placitelli)
Whiteboard: [lang=C++][good-first-bug]
(Assignee)

Comment 3

11 months ago
Hello Alessio,

I'm looking at making this my first open source commit, but I'm having some trouble with the unit tests. 34 out of 35 pass, with the exception being test_TelemetryEnvironment.js. I receive a lot of information related to the test, but I'm not quite sure what to post to receive help.

The tests started with a warning that the MOZ_NODE_PATH environment variable is not set, so tests requiring http/2 will fail. I don't know if that's the cause, but I also don't know how to fix it to find out.

Services.telemetry.scalarSet("telemetry.test.string_kind", "aaaaaaaaaaaaaaaaaaaaaaaaaaasssdasdasdasdadasdasdaasdasd") comes back as undefined on the developer console, which means I didn't edit the code right I assume. What message should I expect to receive from that test so I may know if I did it right on my next attempt?
(In reply to mcalabrese85 from comment #3)
> Hello Alessio,
> 
> I'm looking at making this my first open source commit, but I'm having some
> trouble with the unit tests. 34 out of 35 pass, with the exception being
> test_TelemetryEnvironment.js. I receive a lot of information related to the
> test, but I'm not quite sure what to post to receive help.

Thank you for your contribution... and welcome! That's the right place to ask for help. If you want some real-time help, you're also encouraged to join the #introduction IRC channel on irc.mozilla.org!

> The tests started with a warning that the MOZ_NODE_PATH environment variable
> is not set, so tests requiring http/2 will fail. I don't know if that's the
> cause, but I also don't know how to fix it to find out.

That looks unrelated. You should look for "unexpected" or "exceptions" keywords. Usually, if you're running the test on Linux, the error line will be highlighted in red.

That's probably close to the end of the test log. I would start looking there for the offending log line. When you find it, feel free to attach the relevant log section to the bug. If you have troubles finding the problem, please attach both your patch and the full test log.

> Services.telemetry.scalarSet("telemetry.test.string_kind",
> "aaaaaaaaaaaaaaaaaaaaaaaaaaasssdasdasdasdadasdasdaasdasd") comes back as
> undefined on the developer console, which means I didn't edit the code right
> I assume. What message should I expect to receive from that test so I may
> know if I did it right on my next attempt?

You're expected to see "Truncating scalar value to 50 characters" :)

Please let me know if you need further help!
Assignee: nobody → mcalabrese85
(Assignee)

Comment 5

11 months ago
> Thank you for your contribution... and welcome! That's the right place to
> ask for help. If you want some real-time help, you're also encouraged to
> join the #introduction IRC channel on irc.mozilla.org!

Thank you for pointing me towards them, they were able to help with the issue. All automated tests now pass. It turned out that my DNS provider was returning a valid address when the test was expecting it to come back as invalid. I got to file my first (hand-held and hopefully good) bug report because of that. :)

> You're expected to see "Truncating scalar value to 50 characters" :)

I expected that to be the case. Unfortunately I was also unable to produce the expected results with a downloaded version of Firefox, nor with a clean build and downloaded version on a separate computer. I still receive "undefined" as a response.

I'm basically copying the command you wrote out above. I tried to use the string from test_TelemetryScalars.js also ("browser.qaxfiuosnzmhlg.rpvxicawolhtvmbkpnludhedobxvkjwqyeyvmv"), but the same result occurred. I know that the command is recognized because it will the console will mention that I do not have the correct number of arguments. I tried to see if there was anything else that may need to be done before or after entering that command, but as best I can tell it should work as a stand-alone one.

Any help with this would be appreciated. I plan on playing around with it some more, but mach suddenly decided to stop working so I may spend the rest of the night fixing that.
(Assignee)

Comment 6

11 months ago
I also forgot to mention that I tried to test the 100 key limit using the test_keyed_max_keys function to guide me. The following lines returned undefined, both when sent at the same time and sent individually:

Services.telemetry.keyedScalarAdd(KEYED_UINT_SCALAR, LAST_KEY_NAME, 10);
Services.telemetry.keyedScalarSet(KEYED_UINT_SCALAR, LAST_KEY_NAME, 1);
Services.telemetry.keyedScalarSetMaximum(KEYED_UINT_SCALAR, LAST_KEY_NAME, 10);

I can see that keyNamesSet has 100 keys in it by using a foreach loop to log them to the console. I cannot tell if each scalar got added correctly from keyNamesSet, though, as I do not see a way to check what is there.
(In reply to mcalabrese85 from comment #5)
> > Thank you for your contribution... and welcome! That's the right place to
> > ask for help. If you want some real-time help, you're also encouraged to
> > join the #introduction IRC channel on irc.mozilla.org!
> 
> Thank you for pointing me towards them, they were able to help with the
> issue. All automated tests now pass. It turned out that my DNS provider was
> returning a valid address when the test was expecting it to come back as
> invalid. I got to file my first (hand-held and hopefully good) bug report
> because of that. :)

Nice! Do you have a bug number for that?

> > You're expected to see "Truncating scalar value to 50 characters" :)
> 
> I expected that to be the case. Unfortunately I was also unable to produce
> the expected results with a downloaded version of Firefox, nor with a clean
> build and downloaded version on a separate computer. I still receive
> "undefined" as a response.

I think I understand where the confusion comes from! You should look for the message in the browser console, you're probably looking at the console from the devtools, which is different :) See https://developer.mozilla.org/en-US/docs/Tools/Browser_Console#Opening_the_Browser_Console

I double checked and the message is printed correctly with the vanilla Firefox Release/Nightly/Beta.
(Assignee)

Comment 8

11 months ago
Awesome, thank you again for your help. I was indeed using the web console instead of the browser one. 

I can confirm that manually doing the tests results in the expected error messages. I did notice a discrepancy, though. In the original hard-coded message the error message is "The key length must be limited to 70 characters". However, kMaximumKeyStringLength is set to 72, which changes the value seen in the message.

The bug number for the one I filed is bug 1453832.

What would be the next step now that I have a solution?
(Assignee)

Comment 9

11 months ago
I think I did the commit correctly, and hopefully referenced you in it right, too. Please let me know either way.

Comment 11

11 months ago
mozreview-review
Comment on attachment 8968108 [details]
Bug 1450098 - Rewrote part of TelemetryScalars' internal_LogScalarError to use nsPrintfCString and defined variables instead of constants;

https://reviewboard.mozilla.org/r/236784/#review242520

Hi! Thanks for your patch, the commit is right. It's not an r+ yet as there are a few style problems. You're right about the discrepancy :) Things tend to get out of sync when using hardcoded stuff instead of constants ;) 72 is correct!

::: toolkit/components/telemetry/TelemetryScalar.cpp:931
(Diff revision 1)
>        break;
>      case ScalarResult::InvalidValue:
>        errorMessage.AppendLiteral(u" - Attempted to set the scalar to an incompatible value.");
>        break;
>      case ScalarResult::StringTooLong:
> -      errorMessage.AppendLiteral(u" - Truncating scalar value to 50 characters.");
> +			AppendUTF8toUTF16(nsPrintfCString(" - Truncating scalar value to %i characters.", kMaximumStringValueLength), errorMessage);

Please use spaces instead of tabs, as per our [Mozilla Code Style guide](https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Coding_Style#Naming_and_Formatting_code).

::: toolkit/components/telemetry/TelemetryScalar.cpp:937
(Diff revision 1)
>        break;
>      case ScalarResult::KeyIsEmpty:
>        errorMessage.AppendLiteral(u" - The key must not be empty.");
>        break;
>      case ScalarResult::KeyTooLong:
> -      errorMessage.AppendLiteral(u" - The key length must be limited to 72 characters.");
> +			AppendUTF8toUTF16(nsPrintfCString(" - The key length must be limited to %i characters.", kMaximumKeyStringLength), errorMessage);

Please use spaces.

::: toolkit/components/telemetry/TelemetryScalar.cpp:940
(Diff revision 1)
>        break;
>      case ScalarResult::KeyTooLong:
> -      errorMessage.AppendLiteral(u" - The key length must be limited to 72 characters.");
> +			AppendUTF8toUTF16(nsPrintfCString(" - The key length must be limited to %i characters.", kMaximumKeyStringLength), errorMessage);
>        break;
>      case ScalarResult::TooManyKeys:
> -      errorMessage.AppendLiteral(u" - Keyed scalars cannot have more than 100 keys.");
> +			AppendUTF8toUTF16(nsPrintfCString(" - Keyed scalars cannot have more than %i keys.", kMaximumNumberOfKeys), errorMessage);

Please use spaces
Attachment #8968108 - Flags: review?(alessio.placitelli) → review-
(Assignee)

Updated

11 months ago
Attachment #8968108 - Attachment is obsolete: true
(Assignee)

Comment 13

11 months ago
Whoops, sorry about that. Forgot to change the settings in my IDE. It should be good now and has been resubmitted.

Comment 14

11 months ago
mozreview-review
Comment on attachment 8968330 [details]
Bug 1450098 - Rewrote part of TelemetryScalars' internal_LogScalarError to use nsPrintfCString and defined variables instead of constants;

https://reviewboard.mozilla.org/r/236996/#review242930
Attachment #8968330 - Flags: review?(alessio.placitelli) → review+

Comment 15

11 months ago
Pushed by alessio.placitelli@gmail.com:
https://hg.mozilla.org/integration/autoland/rev/9b0b1c38b8ba
Rewrote part of TelemetryScalars' internal_LogScalarError to use nsPrintfCString and defined variables instead of constants; r=Dexter

Comment 16

11 months ago
bugherder
https://hg.mozilla.org/mozilla-central/rev/9b0b1c38b8ba
Status: NEW → RESOLVED
Last Resolved: 11 months ago
status-firefox61: affected → fixed
Resolution: --- → FIXED
Target Milestone: --- → mozilla61
(Assignee)

Comment 17

11 months ago
Hey Alessio,

Thank you for taking the time to mentor me throughout this process. It gave me a lot more confidence in trying to contribute to this and other projects.
(In reply to Michael Calabrese from comment #17)
> Hey Alessio,
> 
> Thank you for taking the time to mentor me throughout this process. It gave
> me a lot more confidence in trying to contribute to this and other projects.

You're welcome! Would you be interested in working on another bug? If so, do you have a language preference?
Flags: needinfo?(mcalabrese85)
(Assignee)

Comment 19

11 months ago
I would like to. I prefer C++, but can work with Java, too. If the other languages you use involve bugs that aren't too complex I can work on those, though I would need extra time to go through the language some first.
Flags: needinfo?(mcalabrese85)
You need to log in before you can comment on or make changes to this bug.