summaryrefslogtreecommitdiffstats
path: root/git_trace2_event_log_base.py
diff options
context:
space:
mode:
authorJason Chang <jasonnc@google.com>2023-09-01 16:07:34 -0700
committerLUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-09-06 18:22:33 +0000
commitf19b310f15e03e92075e7409c9d7f0956acc007d (patch)
tree554ef8c4dcf5aab828a9f74e3568e6dffee17eab /git_trace2_event_log_base.py
parent712e62b9b07f690abbb40e089a17f4ddec6ba952 (diff)
downloadgit-repo-f19b310f15e03e92075e7409c9d7f0956acc007d.tar.gz
Log ErrorEvent for failing GitCommands
Change-Id: I270af7401cff310349e736bef87e9b381cc4d016 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/385054 Reviewed-by: Gavin Mak <gavinmak@google.com> Commit-Queue: Jason Chang <jasonnc@google.com> Tested-by: Jason Chang <jasonnc@google.com>
Diffstat (limited to 'git_trace2_event_log_base.py')
-rw-r--r--git_trace2_event_log_base.py352
1 files changed, 352 insertions, 0 deletions
diff --git a/git_trace2_event_log_base.py b/git_trace2_event_log_base.py
new file mode 100644
index 00000000..a111668a
--- /dev/null
+++ b/git_trace2_event_log_base.py
@@ -0,0 +1,352 @@
1# Copyright (C) 2020 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Provide event logging in the git trace2 EVENT format.
16
17The git trace2 EVENT format is defined at:
18https://www.kernel.org/pub/software/scm/git/docs/technical/api-trace2.html#_event_format
19https://git-scm.com/docs/api-trace2#_the_event_format_target
20
21 Usage:
22
23 git_trace_log = EventLog()
24 git_trace_log.StartEvent()
25 ...
26 git_trace_log.ExitEvent()
27 git_trace_log.Write()
28"""
29
30
31import datetime
32import errno
33import json
34import os
35import socket
36import sys
37import tempfile
38import threading
39
40
41# BaseEventLog __init__ Counter that is consistent within the same process
42p_init_count = 0
43
44
45class BaseEventLog(object):
46 """Event log that records events that occurred during a repo invocation.
47
48 Events are written to the log as a consecutive JSON entries, one per line.
49 Entries follow the git trace2 EVENT format.
50
51 Each entry contains the following common keys:
52 - event: The event name
53 - sid: session-id - Unique string to allow process instance to be
54 identified.
55 - thread: The thread name.
56 - time: is the UTC time of the event.
57
58 Valid 'event' names and event specific fields are documented here:
59 https://git-scm.com/docs/api-trace2#_event_format
60 """
61
62 def __init__(
63 self, env=None, repo_source_version=None, add_init_count=False
64 ):
65 """Initializes the event log."""
66 global p_init_count
67 p_init_count += 1
68 self._log = []
69 # Try to get session-id (sid) from environment (setup in repo launcher).
70 KEY = "GIT_TRACE2_PARENT_SID"
71 if env is None:
72 env = os.environ
73
74 self.start = datetime.datetime.utcnow()
75
76 # Save both our sid component and the complete sid.
77 # We use our sid component (self._sid) as the unique filename prefix and
78 # the full sid (self._full_sid) in the log itself.
79 self._sid = "repo-%s-P%08x" % (
80 self.start.strftime("%Y%m%dT%H%M%SZ"),
81 os.getpid(),
82 )
83
84 if add_init_count:
85 self._sid = f"{self._sid}-{p_init_count}"
86
87 parent_sid = env.get(KEY)
88 # Append our sid component to the parent sid (if it exists).
89 if parent_sid is not None:
90 self._full_sid = parent_sid + "/" + self._sid
91 else:
92 self._full_sid = self._sid
93
94 # Set/update the environment variable.
95 # Environment handling across systems is messy.
96 try:
97 env[KEY] = self._full_sid
98 except UnicodeEncodeError:
99 env[KEY] = self._full_sid.encode()
100
101 if repo_source_version is not None:
102 # Add a version event to front of the log.
103 self._AddVersionEvent(repo_source_version)
104
105 @property
106 def full_sid(self):
107 return self._full_sid
108
109 def _AddVersionEvent(self, repo_source_version):
110 """Adds a 'version' event at the beginning of current log."""
111 version_event = self._CreateEventDict("version")
112 version_event["evt"] = "2"
113 version_event["exe"] = repo_source_version
114 self._log.insert(0, version_event)
115
116 def _CreateEventDict(self, event_name):
117 """Returns a dictionary with common keys/values for git trace2 events.
118
119 Args:
120 event_name: The event name.
121
122 Returns:
123 Dictionary with the common event fields populated.
124 """
125 return {
126 "event": event_name,
127 "sid": self._full_sid,
128 "thread": threading.current_thread().name,
129 "time": datetime.datetime.utcnow().isoformat() + "Z",
130 }
131
132 def StartEvent(self):
133 """Append a 'start' event to the current log."""
134 start_event = self._CreateEventDict("start")
135 start_event["argv"] = sys.argv
136 self._log.append(start_event)
137
138 def ExitEvent(self, result):
139 """Append an 'exit' event to the current log.
140
141 Args:
142 result: Exit code of the event
143 """
144 exit_event = self._CreateEventDict("exit")
145
146 # Consider 'None' success (consistent with event_log result handling).
147 if result is None:
148 result = 0
149 exit_event["code"] = result
150 time_delta = datetime.datetime.utcnow() - self.start
151 exit_event["t_abs"] = time_delta.total_seconds()
152 self._log.append(exit_event)
153
154 def CommandEvent(self, name, subcommands):
155 """Append a 'command' event to the current log.
156
157 Args:
158 name: Name of the primary command (ex: repo, git)
159 subcommands: List of the sub-commands (ex: version, init, sync)
160 """
161 command_event = self._CreateEventDict("command")
162 command_event["name"] = name
163 command_event["subcommands"] = subcommands
164 self._log.append(command_event)
165
166 def LogConfigEvents(self, config, event_dict_name):
167 """Append a |event_dict_name| event for each config key in |config|.
168
169 Args:
170 config: Configuration dictionary.
171 event_dict_name: Name of the event dictionary for items to be logged
172 under.
173 """
174 for param, value in config.items():
175 event = self._CreateEventDict(event_dict_name)
176 event["param"] = param
177 event["value"] = value
178 self._log.append(event)
179
180 def DefParamRepoEvents(self, config):
181 """Append 'def_param' events for repo config keys to the current log.
182
183 This appends one event for each repo.* config key.
184
185 Args:
186 config: Repo configuration dictionary
187 """
188 # Only output the repo.* config parameters.
189 repo_config = {k: v for k, v in config.items() if k.startswith("repo.")}
190 self.LogConfigEvents(repo_config, "def_param")
191
192 def GetDataEventName(self, value):
193 """Returns 'data-json' if the value is an array else returns 'data'."""
194 return "data-json" if value[0] == "[" and value[-1] == "]" else "data"
195
196 def LogDataConfigEvents(self, config, prefix):
197 """Append a 'data' event for each entry in |config| to the current log.
198
199 For each keyX and valueX of the config, "key" field of the event is
200 '|prefix|/keyX' and the "value" of the "key" field is valueX.
201
202 Args:
203 config: Configuration dictionary.
204 prefix: Prefix for each key that is logged.
205 """
206 for key, value in config.items():
207 event = self._CreateEventDict(self.GetDataEventName(value))
208 event["key"] = f"{prefix}/{key}"
209 event["value"] = value
210 self._log.append(event)
211
212 def ErrorEvent(self, msg, fmt=None):
213 """Append a 'error' event to the current log."""
214 error_event = self._CreateEventDict("error")
215 if fmt is None:
216 fmt = msg
217 error_event["msg"] = f"RepoErrorEvent:{msg}"
218 error_event["fmt"] = f"RepoErrorEvent:{fmt}"
219 self._log.append(error_event)
220
221 def _WriteLog(self, write_fn):
222 """Writes the log out using a provided writer function.
223
224 Generate compact JSON output for each item in the log, and write it
225 using write_fn.
226
227 Args:
228 write_fn: A function that accepts byts and writes them to a
229 destination.
230 """
231
232 for e in self._log:
233 # Dump in compact encoding mode.
234 # See 'Compact encoding' in Python docs:
235 # https://docs.python.org/3/library/json.html#module-json
236 write_fn(
237 json.dumps(e, indent=None, separators=(",", ":")).encode(
238 "utf-8"
239 )
240 + b"\n"
241 )
242
243 def Write(self, path=None):
244 """Writes the log out to a file or socket.
245
246 Log is only written if 'path' or 'git config --get trace2.eventtarget'
247 provide a valid path (or socket) to write logs to.
248
249 Logging filename format follows the git trace2 style of being a unique
250 (exclusive writable) file.
251
252 Args:
253 path: Path to where logs should be written. The path may have a
254 prefix of the form "af_unix:[{stream|dgram}:]", in which case
255 the path is treated as a Unix domain socket. See
256 https://git-scm.com/docs/api-trace2#_enabling_a_target for
257 details.
258
259 Returns:
260 log_path: Path to the log file or socket if log is written,
261 otherwise None
262 """
263 log_path = None
264 # If no logging path is specified, exit.
265 if path is None:
266 return None
267
268 path_is_socket = False
269 socket_type = None
270 if isinstance(path, str):
271 parts = path.split(":", 1)
272 if parts[0] == "af_unix" and len(parts) == 2:
273 path_is_socket = True
274 path = parts[1]
275 parts = path.split(":", 1)
276 if parts[0] == "stream" and len(parts) == 2:
277 socket_type = socket.SOCK_STREAM
278 path = parts[1]
279 elif parts[0] == "dgram" and len(parts) == 2:
280 socket_type = socket.SOCK_DGRAM
281 path = parts[1]
282 else:
283 # Get absolute path.
284 path = os.path.abspath(os.path.expanduser(path))
285 else:
286 raise TypeError("path: str required but got %s." % type(path))
287
288 # Git trace2 requires a directory to write log to.
289
290 # TODO(https://crbug.com/gerrit/13706): Support file (append) mode also.
291 if not (path_is_socket or os.path.isdir(path)):
292 return None
293
294 if path_is_socket:
295 if socket_type == socket.SOCK_STREAM or socket_type is None:
296 try:
297 with socket.socket(
298 socket.AF_UNIX, socket.SOCK_STREAM
299 ) as sock:
300 sock.connect(path)
301 self._WriteLog(sock.sendall)
302 return f"af_unix:stream:{path}"
303 except OSError as err:
304 # If we tried to connect to a DGRAM socket using STREAM,
305 # ignore the attempt and continue to DGRAM below. Otherwise,
306 # issue a warning.
307 if err.errno != errno.EPROTOTYPE:
308 print(
309 f"repo: warning: git trace2 logging failed: {err}",
310 file=sys.stderr,
311 )
312 return None
313 if socket_type == socket.SOCK_DGRAM or socket_type is None:
314 try:
315 with socket.socket(
316 socket.AF_UNIX, socket.SOCK_DGRAM
317 ) as sock:
318 self._WriteLog(lambda bs: sock.sendto(bs, path))
319 return f"af_unix:dgram:{path}"
320 except OSError as err:
321 print(
322 f"repo: warning: git trace2 logging failed: {err}",
323 file=sys.stderr,
324 )
325 return None
326 # Tried to open a socket but couldn't connect (SOCK_STREAM) or write
327 # (SOCK_DGRAM).
328 print(
329 "repo: warning: git trace2 logging failed: could not write to "
330 "socket",
331 file=sys.stderr,
332 )
333 return None
334
335 # Path is an absolute path
336 # Use NamedTemporaryFile to generate a unique filename as required by
337 # git trace2.
338 try:
339 with tempfile.NamedTemporaryFile(
340 mode="xb", prefix=self._sid, dir=path, delete=False
341 ) as f:
342 # TODO(https://crbug.com/gerrit/13706): Support writing events
343 # as they occur.
344 self._WriteLog(f.write)
345 log_path = f.name
346 except FileExistsError as err:
347 print(
348 "repo: warning: git trace2 logging failed: %r" % err,
349 file=sys.stderr,
350 )
351 return None
352 return log_path