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 CopyReaderToLog 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 // CopyReaderToLog reads from a Reader and prints to a Logger, with long line
121 func CopyReaderToLog(in io.Reader, logger *log.Logger, done chan<- bool) {
122 reader := bufio.NewReaderSize(in, MaxLogLine)
125 line, isPrefix, err := reader.ReadLine()
128 } else if err != nil {
129 logger.Print("error reading container log:", err)
135 logger.Print(prefix, string(line), suffix)
136 // Set up prefix for following line
146 // NewThrottledLogger creates a new thottled logger that
147 // (a) prepends timestamps to each line
148 // (b) batches log messages and only calls the underlying Writer at most once
150 func NewThrottledLogger(writer io.WriteCloser) *ThrottledLogger {
151 alw := &ThrottledLogger{}
152 alw.flusherDone = make(chan bool)
154 alw.Logger = log.New(alw, "", 0)
155 alw.Timestamper = RFC3339Timestamp
160 // ArvLogWriter implements a writer that writes to each of a WriteCloser
161 // (typically CollectionFileWriter) and creates an API server log entry.
162 type ArvLogWriter struct {
163 ArvClient IArvadosClient
166 writeCloser io.WriteCloser
169 func (arvlog *ArvLogWriter) Write(p []byte) (n int, err error) {
170 // Write to the next writer in the chain (a file in Keep)
172 if arvlog.writeCloser != nil {
173 _, err1 = arvlog.writeCloser.Write(p)
177 lr := arvadosclient.Dict{"object_uuid": arvlog.UUID,
178 "event_type": arvlog.loggingStream,
179 "properties": map[string]string{"text": string(p)}}
180 err2 := arvlog.ArvClient.Create("logs", lr, nil)
182 if err1 != nil || err2 != nil {
183 return 0, fmt.Errorf("%s ; %s", err1, err2)
188 // Close the underlying writer
189 func (arvlog *ArvLogWriter) Close() (err error) {
190 if arvlog.writeCloser != nil {
191 err = arvlog.writeCloser.Close()
192 arvlog.writeCloser = nil