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

Values overwritten when more than 1 page of data available #117

Closed
piechu opened this issue Jan 30, 2020 · 6 comments · Fixed by #125
Closed

Values overwritten when more than 1 page of data available #117

piechu opened this issue Jan 30, 2020 · 6 comments · Fixed by #125

Comments

@piechu
Copy link

piechu commented Jan 30, 2020

Hi,

I have been playing with new relic terraform plugin which is using newrelic-client-go. I noticed that when I was searching for an alert notification channel - depending on environment - sometimes it was found, sometimes not. When I tried to compare the difference between environments - the only one was number of already defined channels. I thought that problem might be with plugin getting just one page of data - but since it is using newrelic-client-go - it should receive all data regardless how many is defined in NewRelic.

Playing with the code - I've noticed that data returned from newrelic-client-go ListChannels method is corrupted - multiple entries were duplicated. I think it is caused by using reference for response (&response) when calling alerts.client.Get - as same object is being reuse - it does not reallocate memory, which cause entries in array overwritten. Call ends up with proper number of elements in alertChannels slice, but with duplicated entries.

I've checked that assigning fresh value for response works as expected. As I'm not a Go expert maybe there is a better solution.

func (alerts *Alerts) ListChannels() ([]*Channel, error) {
	response := alertChannelsResponse{}
	alertChannels := []*Channel{}
	nextURL := "/alerts_channels.json"

	for nextURL != "" {
		resp, err := alerts.client.Get(nextURL, nil, &response)

		if err != nil {
			return nil, err
		}

		alertChannels = append(alertChannels, response.Channels...)

		paging := alerts.pager.Parse(resp)
		nextURL = paging.Next

		response = alertChannelsResponse{}
	}

	return alertChannels, nil
}

Thanks.

@sanderblue
Copy link
Contributor

Hi @piechu, interesting find. Thanks for reporting and digging into this. We'll take a look and report back.

@sanderblue
Copy link
Contributor

@piechu I tried reproducing this issue multiple times with over 300 channels and was unable to reproduce it. Do you have any other information that might help me reproduce the problem? How many channels, etc?

@piechu
Copy link
Author

piechu commented Jan 30, 2020

@sanderblue I have around 430 notification channels and every call to New Relic return page with 200 entries. Do you check for number of entries or actual values in table? As number of entries should be correct.

@sanderblue
Copy link
Contributor

I checked for duplicates entries (duplicate IDs).

@sanderblue
Copy link
Contributor

So after doing some testing in a better controlled environment, I was able to reproduce the duplication issue. Thanks again for the investigation on your end. We'll let you know once we get a fix out for this.

@piechu
Copy link
Author

piechu commented Jan 31, 2020

@sanderblue great to hear that. I have a unit test covering this scenario - attaching patch here.
test.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants