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

plugins/parsers/json: Use same timestamp for all objects in arrays #7412

Merged
merged 2 commits into from
Apr 29, 2020
Merged

plugins/parsers/json: Use same timestamp for all objects in arrays #7412

merged 2 commits into from
Apr 29, 2020

Conversation

presslab-us
Copy link
Contributor

I am new to this, so please let me know if this is not appropriate.

My data is sent as a single JSON packet but the resulting measurements generated from the JSON array all had different timestamps. This made it difficult to work with the data. Because all objects in the JSON array arrive at the same time they should have the same timestamp. With the same timestamp the data has the correlation that it should.

Required for all PRs:

  • Signed CLA.
  • Associated README.md updated. (no changes needed)
  • Has appropriate unit tests.

All objects arrive at the same time so they should have the same timestamp
@ssoroka
Copy link
Contributor

ssoroka commented Apr 27, 2020

Looks great, thanks so much for the contribution!

Copy link
Contributor

@danielnelson danielnelson left a comment

Choose a reason for hiding this comment

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

Looks great, do you think you can add a unit test too?

@presslab-us
Copy link
Contributor Author

Looks great, do you think you can add a unit test too?

I did run make test and it passed that plugin:

ok  	github.com/influxdata/telegraf/plugins/parsers/json	(cached)

I figured the code coverage would be unchanged due to the nature of the change. Was there some specific test you would like to see?

@danielnelson
Copy link
Contributor

Just something to check and document the behavior: parse a document that creates two metrics and ensure that the timestamp is the same between them. The document could be something very simple like:

[{"value": 1}, {"value":2}]

@presslab-us
Copy link
Contributor Author

Okay, I see what you are saying. It looks like the unit test is in config.go? The structure is not clear to me. Is there some other test I can copy from to make this? I have not worked with Golang before this.

@danielnelson
Copy link
Contributor

You would want to add the test to plugins/parsers/json/parser_test.go. Some of the tests in this file are on the older side and aren't great examples of our current style, but something like this might be a good starting point.

func TestShareTimestamp(t *testing.T) {
	parser, err := New(
		&Config{
			MetricName: "json",
		},
	)
	require.NoError(t, err)

	actual, err := parser.Parse([]byte(`[{"value": 1}, {"value":2}]`))
	require.NoError(t, err)

	// check that all metrics have the same timestamp
}

@presslab-us
Copy link
Contributor Author

Thanks a lot for your help. I have added the test.

@danielnelson danielnelson added this to the 1.14.3 milestone Apr 29, 2020
@danielnelson danielnelson added the fix pr to fix corresponding bug label Apr 29, 2020
@danielnelson danielnelson merged commit e1b2ebe into influxdata:master Apr 29, 2020
danielnelson pushed a commit that referenced this pull request Apr 29, 2020
idohalevi pushed a commit to idohalevi/telegraf that referenced this pull request Sep 29, 2020
arstercz pushed a commit to arstercz/telegraf that referenced this pull request Mar 5, 2023
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants