-
Notifications
You must be signed in to change notification settings - Fork 302
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
Auto-create tracing spans for log groups #2309
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,9 @@ type Shell struct { | |
|
||
// Amount of time to wait between sending the InterruptSignal and SIGKILL | ||
SignalGracePeriod time.Duration | ||
|
||
// Whether to auto create tracing spans for log groups | ||
TraceLogGroups bool | ||
} | ||
|
||
// New returns a new Shell | ||
|
@@ -532,6 +535,32 @@ func round(d time.Duration) time.Duration { | |
} | ||
} | ||
|
||
// spanMakerWriter is an io.Writer shim that captures logs and automatically creates trace spans for the log group. | ||
type spanMakerWriter struct { | ||
w io.Writer | ||
ctx context.Context | ||
span opentracing.Span | ||
} | ||
|
||
func (s *spanMakerWriter) Write(p []byte) (n int, err error) { | ||
if bytes.HasPrefix(p, []byte("~~~ ")) || bytes.HasPrefix(p, []byte("--- ")) || bytes.HasPrefix(p, []byte("+++ ")) { | ||
s.FinishIfActive() | ||
operationName, _, _ := strings.Cut(string(p[4:]), "\r\n") | ||
// We don't store the context bc we don't need to create child spans (yet). If we stored it, every log group would | ||
// look like a child of the previous log group, where they're all more like siblings under the same parent span, | ||
// since Buildkite itself has no concept of log group hierarchy. | ||
s.span, _ = opentracing.StartSpanFromContext(s.ctx, operationName) | ||
} | ||
return s.w.Write(p) | ||
} | ||
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. So the complete line is not guaranteed to be contained in a single I think what we need here is a |
||
|
||
func (s *spanMakerWriter) FinishIfActive() { | ||
if s.span != nil { | ||
s.span.Finish() | ||
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. One thing missing is being able to mark a span as an error. I think we'd need to pass in the return of p.WaitResult here to know the process' exit code. |
||
s.span = nil | ||
} | ||
} | ||
|
||
func (s *Shell) executeCommand( | ||
ctx context.Context, | ||
cmd *command, | ||
|
@@ -542,6 +571,14 @@ func (s *Shell) executeCommand( | |
tracedEnv := env.FromSlice(cmd.Env) | ||
s.injectTraceCtx(ctx, tracedEnv) | ||
cmd.Env = tracedEnv.ToSlice() | ||
writer := w | ||
writerCloser := func() {} | ||
if s.TraceLogGroups { | ||
logToSpanWriter := &spanMakerWriter{w: w, ctx: ctx, span: nil} | ||
writer = logToSpanWriter | ||
writerCloser = func() { logToSpanWriter.FinishIfActive() } | ||
} | ||
defer writerCloser() | ||
|
||
s.cmdLock.Lock() | ||
s.cmd = cmd | ||
|
@@ -561,11 +598,11 @@ func (s *Shell) executeCommand( | |
// Modify process config based on execution flags | ||
if flags.PTY { | ||
cfg.PTY = true | ||
cfg.Stdout = w | ||
cfg.Stdout = writer | ||
} else { | ||
// Show stdout if requested or via debug | ||
if flags.Stdout { | ||
cfg.Stdout = w | ||
cfg.Stdout = writer | ||
} else if s.Debug { | ||
stdOutStreamer := NewLoggerStreamer(s.Logger) | ||
defer stdOutStreamer.Close() | ||
|
@@ -574,7 +611,7 @@ func (s *Shell) executeCommand( | |
|
||
// Show stderr if requested or via debug | ||
if flags.Stderr { | ||
cfg.Stderr = w | ||
cfg.Stderr = writer | ||
} else if s.Debug { | ||
stdErrStreamer := NewLoggerStreamer(s.Logger) | ||
defer stdErrStreamer.Close() | ||
|
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 file is getting pretty big. Can you put
spanMakerWriter
and its methods in a new file?And I think its name should reflect that it creates spans from log groups.
spansFromLogGroupWriter
perhaps?