diff options
| author | Raman Tenneti <rtenneti@google.com> | 2021-09-13 17:40:07 -0700 |
|---|---|---|
| committer | Raman Tenneti <rtenneti@google.com> | 2021-09-14 21:36:12 +0000 |
| commit | 6448a4f2af53c241594b5d12eb0d03b18f889eda (patch) | |
| tree | 554753373be6dbd595a659907d2016ac203268cd | |
| parent | 1328c35a4d0b47c7e8c00fe351f0e587481e28c2 (diff) | |
| download | git-repo-6448a4f2af53c241594b5d12eb0d03b18f889eda.tar.gz | |
sync: Log repo sync state events as 'data' events.
git_trace2_event_log.py:
+ Added LogDataConfigEvents method to log 'data' events.
Sync's current_sync_state and previous_sync_state are logged
as 'data' events in the current log.
It logs are key/value in the |config| argument. Each key is
prefixed with |prefix| argument.
The following are sample events that are logged during repo sync.
{"event":"data",
"sid":"repo-20210914T181545Z-P000330c0/repo-20210914T181545Z-P000330c0",
"thread":"MainThread",
"time":"2021-09-14T18:16:19.935846Z",
"key":"previous_sync_state/repo.syncstate.main.synctime",
"value":"2021-09-14T17:27:11.573717Z"}
{"event":"data",
"sid":"repo-20210914T181545Z-P000330c0/repo-20210914T181545Z-P000330c0",
"thread":"MainThread",
"time":"2021-09-14T18:16:19.955546Z",
"key":"current_sync_state/repo.syncstate.main.synctime",
"value":"2021-09-14T18:16:19.935979Z"}
tests/test_git_trace2_event_log.py:
+ Added unit tests
sync.py:
+ Changed logging calls to LogDataConfigEvents.
Tested:
$ ./run_tests
Tested it by running the following command multiple times.
$ repo_dev sync -j 20
repo sync has finished successfully
Verified config data is looged in trace2 event logs.
Bug: [google internal] b/199758376
Change-Id: I75fd830e90c1811ec28510538c99a2632b104e85
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/317823
Reviewed-by: Josh Steadmon <steadmon@google.com>
Reviewed-by: Xin Li <delphij@google.com>
Tested-by: Raman Tenneti <rtenneti@google.com>
| -rw-r--r-- | git_trace2_event_log.py | 16 | ||||
| -rw-r--r-- | subcmds/sync.py | 8 | ||||
| -rw-r--r-- | tests/fixtures/test.gitconfig | 2 | ||||
| -rw-r--r-- | tests/test_git_trace2_event_log.py | 33 |
4 files changed, 54 insertions, 5 deletions
diff --git a/git_trace2_event_log.py b/git_trace2_event_log.py index 9c9e5a70..92c4cc6b 100644 --- a/git_trace2_event_log.py +++ b/git_trace2_event_log.py | |||
| @@ -167,6 +167,22 @@ class EventLog(object): | |||
| 167 | repo_config = {k: v for k, v in config.items() if k.startswith('repo.')} | 167 | repo_config = {k: v for k, v in config.items() if k.startswith('repo.')} |
| 168 | self.LogConfigEvents(repo_config, 'def_param') | 168 | self.LogConfigEvents(repo_config, 'def_param') |
| 169 | 169 | ||
| 170 | def LogDataConfigEvents(self, config, prefix): | ||
| 171 | """Append a 'data' event for each config key/value in |config| to the current log. | ||
| 172 | |||
| 173 | For each keyX and valueX of the config, "key" field of the event is '|prefix|/keyX' | ||
| 174 | and the "value" of the "key" field is valueX. | ||
| 175 | |||
| 176 | Args: | ||
| 177 | config: Configuration dictionary. | ||
| 178 | prefix: Prefix for each key that is logged. | ||
| 179 | """ | ||
| 180 | for key, value in config.items(): | ||
| 181 | event = self._CreateEventDict('data') | ||
| 182 | event['key'] = f'{prefix}/{key}' | ||
| 183 | event['value'] = value | ||
| 184 | self._log.append(event) | ||
| 185 | |||
| 170 | def ErrorEvent(self, msg, fmt): | 186 | def ErrorEvent(self, msg, fmt): |
| 171 | """Append a 'error' event to the current log.""" | 187 | """Append a 'error' event to the current log.""" |
| 172 | error_event = self._CreateEventDict('error') | 188 | error_event = self._CreateEventDict('error') |
diff --git a/subcmds/sync.py b/subcmds/sync.py index 2a0de0a9..c99b06ca 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py | |||
| @@ -1092,13 +1092,13 @@ later is required to fix a server side protocol bug. | |||
| 1092 | sys.exit(1) | 1092 | sys.exit(1) |
| 1093 | 1093 | ||
| 1094 | # Log the previous sync analysis state from the config. | 1094 | # Log the previous sync analysis state from the config. |
| 1095 | self.git_event_log.LogConfigEvents(mp.config.GetSyncAnalysisStateData(), | 1095 | self.git_event_log.LogDataConfigEvents(mp.config.GetSyncAnalysisStateData(), |
| 1096 | 'previous_sync_state') | 1096 | 'previous_sync_state') |
| 1097 | 1097 | ||
| 1098 | # Update and log with the new sync analysis state. | 1098 | # Update and log with the new sync analysis state. |
| 1099 | mp.config.UpdateSyncAnalysisState(opt, superproject_logging_data) | 1099 | mp.config.UpdateSyncAnalysisState(opt, superproject_logging_data) |
| 1100 | self.git_event_log.LogConfigEvents(mp.config.GetSyncAnalysisStateData(), | 1100 | self.git_event_log.LogDataConfigEvents(mp.config.GetSyncAnalysisStateData(), |
| 1101 | 'current_sync_state') | 1101 | 'current_sync_state') |
| 1102 | 1102 | ||
| 1103 | if not opt.quiet: | 1103 | if not opt.quiet: |
| 1104 | print('repo sync has finished successfully.') | 1104 | print('repo sync has finished successfully.') |
diff --git a/tests/fixtures/test.gitconfig b/tests/fixtures/test.gitconfig index 90afff04..b178cf60 100644 --- a/tests/fixtures/test.gitconfig +++ b/tests/fixtures/test.gitconfig | |||
| @@ -12,7 +12,7 @@ | |||
| 12 | intm = 10m | 12 | intm = 10m |
| 13 | intg = 10g | 13 | intg = 10g |
| 14 | [repo "syncstate.main"] | 14 | [repo "syncstate.main"] |
| 15 | synctime = 2021-08-13T18:37:43.928600Z | 15 | synctime = 2021-09-14T17:23:43.537338Z |
| 16 | version = 1 | 16 | version = 1 |
| 17 | [repo "syncstate.sys"] | 17 | [repo "syncstate.sys"] |
| 18 | argv = ['/usr/bin/pytest-3'] | 18 | argv = ['/usr/bin/pytest-3'] |
diff --git a/tests/test_git_trace2_event_log.py b/tests/test_git_trace2_event_log.py index bee5865b..a5a6cbba 100644 --- a/tests/test_git_trace2_event_log.py +++ b/tests/test_git_trace2_event_log.py | |||
| @@ -234,6 +234,39 @@ class EventLogTestCase(unittest.TestCase): | |||
| 234 | self.assertEqual(len(self._log_data), 1) | 234 | self.assertEqual(len(self._log_data), 1) |
| 235 | self.verifyCommonKeys(self._log_data[0], expected_event_name='version') | 235 | self.verifyCommonKeys(self._log_data[0], expected_event_name='version') |
| 236 | 236 | ||
| 237 | def test_data_event_config(self): | ||
| 238 | """Test 'data' event data outputs all config keys. | ||
| 239 | |||
| 240 | Expected event log: | ||
| 241 | <version event> | ||
| 242 | <data event> | ||
| 243 | <data event> | ||
| 244 | """ | ||
| 245 | config = { | ||
| 246 | 'git.foo': 'bar', | ||
| 247 | 'repo.partialclone': 'false', | ||
| 248 | 'repo.syncstate.superproject.hassuperprojecttag': 'true', | ||
| 249 | } | ||
| 250 | prefix_value = 'prefix' | ||
| 251 | self._event_log_module.LogDataConfigEvents(config, prefix_value) | ||
| 252 | |||
| 253 | with tempfile.TemporaryDirectory(prefix='event_log_tests') as tempdir: | ||
| 254 | log_path = self._event_log_module.Write(path=tempdir) | ||
| 255 | self._log_data = self.readLog(log_path) | ||
| 256 | |||
| 257 | self.assertEqual(len(self._log_data), 4) | ||
| 258 | data_events = self._log_data[1:] | ||
| 259 | self.verifyCommonKeys(self._log_data[0], expected_event_name='version') | ||
| 260 | |||
| 261 | for event in data_events: | ||
| 262 | self.verifyCommonKeys(event, expected_event_name='data') | ||
| 263 | # Check for 'data' event specific fields. | ||
| 264 | self.assertIn('key', event) | ||
| 265 | self.assertIn('value', event) | ||
| 266 | key = event['key'].removeprefix(f'{prefix_value}/') | ||
| 267 | value = event['value'] | ||
| 268 | self.assertTrue(key in config and value == config[key]) | ||
| 269 | |||
| 237 | def test_error_event(self): | 270 | def test_error_event(self): |
| 238 | """Test and validate 'error' event data is valid. | 271 | """Test and validate 'error' event data is valid. |
| 239 | 272 | ||
