@@ -2,8 +2,10 @@ package docker_log
2
2
3
3
import (
4
4
"bufio"
5
+ "bytes"
5
6
"context"
6
7
"crypto/tls"
8
+ "fmt"
7
9
"io"
8
10
"strings"
9
11
"sync"
@@ -287,7 +289,7 @@ func (d *DockerLogs) tailContainerLogs(
287
289
logOptions := types.ContainerLogsOptions {
288
290
ShowStdout : true ,
289
291
ShowStderr : true ,
290
- Timestamps : false ,
292
+ Timestamps : true ,
291
293
Details : false ,
292
294
Follow : true ,
293
295
Tail : tail ,
@@ -311,6 +313,30 @@ func (d *DockerLogs) tailContainerLogs(
311
313
}
312
314
}
313
315
316
+ func parseLine (line []byte ) (time.Time , string , error ) {
317
+ parts := bytes .SplitN (line , []byte (" " ), 2 )
318
+
319
+ switch len (parts ) {
320
+ case 1 :
321
+ parts = append (parts , []byte ("" ))
322
+ }
323
+
324
+ tsString := string (parts [0 ])
325
+
326
+ // Keep any leading space, but remove whitespace from end of line.
327
+ // This preserves space in, for example, stacktraces, while removing
328
+ // annoying end of line characters and is similar to how other logging
329
+ // plugins such as syslog behave.
330
+ message := bytes .TrimRightFunc (parts [1 ], unicode .IsSpace )
331
+
332
+ ts , err := time .Parse (time .RFC3339Nano , tsString )
333
+ if err != nil {
334
+ return time.Time {}, "" , fmt .Errorf ("error parsing timestamp %q: %v" , tsString , err )
335
+ }
336
+
337
+ return ts , string (message ), nil
338
+ }
339
+
314
340
func tailStream (
315
341
acc telegraf.Accumulator ,
316
342
baseTags map [string ]string ,
@@ -328,22 +354,19 @@ func tailStream(
328
354
329
355
r := bufio .NewReaderSize (reader , 64 * 1024 )
330
356
331
- var err error
332
- var message string
333
357
for {
334
- message , err = r .ReadString ('\n' )
335
-
336
- // Keep any leading space, but remove whitespace from end of line.
337
- // This preserves space in, for example, stacktraces, while removing
338
- // annoying end of line characters and is similar to how other logging
339
- // plugins such as syslog behave.
340
- message = strings .TrimRightFunc (message , unicode .IsSpace )
341
-
342
- if len (message ) != 0 {
343
- acc .AddFields ("docker_log" , map [string ]interface {}{
344
- "container_id" : containerID ,
345
- "message" : message ,
346
- }, tags )
358
+ line , err := r .ReadBytes ('\n' )
359
+
360
+ if len (line ) != 0 {
361
+ ts , message , err := parseLine (line )
362
+ if err != nil {
363
+ acc .AddError (err )
364
+ } else {
365
+ acc .AddFields ("docker_log" , map [string ]interface {}{
366
+ "container_id" : containerID ,
367
+ "message" : message ,
368
+ }, tags , ts )
369
+ }
347
370
}
348
371
349
372
if err != nil {
0 commit comments