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

[release-v0.13.x] Fix Logs ordered from the Loki response #950

Open
wants to merge 1 commit into
base: release-v0.13.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions pkg/api/server/v1alpha2/plugin/plugin_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/url"
"path"
"path/filepath"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -237,6 +238,7 @@ func getLokiLogs(s *LogServer, writer io.Writer, parent string, rec *db.Record)
Stream struct {
Message string `json:"message"`
} `json:"stream"`
Values [][]string `json:"values"`
} `json:"result"`
Stats map[string]interface{} `json:"stats"`
} `json:"data"`
Expand All @@ -255,20 +257,37 @@ func getLokiLogs(s *LogServer, writer io.Writer, parent string, rec *db.Record)
return status.Error(codes.Internal, "Error fetching log data from Loki")
}

var logMessages []string
var logMessages [][]string
for _, result := range lokiResponse.Data.Result {
logMessages = append(logMessages, result.Stream.Message)
logMessages = append(logMessages, result.Values...)
}

formattedLogs := strings.Join(logMessages, "\n")
sort.Slice(logMessages, func(i, j int) bool {
if len(logMessages[i]) == 0 {
return true
}
if len(logMessages[j]) == 0 {
return false
}
return logMessages[i][0] < logMessages[j][0]
})

var orderMessages []string
for _, val := range logMessages {
if len(val) <= 1 {
continue
}
orderMessages = append(orderMessages, val[1:]...)
}

formattedLogs := strings.Join(orderMessages, "\n")

if _, err = writer.Write([]byte(formattedLogs)); err != nil {
s.logger.Errorf("failed to write log data, err: %s", err.Error())
return status.Error(codes.Internal, "Error streaming log")
}

return nil

}

func getBlobLogs(s *LogServer, writer io.Writer, parent string, rec *db.Record) error {
Expand Down
18 changes: 15 additions & 3 deletions pkg/api/server/v1alpha2/plugin/plugin_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,21 @@ func TestLogPluginServer_GetLog(t *testing.T) {
"data": map[string]interface{}{
"result": []map[string]interface{}{
{
"stream": map[string]string{"message": "Foo Bar!"},
"stream": map[string]string{},
"values": [][]string{
{"1625081600000000000", "Hello World!"},
{"1625081600000000001", "Log Message 1"},
},
},
{
"stream": map[string]string{},
"values": [][]string{
{"1625081600000000003", "Log Message 3"},
},
},
{
"stream": map[string]string{},
"values": [][]string{
{"1625081600000000000", "Log Message 0"},
},
},
},
Expand Down Expand Up @@ -139,7 +151,7 @@ func TestLogPluginServer_GetLog(t *testing.T) {
Name: log.FormatName(res.GetName(), "baz"),
}

expectedData := "Foo Bar!"
expectedData := "Log Message 0\nLog Message 1\nLog Message 3"
// Call GetLog
err = srv.LogPluginServer.GetLog(req, mockServer)
if err != nil {
Expand Down