/
steaming_exporter.go
193 lines (152 loc) · 3.74 KB
/
steaming_exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package logger
import (
"bufio"
"bytes"
"context"
"fmt"
"os"
"sync"
"sync/atomic"
"time"
"github.com/dagger/dagger/telemetry/sdklog"
"github.com/muesli/termenv"
"github.com/vito/progrock/ui"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)
func newStreamingExporter() *streamingExporter {
output := termenv.NewOutput(
os.Stdout,
termenv.WithProfile(ui.ColorProfile()),
termenv.WithUnsafe(),
)
return &streamingExporter{
output: output,
done: make(chan struct{}),
frameTicker: time.NewTicker(50 * time.Millisecond),
}
}
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 (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
lineCount int
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()
lineCount := len(w.lines)
if lineCount == 0 {
return
}
spanName := w.getSpanName(w.spanID)
if spanName == "" {
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.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)
}