-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: More robust connections to telemetry backend #1289
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
|
|
||
| from .integ_base import IntegBase, TelemetryServer | ||
|
|
||
|
|
||
| class TestTelemetryContract(IntegBase): | ||
| """ | ||
| Validates the basic tenets/contract Telemetry module needs to adhere to | ||
| """ | ||
|
|
||
| def test_must_not_send_metrics_if_disabled_using_envvar(self): | ||
| """ | ||
| No metrics should be sent if "Enabled via Config file but Disabled via Envvar" | ||
| """ | ||
| # Enable it via configuration file | ||
| self.set_config(telemetry_enabled=True) | ||
|
|
||
| with TelemetryServer() as server: | ||
| # Start the CLI, but opt-out of Telemetry using env var | ||
| process = self.run_cmd(optout_envvar_value="0") | ||
| (_, stderrdata) = process.communicate() | ||
| retcode = process.poll() | ||
| self.assertEquals(retcode, 0, "Command should successfully complete") | ||
| all_requests = server.get_all_requests() | ||
| self.assertEquals(0, len(all_requests), "No metrics should be sent") | ||
|
|
||
| # Now run again without the Env Var Opt out | ||
| process = self.run_cmd() | ||
| (_, stderrdata) = process.communicate() | ||
| retcode = process.poll() | ||
| self.assertEquals(retcode, 0, "Command should successfully complete") | ||
| all_requests = server.get_all_requests() | ||
| self.assertEquals(1, len(all_requests), "Command run metric should be sent") | ||
|
|
||
| def test_must_send_metrics_if_enabled_via_envvar(self): | ||
| """ | ||
| Metrics should be sent if "Disabled via config file but Enabled via Envvar" | ||
| """ | ||
| # Disable it via configuration file | ||
| self.set_config(telemetry_enabled=False) | ||
|
|
||
| with TelemetryServer() as server: | ||
| # Run without any envvar.Should not publish metrics | ||
| process = self.run_cmd() | ||
| (_, stderrdata) = process.communicate() | ||
| retcode = process.poll() | ||
| self.assertEquals(retcode, 0, "Command should successfully complete") | ||
| all_requests = server.get_all_requests() | ||
| self.assertEquals(0, len(all_requests), "No metric should be sent") | ||
|
|
||
| # Opt-in via env var | ||
| process = self.run_cmd(optout_envvar_value="1") | ||
| (_, stderrdata) = process.communicate() | ||
| retcode = process.poll() | ||
| self.assertEquals(retcode, 0, "Command should successfully complete") | ||
| all_requests = server.get_all_requests() | ||
| self.assertEquals(1, len(all_requests), "Command run metric must be sent") | ||
|
|
||
| def test_must_not_crash_when_offline(self): | ||
| """ | ||
| Must not crash the process if internet is not available | ||
| """ | ||
| self.set_config(telemetry_enabled=True) | ||
|
|
||
| # DO NOT START Telemetry Server here. | ||
| # Try to run the command without it. | ||
|
|
||
| # Start the CLI | ||
| process = self.run_cmd() | ||
|
|
||
| (_, stderrdata) = process.communicate() | ||
|
|
||
| retcode = process.poll() | ||
| self.assertEquals(retcode, 0, "Command should successfully complete") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,7 +100,7 @@ def test_default_request_should_be_fire_and_forget(self, requests_mock): | |
| telemetry = Telemetry(url=self.url) | ||
|
|
||
| telemetry.emit("metric_name", {}) | ||
| requests_mock.post.assert_called_once_with(ANY, json=ANY, timeout=(2, 0.001)) # 1ms response timeout | ||
| requests_mock.post.assert_called_once_with(ANY, json=ANY, timeout=(2, 0.1)) # 100ms response timeout | ||
|
|
||
| @patch("samcli.lib.telemetry.telemetry.requests") | ||
| def test_request_must_wait_for_2_seconds_for_response(self, requests_mock): | ||
|
|
@@ -121,15 +121,27 @@ def test_must_swallow_timeout_exception(self, requests_mock): | |
| # | ||
|
|
||
| requests_mock.exceptions.Timeout = requests.exceptions.Timeout | ||
| requests_mock.exceptions.ConnectionError = requests.exceptions.ConnectionError | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed here if the this test is only for Timeout exception?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah lbecause of how Python exception mocking works |
||
| requests_mock.post.side_effect = requests.exceptions.Timeout() | ||
|
|
||
| telemetry.emit("metric_name", {}) | ||
|
|
||
| @patch("samcli.lib.telemetry.telemetry.requests") | ||
| def test_must_swallow_connection_error_exception(self, requests_mock): | ||
| telemetry = Telemetry(url=self.url) | ||
|
|
||
| requests_mock.exceptions.Timeout = requests.exceptions.Timeout | ||
| requests_mock.exceptions.ConnectionError = requests.exceptions.ConnectionError | ||
| requests_mock.post.side_effect = requests.exceptions.ConnectionError() | ||
|
|
||
| telemetry.emit("metric_name", {}) | ||
|
|
||
| @patch("samcli.lib.telemetry.telemetry.requests") | ||
| def test_must_raise_on_other_requests_exception(self, requests_mock): | ||
| telemetry = Telemetry(url=self.url) | ||
|
|
||
| requests_mock.exceptions.Timeout = requests.exceptions.Timeout | ||
| requests_mock.exceptions.ConnectionError = requests.exceptions.ConnectionError | ||
| requests_mock.post.side_effect = IOError() | ||
|
|
||
| with self.assertRaises(IOError): | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I am not crazy about setting this config through code, as these are integ tests and should be set how customers would enable/disable. I am ok with it, just would prefer always going through customer paths in our integ tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how customers would enable telemetry. Thru the config.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a sight nuance to me though. This function gets called through command executions for the customers. The customer would never explicitly call this method. I know it’s what happens for them but since a customer never directly interacts with it, calling the method in an integ test looks off to me. Not blocking on this, just something I noticed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but we need some way of mimicing customer's environment. Calling this method is a way to do that