fission function logs returns logs in correct order now (#405)

Fix a bug in `fission function logs` due to which logs with the same timestamp were printed in the wrong order.
This commit is contained in:
prithviramesh
2017-11-22 18:05:49 -08:00
committed by Soam Vasani
parent 04531850d5
commit 2247d3118a
4 changed files with 38 additions and 11 deletions
+20 -2
View File
@@ -22,6 +22,8 @@ import (
"log"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
@@ -74,12 +76,13 @@ func (influx InfluxDB) GetLogs(filter LogFilter) ([]LogEntry, error) {
parameters := make(map[string]interface{})
parameters["funcuid"] = filter.FuncUid
parameters["time"] = timestamp
//the parameters above are only for the where clause and do not work with LIMIT
if filter.Pod != "" {
queryCmd = "select * from \"log\" where \"funcuid\" = $funcuid AND \"pod\" = $pod AND \"time\" > $time ORDER BY time ASC"
queryCmd = "select * from \"log\" where \"funcuid\" = $funcuid AND \"pod\" = $pod AND \"time\" > $time LIMIT " + strconv.Itoa(filter.RecordLimit)
parameters["pod"] = filter.Pod
} else {
queryCmd = "select * from \"log\" where \"funcuid\" = $funcuid AND \"time\" > $time ORDER BY time ASC"
queryCmd = "select * from \"log\" where \"funcuid\" = $funcuid AND \"time\" > $time LIMIT " + strconv.Itoa(filter.RecordLimit)
}
query := influxdbClient.NewQueryWithParameters(queryCmd, INFLUXDB_DATABASE, "", parameters)
@@ -95,6 +98,10 @@ func (influx InfluxDB) GetLogs(filter LogFilter) ([]LogEntry, error) {
if err != nil {
log.Fatal(err)
}
seqNum, err := strconv.Atoi(row[1].(string))
if err != nil {
return logEntries, err
}
logEntries = append(logEntries, LogEntry{
//The attributes of the LogEntry are selected as relative to their position in InfluxDB's line protocol response
Timestamp: t,
@@ -105,10 +112,21 @@ func (influx InfluxDB) GetLogs(filter LogFilter) ([]LogEntry, error) {
Namespace: row[14].(string), //kubernetes_namespace_name
Pod: row[15].(string), //kubernetes_pod_name
Stream: row[18].(string), //stream
Sequence: seqNum, //sequence tag
})
}
}
}
sort.Slice(logEntries, func(i, j int) bool {
if logEntries[i].Timestamp.Before(logEntries[j].Timestamp) {
return true
}
if logEntries[j].Timestamp.Before(logEntries[i].Timestamp) {
return false
}
return logEntries[i].Sequence < logEntries[j].Sequence
})
return logEntries, nil
}
+6 -4
View File
@@ -32,16 +32,18 @@ type LogDatabase interface {
}
type LogFilter struct {
Pod string
Function string
FuncUid string
Since time.Time
Pod string
Function string
FuncUid string
Since time.Time
RecordLimit int
}
type LogEntry struct {
Timestamp time.Time
Message string
Stream string
Sequence int
Container string
Namespace string
FuncName string