diff --git a/traffic_monitor/cache/cache.go b/traffic_monitor/cache/cache.go index 339c4a5f5c..9bedd3494f 100644 --- a/traffic_monitor/cache/cache.go +++ b/traffic_monitor/cache/cache.go @@ -23,6 +23,7 @@ import ( "fmt" "io" "regexp" + "strconv" "time" "github.com/apache/trafficcontrol/lib/go-log" @@ -306,6 +307,17 @@ func (handler Handler) Handle(id string, rdr io.Reader, format string, reqTime t handler.resultChan <- result return } + if val, ok := miscStats["plugin.system_stats.timestamp_ms"]; ok { + valString := fmt.Sprintf("%s", val) + valInt, valErr := strconv.ParseInt(valString, 10, 64) + if valErr != nil { + log.Errorf("parse error '%v'", valErr) + result.Error = valErr + handler.resultChan <- result + return + } + result.Time = time.UnixMilli(valInt) + } if value, ok := miscStats[rfc.Via]; ok { result.ID = fmt.Sprintf("%v", value) } diff --git a/traffic_monitor/cache/cache_test.go b/traffic_monitor/cache/cache_test.go index b20a392507..7d3e685e75 100644 --- a/traffic_monitor/cache/cache_test.go +++ b/traffic_monitor/cache/cache_test.go @@ -20,10 +20,15 @@ package cache */ import ( + "bytes" + "fmt" + "io/ioutil" + "net/http" "testing" "github.com/apache/trafficcontrol/lib/go-tc" "github.com/apache/trafficcontrol/lib/go-util" + "github.com/apache/trafficcontrol/traffic_monitor/poller" "github.com/apache/trafficcontrol/traffic_monitor/todata" ) @@ -95,3 +100,37 @@ func TestComputeStatGbps(t *testing.T) { } } } + +func TestParseAndDecode(t *testing.T) { + file, err := ioutil.ReadFile("stats_over_http.json") + if err != nil { + t.Fatal(err) + } + + pl := &poller.HTTPPollCtx{HTTPHeader: http.Header{}} + ctx := interface{}(pl) + ctx.(*poller.HTTPPollCtx).HTTPHeader.Set("Content-Type", "text/json") + + decoder, err := GetDecoder("stats_over_http") + if err != nil { + t.Errorf("decoder error, expected: nil, got: %v", err) + } + + _, miscStats, err := decoder.Parse("1", bytes.NewReader(file), ctx) + if err != nil { + t.Errorf("decoder parse error, expected: nil, got: %v", err) + } + + if len(miscStats) < 1 { + t.Errorf("empty miscStats structure") + } + + if val, ok := miscStats["plugin.system_stats.timestamp_ms"]; ok { + valString := fmt.Sprintf("%s", val) + if valString != "1684784877939" { + t.Errorf("unable to read `plugin.system_stats.timestamp_ms`") + } + } else { + t.Errorf("plugin.system_stats.timestamp_ms field was not found in the json file") + } +}