Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Csv time formats #2906

Merged
merged 12 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.