blob: 2f1aac76f309ed6cc41098bf7da61ce2e16439b1 [file] [log] [blame]
Jason Changf19b3102023-09-01 16:07:34 -07001# 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
Josip Sokcevice5fb6e52023-12-19 09:45:44 -080041# Timeout when sending events via socket (applies to connect, send)
42SOCK_TIMEOUT = 0.5 # in seconds
Jason Changf19b3102023-09-01 16:07:34 -070043# BaseEventLog __init__ Counter that is consistent within the same process
44p_init_count = 0
45
46
Mike Frysingerd4aee652023-10-19 05:13:32 -040047class BaseEventLog:
Jason Changf19b3102023-09-01 16:07:34 -070048 """Event log that records events that occurred during a repo invocation.
49
50 Events are written to the log as a consecutive JSON entries, one per line.
51 Entries follow the git trace2 EVENT format.
52
53 Each entry contains the following common keys:
54 - event: The event name
55 - sid: session-id - Unique string to allow process instance to be
56 identified.
57 - thread: The thread name.
58 - time: is the UTC time of the event.
59
60 Valid 'event' names and event specific fields are documented here:
61 https://git-scm.com/docs/api-trace2#_event_format
62 """
63
64 def __init__(
65 self, env=None, repo_source_version=None, add_init_count=False
66 ):
67 """Initializes the event log."""
68 global p_init_count
69 p_init_count += 1
70 self._log = []
71 # Try to get session-id (sid) from environment (setup in repo launcher).
72 KEY = "GIT_TRACE2_PARENT_SID"
73 if env is None:
74 env = os.environ
75
LuK1337aadd12c2023-09-16 09:36:49 +020076 self.start = datetime.datetime.now(datetime.timezone.utc)
Jason Changf19b3102023-09-01 16:07:34 -070077
78 # Save both our sid component and the complete sid.
79 # We use our sid component (self._sid) as the unique filename prefix and
80 # the full sid (self._full_sid) in the log itself.
Jason R. Coombsb32ccbb2023-09-29 11:04:49 -040081 self._sid = (
82 f"repo-{self.start.strftime('%Y%m%dT%H%M%SZ')}-P{os.getpid():08x}"
Jason Changf19b3102023-09-01 16:07:34 -070083 )
84
85 if add_init_count:
86 self._sid = f"{self._sid}-{p_init_count}"
87
88 parent_sid = env.get(KEY)
89 # Append our sid component to the parent sid (if it exists).
90 if parent_sid is not None:
91 self._full_sid = parent_sid + "/" + self._sid
92 else:
93 self._full_sid = self._sid
94
95 # Set/update the environment variable.
96 # Environment handling across systems is messy.
97 try:
98 env[KEY] = self._full_sid
99 except UnicodeEncodeError:
100 env[KEY] = self._full_sid.encode()
101
102 if repo_source_version is not None:
103 # Add a version event to front of the log.
104 self._AddVersionEvent(repo_source_version)
105
106 @property
107 def full_sid(self):
108 return self._full_sid
109
110 def _AddVersionEvent(self, repo_source_version):
111 """Adds a 'version' event at the beginning of current log."""
112 version_event = self._CreateEventDict("version")
113 version_event["evt"] = "2"
114 version_event["exe"] = repo_source_version
115 self._log.insert(0, version_event)
116
117 def _CreateEventDict(self, event_name):
118 """Returns a dictionary with common keys/values for git trace2 events.
119
120 Args:
121 event_name: The event name.
122
123 Returns:
124 Dictionary with the common event fields populated.
125 """
126 return {
127 "event": event_name,
128 "sid": self._full_sid,
129 "thread": threading.current_thread().name,
LuK1337aadd12c2023-09-16 09:36:49 +0200130 "time": datetime.datetime.now(datetime.timezone.utc).isoformat(),
Jason Changf19b3102023-09-01 16:07:34 -0700131 }
132
133 def StartEvent(self):
134 """Append a 'start' event to the current log."""
135 start_event = self._CreateEventDict("start")
136 start_event["argv"] = sys.argv
137 self._log.append(start_event)
138
139 def ExitEvent(self, result):
140 """Append an 'exit' event to the current log.
141
142 Args:
143 result: Exit code of the event
144 """
145 exit_event = self._CreateEventDict("exit")
146
147 # Consider 'None' success (consistent with event_log result handling).
148 if result is None:
149 result = 0
150 exit_event["code"] = result
LuK1337aadd12c2023-09-16 09:36:49 +0200151 time_delta = datetime.datetime.now(datetime.timezone.utc) - self.start
Jason Changf19b3102023-09-01 16:07:34 -0700152 exit_event["t_abs"] = time_delta.total_seconds()
153 self._log.append(exit_event)
154
155 def CommandEvent(self, name, subcommands):
156 """Append a 'command' event to the current log.
157
158 Args:
159 name: Name of the primary command (ex: repo, git)
160 subcommands: List of the sub-commands (ex: version, init, sync)
161 """
162 command_event = self._CreateEventDict("command")
163 command_event["name"] = name
164 command_event["subcommands"] = subcommands
165 self._log.append(command_event)
166
167 def LogConfigEvents(self, config, event_dict_name):
168 """Append a |event_dict_name| event for each config key in |config|.
169
170 Args:
171 config: Configuration dictionary.
172 event_dict_name: Name of the event dictionary for items to be logged
173 under.
174 """
175 for param, value in config.items():
176 event = self._CreateEventDict(event_dict_name)
177 event["param"] = param
178 event["value"] = value
179 self._log.append(event)
180
181 def DefParamRepoEvents(self, config):
182 """Append 'def_param' events for repo config keys to the current log.
183
184 This appends one event for each repo.* config key.
185
186 Args:
187 config: Repo configuration dictionary
188 """
189 # Only output the repo.* config parameters.
190 repo_config = {k: v for k, v in config.items() if k.startswith("repo.")}
191 self.LogConfigEvents(repo_config, "def_param")
192
193 def GetDataEventName(self, value):
194 """Returns 'data-json' if the value is an array else returns 'data'."""
195 return "data-json" if value[0] == "[" and value[-1] == "]" else "data"
196
197 def LogDataConfigEvents(self, config, prefix):
198 """Append a 'data' event for each entry in |config| to the current log.
199
200 For each keyX and valueX of the config, "key" field of the event is
201 '|prefix|/keyX' and the "value" of the "key" field is valueX.
202
203 Args:
204 config: Configuration dictionary.
205 prefix: Prefix for each key that is logged.
206 """
207 for key, value in config.items():
208 event = self._CreateEventDict(self.GetDataEventName(value))
209 event["key"] = f"{prefix}/{key}"
210 event["value"] = value
211 self._log.append(event)
212
213 def ErrorEvent(self, msg, fmt=None):
214 """Append a 'error' event to the current log."""
215 error_event = self._CreateEventDict("error")
216 if fmt is None:
217 fmt = msg
218 error_event["msg"] = f"RepoErrorEvent:{msg}"
219 error_event["fmt"] = f"RepoErrorEvent:{fmt}"
220 self._log.append(error_event)
221
222 def _WriteLog(self, write_fn):
223 """Writes the log out using a provided writer function.
224
225 Generate compact JSON output for each item in the log, and write it
226 using write_fn.
227
228 Args:
229 write_fn: A function that accepts byts and writes them to a
230 destination.
231 """
232
233 for e in self._log:
234 # Dump in compact encoding mode.
235 # See 'Compact encoding' in Python docs:
236 # https://docs.python.org/3/library/json.html#module-json
237 write_fn(
238 json.dumps(e, indent=None, separators=(",", ":")).encode(
239 "utf-8"
240 )
241 + b"\n"
242 )
243
244 def Write(self, path=None):
245 """Writes the log out to a file or socket.
246
247 Log is only written if 'path' or 'git config --get trace2.eventtarget'
248 provide a valid path (or socket) to write logs to.
249
250 Logging filename format follows the git trace2 style of being a unique
251 (exclusive writable) file.
252
253 Args:
254 path: Path to where logs should be written. The path may have a
255 prefix of the form "af_unix:[{stream|dgram}:]", in which case
256 the path is treated as a Unix domain socket. See
257 https://git-scm.com/docs/api-trace2#_enabling_a_target for
258 details.
259
260 Returns:
261 log_path: Path to the log file or socket if log is written,
262 otherwise None
263 """
264 log_path = None
265 # If no logging path is specified, exit.
266 if path is None:
267 return None
268
269 path_is_socket = False
270 socket_type = None
271 if isinstance(path, str):
272 parts = path.split(":", 1)
273 if parts[0] == "af_unix" and len(parts) == 2:
274 path_is_socket = True
275 path = parts[1]
276 parts = path.split(":", 1)
277 if parts[0] == "stream" and len(parts) == 2:
278 socket_type = socket.SOCK_STREAM
279 path = parts[1]
280 elif parts[0] == "dgram" and len(parts) == 2:
281 socket_type = socket.SOCK_DGRAM
282 path = parts[1]
283 else:
284 # Get absolute path.
285 path = os.path.abspath(os.path.expanduser(path))
286 else:
287 raise TypeError("path: str required but got %s." % type(path))
288
289 # Git trace2 requires a directory to write log to.
290
291 # TODO(https://crbug.com/gerrit/13706): Support file (append) mode also.
292 if not (path_is_socket or os.path.isdir(path)):
293 return None
294
295 if path_is_socket:
296 if socket_type == socket.SOCK_STREAM or socket_type is None:
297 try:
298 with socket.socket(
299 socket.AF_UNIX, socket.SOCK_STREAM
300 ) as sock:
Josip Sokcevice5fb6e52023-12-19 09:45:44 -0800301 sock.settimeout(SOCK_TIMEOUT)
Jason Changf19b3102023-09-01 16:07:34 -0700302 sock.connect(path)
303 self._WriteLog(sock.sendall)
304 return f"af_unix:stream:{path}"
305 except OSError as err:
306 # If we tried to connect to a DGRAM socket using STREAM,
307 # ignore the attempt and continue to DGRAM below. Otherwise,
308 # issue a warning.
309 if err.errno != errno.EPROTOTYPE:
310 print(
311 f"repo: warning: git trace2 logging failed: {err}",
312 file=sys.stderr,
313 )
314 return None
315 if socket_type == socket.SOCK_DGRAM or socket_type is None:
316 try:
317 with socket.socket(
318 socket.AF_UNIX, socket.SOCK_DGRAM
319 ) as sock:
320 self._WriteLog(lambda bs: sock.sendto(bs, path))
321 return f"af_unix:dgram:{path}"
322 except OSError as err:
323 print(
324 f"repo: warning: git trace2 logging failed: {err}",
325 file=sys.stderr,
326 )
327 return None
328 # Tried to open a socket but couldn't connect (SOCK_STREAM) or write
329 # (SOCK_DGRAM).
330 print(
331 "repo: warning: git trace2 logging failed: could not write to "
332 "socket",
333 file=sys.stderr,
334 )
335 return None
336
337 # Path is an absolute path
338 # Use NamedTemporaryFile to generate a unique filename as required by
339 # git trace2.
340 try:
341 with tempfile.NamedTemporaryFile(
342 mode="xb", prefix=self._sid, dir=path, delete=False
343 ) as f:
344 # TODO(https://crbug.com/gerrit/13706): Support writing events
345 # as they occur.
346 self._WriteLog(f.write)
347 log_path = f.name
348 except FileExistsError as err:
349 print(
350 "repo: warning: git trace2 logging failed: %r" % err,
351 file=sys.stderr,
352 )
353 return None
354 return log_path