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

fixed percentiles not being able to be ints #9447

Merged
merged 4 commits into from
Jul 19, 2021
Merged

Conversation

MyaLongmire
Copy link
Contributor

Required for all PRs:

  • Updated associated README.md.
  • Wrote appropriate unit tests.

resolves #9441

added a Number struct and an unmarshal that will allow for ints or floats

@telegraf-tiger telegraf-tiger bot added the feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin label Jun 29, 2021
@MyaLongmire MyaLongmire marked this pull request as ready for review June 29, 2021 17:23
@sspaink sspaink added regression something that used to work, but is now broken fix pr to fix corresponding bug and removed feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin labels Jun 29, 2021
Copy link
Contributor

@sspaink sspaink left a comment

Choose a reason for hiding this comment

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

Good work, overall it looks good but can you add another unit test that reads from a toml config with a int instead of a float to verify this change works? Something like this:

func TestConfig(t *testing.T) {
        // Load a sample toml config
	c := config.NewConfig()
	require.NoError(t, c.LoadConfig("./testdata/statsd_int.toml"))
        // Create a statsd input plugin struct and load it with expected values
	s := inputs.Inputs["statsd"]().(*Statsd)
	s.Percentiles = 90.0
	require.Equal(t, s, c.Inputs[0].Input)
}

@srebhan
Copy link
Member

srebhan commented Jun 30, 2021

Hey @MyaLongmire, thanks for looking into this. IMO the problem here is an issue in the TOML parser as 90 is a perfect float64 value and should be parsed as such by the parser. So TBH I rather prefer to fix this function instead of circumventing the problem by doing the parsing manually.
What do you think?

@MyaLongmire MyaLongmire requested a review from sspaink June 30, 2021 17:01
@MyaLongmire
Copy link
Contributor Author

Good work, overall it looks good but can you add another unit test that reads from a toml config with a int instead of a float to verify this change works? Something like this:

func TestConfig(t *testing.T) {
        // Load a sample toml config
	c := config.NewConfig()
	require.NoError(t, c.LoadConfig("./testdata/statsd_int.toml"))
        // Create a statsd input plugin struct and load it with expected values
	s := inputs.Inputs["statsd"]().(*Statsd)
	s.Percentiles = 90.0
	require.Equal(t, s, c.Inputs[0].Input)
}

I added a test that ensures ints can be added in the config, although I changed your idea so I wouldn't have to add a test data folder

@MyaLongmire
Copy link
Contributor Author

Hey @MyaLongmire, thanks for looking into this. IMO the problem here is an issue in the TOML parser as 90 is a perfect float64 value and should be parsed as such by the parser. So TBH I rather prefer to fix this function instead of circumventing the problem by doing the parsing manually.
What do you think?

How would we know that 90 is supposed to be a float while parsing it? I feel like putting it in config copies how it used to be in internal.

@srebhan
Copy link
Member

srebhan commented Jul 2, 2021

How would we know that 90 is supposed to be a float while parsing it?
The TOML parser has the struct it unmarshalls to. If you look at the function I linked, fv reflect.Value is the reference to the struct field the parser should unmarshal to. In the big switch fv.Kind() { statement below, the parser converts the known TOML Integer value to the known field type. It's only that it does not take "converting an int to a float" into account. We just would need to add

	case reflect.Float32, reflect.Float64:
		f := float64(i)
		if fv.OverflowFloat(f) {
			return &errorOutOfRange{fv.Kind(), f}
		}
		fv.SetFloat(f)

I feel like putting it in config copies how it used to be in internal.

Right, and you can do this as it restores the functionality. However, it is a workaround and the problem occurs for any float64 field exposed as a configuration option. So you would need to convert all of those fields to config.Number... :-S

@reimda
Copy link
Contributor

reimda commented Jul 6, 2021

@srebhan @MyaLongmire I don't think we want to make this change apply to all float64 fields in the config. TOML's design intentionally has a separate float and int type so making a toml int field initialize a telegraf float setting feels wrong. I think this change should be a very isolated workaround only for the statsd plugin. Statsd is special only for backward compatibility.

I'd prefer not to put Number in config/types.go because I don't want other plugins to do this in the future. What do you think about moving Number from config/ into plugins/inputs/statsd/ and putting in a comment saying it is for backward compatibility and asking people not to do the same thing elsewhere?

@srebhan
Copy link
Member

srebhan commented Jul 7, 2021

@reimda: Fine with me, I just wanted to avoid spreading of Number again. :-)

In general I think using a TOML int as float is a valid use-case. Maybe the parser could have a strict_typing setting, but using 90 as a float is perfectly valid. Using 90.0 as int is not. :-)

@srebhan srebhan self-assigned this Jul 14, 2021
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

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

Perfect. Thanks @MyaLongmire for fixing this one!

@srebhan srebhan added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix pr to fix corresponding bug ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. regression something that used to work, but is now broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Statsd percentile setting fails on integer values
4 participants