Skip to content

Commit

Permalink
output/csv: Support more time formats
Browse files Browse the repository at this point in the history
Added UnixNano, RFC3339Nano, UnixMilli, UnixMicro
  • Loading branch information
Azanul committed Feb 21, 2023
1 parent 87f97e0 commit aab7eae
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 6 deletions.
5 changes: 5 additions & 0 deletions output/csv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ type Config struct {
}

// TimeFormat custom enum type
//
//go:generate enumer -type=TimeFormat -transform=snake -trimprefix TimeFormat -output time_format_gen.go
type TimeFormat uint8

// valid defined values for TimeFormat
const (
TimeFormatUnix TimeFormat = iota
TimeFormatUnixMilli
TimeFormatUnixMicro
TimeFormatUnixNano
TimeFormatRFC3339
TimeFormatRFC3339Nano
)

// NewConfig creates a new Config instance with default values for some fields.
Expand Down
8 changes: 8 additions & 0 deletions output/csv/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,16 @@ func SampleToRow(sample *metrics.Sample, resTags []string, ignoredTags []string,
switch timeFormat {
case TimeFormatRFC3339:
row[1] = sample.Time.Format(time.RFC3339)
case TimeFormatRFC3339Nano:
row[1] = sample.Time.Format(time.RFC3339Nano)
case TimeFormatUnix:
row[1] = strconv.FormatInt(sample.Time.Unix(), 10)
case TimeFormatUnixMilli:
row[1] = strconv.FormatInt(sample.Time.UnixMilli(), 10)
case TimeFormatUnixMicro:
row[1] = strconv.FormatInt(sample.Time.UnixMicro(), 10)
case TimeFormatUnixNano:
row[1] = strconv.FormatInt(sample.Time.UnixNano(), 10)
}

row[2] = fmt.Sprintf("%f", sample.Value)
Expand Down
136 changes: 136 additions & 0 deletions output/csv/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,90 @@ func TestSampleToRow(t *testing.T) {
ignoredTags: []string{"tag4", "tag6"},
timeFormat: "rfc3339",
},
{
testname: "Two res tags, two ignored, with Unix Nano timestamp",
sample: &metrics.Sample{
TimeSeries: metrics.TimeSeries{
Metric: testMetric,
Tags: registry.RootTagSet().WithTagsFromMap(map[string]string{
"tag1": "val1",
"tag2": "val2",
"tag3": "val3",
"tag4": "val4",
"tag5": "val5",
"tag6": "val6",
}),
},
Time: time.Unix(1562324644, 25),
Value: 1,
},
resTags: []string{"tag1", "tag3"},
ignoredTags: []string{"tag4", "tag6"},
timeFormat: "unix_nano",
},
{
testname: "Two res tags, two ignored, with Unix Micro timestamp",
sample: &metrics.Sample{
TimeSeries: metrics.TimeSeries{
Metric: testMetric,
Tags: registry.RootTagSet().WithTagsFromMap(map[string]string{
"tag1": "val1",
"tag2": "val2",
"tag3": "val3",
"tag4": "val4",
"tag5": "val5",
"tag6": "val6",
}),
},
Time: time.Unix(1562324644, 2125),
Value: 1,
},
resTags: []string{"tag1", "tag3"},
ignoredTags: []string{"tag4", "tag6"},
timeFormat: "unix_micro",
},
{
testname: "Two res tags, two ignored, with Unix Milli timestamp",
sample: &metrics.Sample{
TimeSeries: metrics.TimeSeries{
Metric: testMetric,
Tags: registry.RootTagSet().WithTagsFromMap(map[string]string{
"tag1": "val1",
"tag2": "val2",
"tag3": "val3",
"tag4": "val4",
"tag5": "val5",
"tag6": "val6",
}),
},
Time: time.Unix(1562324644, 6482125),
Value: 1,
},
resTags: []string{"tag1", "tag3"},
ignoredTags: []string{"tag4", "tag6"},
timeFormat: "unix_milli",
},
{
testname: "Two res tags, two ignored, with RFC3339Nano timestamp",
sample: &metrics.Sample{
TimeSeries: metrics.TimeSeries{
Metric: testMetric,
Tags: registry.RootTagSet().WithTagsFromMap(map[string]string{
"tag1": "val1",
"tag2": "val2",
"tag3": "val3",
"tag4": "val4",
"tag5": "val5",
"tag6": "val6",
}),
},
Time: time.Unix(1562324644, 0),
Value: 1,
},
resTags: []string{"tag1", "tag3"},
ignoredTags: []string{"tag4", "tag6"},
timeFormat: "rfc3339_nano",
},
}

expected := []struct {
Expand Down Expand Up @@ -160,6 +244,58 @@ func TestSampleToRow(t *testing.T) {
"tag5=val5",
},
},
{
baseRow: []string{
"my_metric",
"1562324644000000025",
"1.000000",
"val1",
"val3",
},
extraRow: []string{
"tag2=val2",
"tag5=val5",
},
},
{
baseRow: []string{
"my_metric",
"1562324644000002",
"1.000000",
"val1",
"val3",
},
extraRow: []string{
"tag2=val2",
"tag5=val5",
},
},
{
baseRow: []string{
"my_metric",
"1562324644006",
"1.000000",
"val1",
"val3",
},
extraRow: []string{
"tag2=val2",
"tag5=val5",
},
},
{
baseRow: []string{
"my_metric",
time.Unix(1562324644, 0).Format(time.RFC3339Nano),
"1.000000",
"val1",
"val3",
},
extraRow: []string{
"tag2=val2",
"tag5=val5",
},
},
}

for i := range testData {
Expand Down
56 changes: 50 additions & 6 deletions output/csv/time_format_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aab7eae

Please sign in to comment.