Skip to content

Commit dc12607

Browse files
committed
trace2: prefetch value of GIT_TRACE2_DST_DEBUG at startup (#663)
Prefetch the value of GIT_TRACE2_DST_DEBUG during startup and before we try to open any Trace2 destination pathnames. Normally, Trace2 always silently fails if a destination target cannot be opened so that it doesn't affect the execution of a Git command. The command should run normally, but just not generate any trace data. This can make it difficult to debug a telemetry setup, since the user doesn't know why telemetry isn't being generated. If the environment variable GIT_TRACE2_DST_DEBUG is true, the Trace2 startup will print a warning message with the `errno` to make debugging easier. However, on Windows, looking up the env variable resets `errno` so the warning message always ends with `...tracing: No error` which is not very helpful. Prefetch the env variable at startup. This avoids the need to update each call-site to capture `errno` in the usual `saved-errno` variable.
2 parents 1b13a22 + a83676d commit dc12607

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Diff for: trace2.c

+10
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ void trace2_initialize_fl(const char *file, int line)
227227
if (!tr2_tgt_want_builtins())
228228
return;
229229
trace2_enabled = 1;
230+
231+
/*
232+
* getenv() on Windows stomps on `errno` and the code in
233+
* tr2_dst.c verifies that warnings are enabled before
234+
* formatting the warning message (and calling strerror()).
235+
* So prefetch the value from the environment before we need
236+
* it.
237+
*/
238+
tr2_dst_want_warning();
239+
230240
if (!git_env_bool("GIT_TRACE2_REDACT", 1))
231241
trace2_redact = 0;
232242

Diff for: trace2/tr2_dst.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
static int tr2env_max_files = 0;
2626

27-
static int tr2_dst_want_warning(void)
27+
int tr2_dst_want_warning(void)
2828
{
2929
static int tr2env_dst_debug = -1;
3030

Diff for: trace2/tr2_dst.h

+12
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,16 @@ int tr2_dst_trace_want(struct tr2_dst *dst);
3535
*/
3636
void tr2_dst_write_line(struct tr2_dst *dst, struct strbuf *buf_line);
3737

38+
/*
39+
* Return true if we want warning messages when trying to open a
40+
* destination.
41+
*
42+
* (Trace2 always silently fails if a target cannot be opened so that
43+
* we don't affect the execution of the Git command, but it is helpful
44+
* for debugging telemetry configuration if we log warning messages
45+
* when trying to open a target. This is controlled by another config
46+
* value.)
47+
*/
48+
int tr2_dst_want_warning(void);
49+
3850
#endif /* TR2_DST_H */

0 commit comments

Comments
 (0)