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 ->
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 {
41 // RFC3339NanoFixed is a fixed-width version of time.RFC3339Nano.
42 const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
44 // RFC3339Timestamp formats t as RFC3339NanoFixed.
45 func RFC3339Timestamp(t time.Time) string {
46 return t.Format(RFC3339NanoFixed)
49 // Write prepends a timestamp to each line of the input data and
50 // appends to the internal buffer. Each line is also logged to
51 // tl.Immediate, if tl.Immediate is not nil.
52 func (tl *ThrottledLogger) Write(p []byte) (n int, err error) {
54 defer tl.Mutex.Unlock()
57 tl.buf = &bytes.Buffer{}
60 now := tl.Timestamper(time.Now().UTC())
61 sc := bufio.NewScanner(bytes.NewBuffer(p))
62 for err == nil && sc.Scan() {
63 out := fmt.Sprintf("%s %s\n", now, sc.Bytes())
64 if tl.Immediate != nil {
65 tl.Immediate.Print(out[:len(out)-1])
67 _, err = io.WriteString(tl.buf, out)
78 // Periodically check the current buffer; if not empty, send it on the
79 // channel to the goWriter goroutine.
80 func (tl *ThrottledLogger) flusher() {
81 ticker := time.NewTicker(time.Second)
84 // We use a separate "stopping" var here to ensure we flush
85 // tl.buf after tl.stop becomes true.
88 var ready *bytes.Buffer
91 ready, tl.buf = tl.buf, nil
94 if ready != nil && ready.Len() > 0 {
95 tl.writer.Write(ready.Bytes())
102 close(tl.flusherDone)
105 // Close the flusher goroutine and wait for it to complete, then close the
106 // underlying Writer.
107 func (tl *ThrottledLogger) Close() error {
110 return tl.writer.Close()
114 // MaxLogLine is the maximum length of stdout/stderr lines before they are split.
118 // ReadWriteLines reads lines from a reader and writes to a Writer, with long
120 func ReadWriteLines(in io.Reader, writer io.Writer, done chan<- bool) {
121 reader := bufio.NewReaderSize(in, MaxLogLine)
124 line, isPrefix, err := reader.ReadLine()
127 } else if err != nil {
128 writer.Write([]byte(fmt.Sprintln("error reading container log:", err)))
135 if prefix == "" && suffix == "" {
138 writer.Write([]byte(fmt.Sprint(prefix, string(line), suffix)))
141 // Set up prefix for following line
151 // NewThrottledLogger creates a new thottled logger that
152 // (a) prepends timestamps to each line
153 // (b) batches log messages and only calls the underlying Writer at most once
155 func NewThrottledLogger(writer io.WriteCloser) *ThrottledLogger {
156 tl := &ThrottledLogger{}
157 tl.flusherDone = make(chan bool)
159 tl.Logger = log.New(tl, "", 0)
160 tl.Timestamper = RFC3339Timestamp
165 // ArvLogWriter is an io.WriteCloser that processes each write by
166 // writing it through to another io.WriteCloser (typically a
167 // CollectionFileWriter) and creating an Arvados 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{"log": arvadosclient.Dict{
184 "object_uuid": arvlog.UUID,
185 "event_type": arvlog.loggingStream,
186 "properties": map[string]string{"text": string(p)}}}
187 err2 := arvlog.ArvClient.Create("logs", lr, nil)
189 if err1 != nil || err2 != nil {
190 return 0, fmt.Errorf("%s ; %s", err1, err2)
195 // Close the underlying writer
196 func (arvlog *ArvLogWriter) Close() (err error) {
197 if arvlog.writeCloser != nil {
198 err = arvlog.writeCloser.Close()
199 arvlog.writeCloser = nil