Skip to content

Commit

Permalink
Creates a new flag to specify timestamp formats
Browse files Browse the repository at this point in the history
The ability to specify the timestamp format besides the previous default
ISO has become increasingly neccesary as users may require different
formats for their respective timestamps when merging the data provided
by carre with other tools.

This commit provides this feature through the --timestamp or -T flag
when calling the carre cli.
  • Loading branch information
lynxplay committed Apr 12, 2021
1 parent 5945a93 commit b16b90d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ on every system. The binaries can be found in the respective releases [here](htt

## Usage

### -container (required)
### --container | -C (required)

The container parameter specifies the name of the container for which the metrics are to be gathered. It is required as
this is the core point of caree,

### -interval
### --interval | -I

The interval parameter can specify the duration between each data point collected by caree.

#### Default: 500ms

#### Units: "ns", "us" (or "µs"), "ms", "s", "m", "h"

### -format
### --format | -F

The format parameter specifies in which format the output is printed. As of right now the follow formats exist.

Expand All @@ -51,6 +51,18 @@ The format parameter specifies in which format the output is printed. As of righ

#### Default: JSON

### --timestamp | -T

The timestamp parameter specifies in which format the timestamp for each data point is going to be printed. As of right
now the following formats exist.

- 'ISO' specifies the time format to follow [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html), more
specifically [RFC3339](https://tools.ietf.org/html/rfc3339), when printing the timestamps.
- 'EPOCH' specifies the timestamp to follow simple [unix epoch](https://en.wikipedia.org/wiki/Unix_time). The timestamp
will be represented as **milliseconds**.

#### Default: EPOCH

------------------------------------------------------------------------------------------------------------------------

## Examples
Expand Down
31 changes: 29 additions & 2 deletions cmd/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/docker/docker/client"
"log"
"strconv"
"strings"
"time"
)
Expand All @@ -18,15 +19,21 @@ var (
)

type OutputMode = uint
type TimestampFormat = uint

const (
CSV OutputMode = iota
JSON OutputMode = iota
)
const (
ISO TimestampFormat = iota
EPOCH TimestampFormat = iota
)

type DisplayContext struct {
Name string
OutputFormat OutputMode
TimestampFormat
}

func DisplayCurrentStats(dockerClient *client.Client, ctx DisplayContext) {
Expand All @@ -41,7 +48,7 @@ func DisplayCurrentStats(dockerClient *client.Client, ctx DisplayContext) {
for i := range result.Processes {
process := result.Processes[i]
processValues := make(map[string]string)
processValues["TIMESTAMP"] = time.Now().String()
processValues["TIMESTAMP"] = GetCurrentTime(ctx.TimestampFormat)
for j := range process {
processValues[result.Titles[j]] = process[j]
}
Expand All @@ -58,7 +65,7 @@ func DisplayCurrentStats(dockerClient *client.Client, ctx DisplayContext) {
}

for i := range result.Processes {
fmt.Println(strings.Join(result.Processes[i], ",") + "," + time.Now().String())
fmt.Println(strings.Join(result.Processes[i], ",") + "," + GetCurrentTime(ctx.TimestampFormat))
}
}
}
Expand All @@ -73,3 +80,23 @@ func ParseOutputFormat(str string) (OutputMode, error) {
return JSON, errors.New("failed to parse output format; defaulting to JSON")
}
}

func ParseTimestampFormat(str string) (TimestampFormat, error) {
switch strings.ToUpper(str) {
case "ISO":
return ISO, nil
case "EPOCH":
return EPOCH, nil // Different case than the default case, JSON was parsed correctly!
default:
return EPOCH, errors.New("failed to parse timestamp format; defaulting to epoch")
}
}

func GetCurrentTime(format TimestampFormat) string {
switch format {
case ISO:
return time.Now().Format(time.RFC3339)
default:
return strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
}
}
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (

func Execute(ctx *cli.Context) error {
outputFormat, err := ParseOutputFormat(ctx.String("format"))
timestampFormat, err := ParseTimestampFormat(ctx.String("timestamp"))

context := DisplayContext{
Name: ctx.String("container"),
OutputFormat: outputFormat,
Name: ctx.String("container"),
OutputFormat: outputFormat,
TimestampFormat: timestampFormat,
}

dockerClient, err := client.NewEnvClient()
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func main() {
Value: "JSON",
Usage: "output format of caree",
},
&cli.StringFlag{
Name: "timestamp",
Aliases: []string{"T"},
Value: "EPOCH",
Usage: "output format of the timestamps carre generates",
},
&cli.DurationFlag{
Name: "interval",
Aliases: []string{"I"},
Expand Down

0 comments on commit b16b90d

Please sign in to comment.