7 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
14 // Timestamper is the signature for a function that takes a timestamp and
15 // return a formated string value.
16 type Timestamper func(t time.Time) string
20 // ThrottledLogger.Logger -> ThrottledLogger.Write ->
21 // ThrottledLogger.buf -> ThrottledLogger.flusher -> goWriter ->
22 // ArvLogWriter.Write -> CollectionFileWriter.Write | Api.Create
24 // For stdout/stderr ReadWriteLines additionally runs as a goroutine to pull
25 // data from the stdout/stderr Reader and send to the Logger.
27 // ThrottledLogger accepts writes, prepends a timestamp to each line of the
28 // write, and periodically flushes to a downstream writer. It supports the
29 // "Logger" and "WriteCloser" interfaces.
30 type ThrottledLogger struct {
40 // RFC3339Fixed is a fixed-width version of RFC3339 with microsecond precision,
41 // because the RFC3339Nano format isn't fixed width.
42 const RFC3339Fixed = "2006-01-02T15:04:05.000000Z07:00"
44 // RFC3339Timestamp return a RFC3339 formatted timestamp using RFC3339Fixed
45 func RFC3339Timestamp(now time.Time) string {
46 return now.Format(RFC3339Fixed)
49 // Write to the internal buffer. Prepend a timestamp to each line of the input
51 func (tl *ThrottledLogger) Write(p []byte) (n int, err error) {
54 tl.buf = &bytes.Buffer{}
56 defer tl.Mutex.Unlock()
58 now := tl.Timestamper(time.Now().UTC())
59 sc := bufio.NewScanner(bytes.NewBuffer(p))
61 _, err = fmt.Fprintf(tl.buf, "%s %s\n", now, sc.Text())
66 // Periodically check the current buffer; if not empty, send it on the
67 // channel to the goWriter goroutine.
68 func (tl *ThrottledLogger) flusher() {
69 bufchan := make(chan *bytes.Buffer)
70 bufterm := make(chan bool)
72 // Use a separate goroutine for the actual write so that the writes are
73 // actually initiated closer every 1s instead of every
74 // 1s + (time to it takes to write).
75 go goWriter(tl.writer, bufchan, bufterm)
78 time.Sleep(1 * time.Second)
81 if tl.buf != nil && tl.buf.Len() > 0 {
95 tl.flusherDone <- true
98 // Receive buffers from a channel and send to the underlying Writer
99 func goWriter(writer io.Writer, c <-chan *bytes.Buffer, t chan<- bool) {
101 writer.Write(b.Bytes())
106 // Close the flusher goroutine and wait for it to complete, then close the
107 // underlying Writer.
108 func (tl *ThrottledLogger) Close() error {
111 return tl.writer.Close()
115 // MaxLogLine is the maximum length of stdout/stderr lines before they are split.
119 // ReadWriteLines reads lines from a reader and writes to a Writer, with long
121 func ReadWriteLines(in io.Reader, writer io.Writer, done chan<- bool) {
122 reader := bufio.NewReaderSize(in, MaxLogLine)
125 line, isPrefix, err := reader.ReadLine()
128 } else if err != nil {
129 writer.Write([]byte(fmt.Sprintln("error reading container log:", err)))
136 if prefix == "" && suffix == "" {
139 writer.Write([]byte(fmt.Sprint(prefix, string(line), suffix)))
142 // Set up prefix for following line
152 // NewThrottledLogger creates a new thottled logger that
153 // (a) prepends timestamps to each line
154 // (b) batches log messages and only calls the underlying Writer at most once
156 func NewThrottledLogger(writer io.WriteCloser) *ThrottledLogger {
157 alw := &ThrottledLogger{}
158 alw.flusherDone = make(chan bool)
160 alw.Logger = log.New(alw, "", 0)
161 alw.Timestamper = RFC3339Timestamp
166 // ArvLogWriter implements a writer that writes to each of a WriteCloser
167 // (typically CollectionFileWriter) and creates an API server log entry.
168 type ArvLogWriter struct {
169 ArvClient IArvadosClient
172 writeCloser io.WriteCloser
175 func (arvlog *ArvLogWriter) Write(p []byte) (n int, err error) {
176 // Write to the next writer in the chain (a file in Keep)
178 if arvlog.writeCloser != nil {
179 _, err1 = arvlog.writeCloser.Write(p)
183 lr := arvadosclient.Dict{"object_uuid": arvlog.UUID,
184 "event_type": arvlog.loggingStream,
185 "properties": map[string]string{"text": string(p)}}
186 err2 := arvlog.ArvClient.Create("logs", lr, nil)
188 if err1 != nil || err2 != nil {
189 return 0, fmt.Errorf("%s ; %s", err1, err2)
194 // Close the underlying writer
195 func (arvlog *ArvLogWriter) Close() (err error) {
196 if arvlog.writeCloser != nil {
197 err = arvlog.writeCloser.Close()
198 arvlog.writeCloser = nil