From c03bb01bca61b5424ad48fdc6868b1db7a59b5df Mon Sep 17 00:00:00 2001 From: prithviramesh Date: Fri, 19 Jan 2018 17:37:12 -0800 Subject: [PATCH] Fix fission function logs (#448) Remove hard coded field indices in influx query, instead searching for them by name. --- fission/logdb/influxdb.go | 40 ++++++++++++++----- test/tests/test_logging/test_function_logs.sh | 18 ++++++--- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/fission/logdb/influxdb.go b/fission/logdb/influxdb.go index b322055d..6adb2541 100644 --- a/fission/logdb/influxdb.go +++ b/fission/logdb/influxdb.go @@ -67,6 +67,15 @@ func (influx InfluxDB) GetPods(filter LogFilter) ([]string, error) { return pods, nil } +func makeIndexMap(cols []string) map[string]int { + indexMap := make(map[string]int, len(cols)) + for i := range cols { + indexMap[cols[i]] = i + } + + return indexMap +} + func (influx InfluxDB) GetLogs(filter LogFilter) ([]LogEntry, error) { timestamp := filter.Since.UnixNano() var queryCmd string @@ -93,26 +102,39 @@ func (influx InfluxDB) GetLogs(filter LogFilter) ([]LogEntry, error) { } for _, r := range response.Results { for _, series := range r.Series { + + //create map of columns to row indeces + indexMap := makeIndexMap(series.Columns) + + container := indexMap["docker_container_id"] + functionName := indexMap["kubernetes_labels_functionName"] + funcuid := indexMap["kubernetes_labels_functionUid"] + logMessage := indexMap["log"] + nameSpace := indexMap["kubernetes_namespace_name"] + podName := indexMap["kubernetes_pod_name"] + stream := indexMap["stream"] + seq := indexMap["_seq"] + for _, row := range series.Values { t, err := time.Parse(time.RFC3339, row[0].(string)) if err != nil { log.Fatal(err) } - seqNum, err := strconv.Atoi(row[1].(string)) + seqNum, err := strconv.Atoi(row[seq].(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, - Container: row[2].(string), //docker_container_id - FuncName: row[8].(string), //kubernetes_labels_functionName - FuncUid: row[3].(string), //funcuid - Message: strings.TrimSuffix(row[17].(string), "\n"), //log field - Namespace: row[14].(string), //kubernetes_namespace_name - Pod: row[15].(string), //kubernetes_pod_name - Stream: row[18].(string), //stream - Sequence: seqNum, //sequence tag + Container: row[container].(string), //docker_container_id + FuncName: row[functionName].(string), //kubernetes_labels_functionName + FuncUid: row[funcuid].(string), //funcuid + Message: strings.TrimSuffix(row[logMessage].(string), "\n"), //log field + Namespace: row[nameSpace].(string), //kubernetes_namespace_name + Pod: row[podName].(string), //kubernetes_pod_name + Stream: row[stream].(string), //stream + Sequence: seqNum, //sequence tag }) } } diff --git a/test/tests/test_logging/test_function_logs.sh b/test/tests/test_logging/test_function_logs.sh index 59bede20..6c28474a 100755 --- a/test/tests/test_logging/test_function_logs.sh +++ b/test/tests/test_logging/test_function_logs.sh @@ -1,6 +1,5 @@ #!/bin/bash -#test:disabled set -euo pipefail @@ -12,6 +11,8 @@ function cleanup { echo "Cleanup route" var=$(fission route list | grep $fn | awk '{print $1;}') fission route delete --name $var + echo "delete logfile" + rm "/tmp/logfile" } # Create a hello world function in nodejs, test it with an http trigger @@ -31,7 +32,7 @@ fission route create --function $fn --url /$fn --method GET trap cleanup EXIT echo "Waiting for router to catch up" -sleep 3 +sleep 15 echo "Doing 4 HTTP GETs on the function's route" for i in 1 2 3 4 @@ -43,11 +44,18 @@ echo "Grabbing logs, should have 4 calls in logs" sleep 15 -logs=$(fission function logs --name $fn --detail) +fission function logs --name $fn --detail > /tmp/logfile + +size=$(wc -c /tmp/logfile +fi + echo "---function logs---" -echo $logs +cat /tmp/logfile echo "------" -num=(cat "$logs" | grep 'log test' | wc -l) +num=$(grep 'log test' /tmp/logfile | wc -l) echo $num logs found if [ $num -ne 4 ]