Repository navigation
feat: added support for streaming stdout when --progress=plain #7069
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d6d54f3
feat: added support for streaming stdout when --progress=plain
morlay 19358a6
chore: fix some linting issues
jedevc 3c375eb
fix: ensure we set tty everywhere
jedevc e0c2519
streaming output should still collect db
jedevc 17963f9
apply suggested fix for empty span name
jedevc db5328f
import newoutput from progrock
jedevc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package idtui | ||
|
|
||
| import ( | ||
| "io" | ||
|
|
||
| "github.com/muesli/termenv" | ||
| ) | ||
|
|
||
| // NewOutput returns a termenv.Output that will always use color, regardless of | ||
| // whether w is a TTY, unless NO_COLOR is explicitly set. | ||
| // | ||
| // Progrock is opinionated here. Termenv disables colors by default if | ||
| // stdout is not a TTY or if the CI env var is set. We don't want that, | ||
| // because folks deserve colorful output in CI too. | ||
| // | ||
| // To disable colors, set NO_COLOR (https://no-color.org/). | ||
| func NewOutput(w io.Writer, opts ...termenv.OutputOption) *termenv.Output { | ||
| return termenv.NewOutput(w, append([]termenv.OutputOption{ | ||
| termenv.WithProfile(ColorProfile()), | ||
| termenv.WithTTY(true), | ||
| }, opts...)...) | ||
| } | ||
|
|
||
| // ColorProfile returns Ascii if, and only if, NO_COLOR or similar is set. | ||
| // Otherwise it returns termenv.ANSI, allowing colors to be used. | ||
| // | ||
| // Note that color profiles beyond simple ANSI are not used by Progrock. 16 | ||
| // colors is all you need. Anything else disrespects the user's color scheme | ||
| // preferences. | ||
| func ColorProfile() termenv.Profile { | ||
| if termenv.EnvNoColor() { | ||
| return termenv.Ascii | ||
| } else { | ||
| return termenv.ANSI | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| package idtui | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "os" | ||
| "sync" | ||
| "sync/atomic" | ||
| "time" | ||
|
|
||
| "github.com/dagger/dagger/telemetry/sdklog" | ||
| "github.com/muesli/termenv" | ||
| sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
| "go.opentelemetry.io/otel/trace" | ||
| ) | ||
|
|
||
| type streamingExporter struct { | ||
| spanNames sync.Map | ||
|
|
||
| // incr idx for spanID | ||
| idx int64 | ||
| batchPrinterGetters sync.Map | ||
| batchPrinterWg sync.WaitGroup | ||
|
|
||
| output *termenv.Output | ||
| outputMu sync.RWMutex | ||
|
|
||
| frameTicker *time.Ticker | ||
|
|
||
| done chan struct{} | ||
| doneOnce sync.Once | ||
| } | ||
|
|
||
| func newStreamingExporter() *streamingExporter { | ||
| return &streamingExporter{ | ||
| output: NewOutput(os.Stderr), | ||
| done: make(chan struct{}), | ||
| frameTicker: time.NewTicker(50 * time.Millisecond), | ||
| } | ||
| } | ||
|
|
||
| func (t *streamingExporter) Shutdown(ctx context.Context) error { | ||
| t.doneOnce.Do(func() { | ||
| t.frameTicker.Stop() | ||
| close(t.done) | ||
| }) | ||
|
|
||
| // wait all printers outputs | ||
| t.batchPrinterWg.Wait() | ||
| return nil | ||
| } | ||
|
|
||
| func (t *streamingExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error { | ||
| for _, span := range spans { | ||
| t.spanNames.Store(span.SpanContext().SpanID(), span.Name()) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (t *streamingExporter) ExportLogs(ctx context.Context, logs []*sdklog.LogData) error { | ||
| for _, log := range logs { | ||
| t.export(log) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (t *streamingExporter) getSpanName(spanID trace.SpanID) string { | ||
| if name, ok := t.spanNames.Load(spanID); ok { | ||
| return name.(string) | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func (t *streamingExporter) export(logData *sdklog.LogData) { | ||
| // FIXME may add GC when printer idle | ||
| get, _ := t.batchPrinterGetters.LoadOrStore(logData.SpanID, sync.OnceValue(func() logPrinter { | ||
| w := &batchPrinter{ | ||
| spanID: logData.SpanID, | ||
| num: atomic.AddInt64(&t.idx, 1), | ||
| getSpanName: t.getSpanName, | ||
| } | ||
|
|
||
| t.batchPrinterWg.Add(1) | ||
|
|
||
| go func() { | ||
| defer t.batchPrinterWg.Done() | ||
|
|
||
| <-t.done | ||
|
|
||
| t.tryRender(func(out *termenv.Output) { | ||
| w.render(out, true) | ||
| }) | ||
| }() | ||
|
|
||
| go func() { | ||
| for range t.frameTicker.C { | ||
| t.tryRender(func(out *termenv.Output) { | ||
| w.render(out, false) | ||
| }) | ||
| } | ||
| }() | ||
|
|
||
| return w | ||
| })) | ||
|
|
||
| get.(func() logPrinter)().PrintLog(logData) | ||
| } | ||
|
|
||
| func (t *streamingExporter) tryRender(print func(out *termenv.Output)) { | ||
| t.outputMu.Lock() | ||
| defer t.outputMu.Unlock() | ||
|
|
||
| print(t.output) | ||
| } | ||
|
|
||
| type batchPrinter struct { | ||
| num int64 | ||
| spanID trace.SpanID | ||
| getSpanName func(spanID trace.SpanID) string | ||
|
|
||
| latestPrintAt time.Time | ||
|
|
||
| lines []string | ||
| mu sync.RWMutex | ||
| } | ||
|
|
||
| func (w *batchPrinter) PrintLog(data *sdklog.LogData) { | ||
| msg := data.Body().AsString() | ||
|
|
||
| if msg == "" { | ||
| return | ||
| } | ||
|
|
||
| s := bufio.NewScanner(bytes.NewBufferString(msg)) | ||
| for s.Scan() { | ||
| if line := s.Text(); len(line) > 0 { | ||
| w.collect(line) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (w *batchPrinter) collect(line string) { | ||
| w.mu.Lock() | ||
| defer w.mu.Unlock() | ||
|
|
||
| w.lines = append(w.lines, line) | ||
| } | ||
|
|
||
| func (w *batchPrinter) render(out *termenv.Output, final bool) { | ||
| w.mu.Lock() | ||
| defer w.mu.Unlock() | ||
|
|
||
|
Contributor
Author
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. it could ignore logs without spanName for now. |
||
| spanName := w.getSpanName(w.spanID) | ||
| if spanName == "" { | ||
| return | ||
| } | ||
|
|
||
| lineCount := len(w.lines) | ||
|
|
||
| if lineCount == 0 { | ||
| return | ||
| } | ||
|
|
||
| if final || lineCount > 10 || time.Since(w.latestPrintAt) > time.Second { | ||
| _, _ = fmt.Fprint(out, out.String(fmt.Sprintf("%d: ", w.num)).Foreground(termenv.ANSIBrightMagenta)) | ||
| _, _ = fmt.Fprint(out, out.String("in ").Foreground(termenv.ANSICyan)) | ||
| _, _ = fmt.Fprintf(out, "%s\n", spanName) | ||
|
|
||
| for _, line := range w.lines { | ||
| _, _ = fmt.Fprint(out, out.String(fmt.Sprintf("%d: ", w.num)).Foreground(termenv.ANSIBrightMagenta)) | ||
| _, _ = fmt.Fprintln(out, line) | ||
| } | ||
|
|
||
| // one more line to break | ||
| _, _ = fmt.Fprintln(out) | ||
|
|
||
| w.lines = nil | ||
| w.latestPrintAt = time.Now() | ||
| } | ||
| } | ||
|
|
||
| type logPrinter interface { | ||
| PrintLog(data *sdklog.LogData) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
the span name is not always get cause otel emit the spans until span.End() called.
may consider set span name in log attrs too.