Skip to content

Commit

Permalink
json timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
nonsense committed Oct 1, 2020
1 parent e962ea1 commit 905630a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion runtime/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (m *Metrics) writeToInfluxDBSink(measurementType string) MetricSinkFn {

measurementName := fmt.Sprintf("%s.%s.%s", measurementType, vals[0], metric.Type.String())

p, err := client.NewPoint(measurementName, tags, fields, time.Unix(0, metric.Timestamp))
p, err := client.NewPoint(measurementName, tags, fields, time.Time(metric.Timestamp))
if err != nil {
return err
}
Expand Down
21 changes: 18 additions & 3 deletions runtime/metrics_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,23 @@ func (mt *MetricType) UnmarshalJSON(b []byte) error {
return fmt.Errorf("invalid metric type")
}

type JSONTime time.Time

func (t *JSONTime) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", time.Time(*t).Format(time.RFC3339))), nil
}

func (t *JSONTime) UnmarshalJSON(b []byte) error {
tt, err := time.Parse(time.RFC3339, string(b[1:len(b)-1]))
if err != nil {
return err
}
*t = JSONTime(tt)
return nil
}

type Metric struct {
Timestamp int64 `json:"ts"`
Timestamp JSONTime `json:"ts"`
Type MetricType `json:"type"`
Name string `json:"name"`
Measures map[string]interface{} `json:"measures"`
Expand All @@ -75,7 +90,7 @@ func NewMetric(name string, i interface{}) *Metric {
var (
m *Metric
t MetricType
ts = time.Now().UnixNano()
ts = time.Now()
)

switch v := i.(type) {
Expand Down Expand Up @@ -163,7 +178,7 @@ func NewMetric(name string, i interface{}) *Metric {

}

m.Timestamp = ts
m.Timestamp = JSONTime(ts)
m.Type = t
m.Name = name
return m
Expand Down
1 change: 0 additions & 1 deletion runtime/runenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ func TestMetricsRecordedInFile(t *testing.T) {
na := make(map[string]struct{})
ty := make(map[string]struct{})
for _, m := range metrics {
require.Greater(m.Timestamp, int64(0))
na[m.Name] = struct{}{}
ty[m.Type.String()] = struct{}{}
require.NotZero(len(m.Measures))
Expand Down

0 comments on commit 905630a

Please sign in to comment.