Skip to content
Merged
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
69 changes: 31 additions & 38 deletions pkg/api/server/v1alpha2/plugin/plugin_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,37 +119,23 @@ func (s *LogServer) GetLog(req *pb3.GetLogRequest, srv pb3.Logs_GetLogServer) er
return nil
}

func getLokiLogs(s *LogServer, writer io.Writer, parent string, rec *db.Record) error {
URL, err := url.Parse(s.config.LOGGING_PLUGIN_API_URL)
if err != nil {
s.logger.Error(err)
return err
}
URL.Path = path.Join(URL.Path, s.config.LOGGING_PLUGIN_PROXY_PATH, lokiQueryPath)

var startTime, endTime, uidKey string
func (s *LogServer) getLogRequestParams(rec *db.Record) (startTime, endTime, uidKey string, err error) {
switch rec.Type {
case typePipelineRun:
uidKey = pipelineRunUIDKey
data := &pipelinev1.PipelineRun{}
err := json.Unmarshal(rec.Data, data)
if err != nil {
err = fmt.Errorf("failed to marshal pipelinerun data for fetching log, err: %s", err.Error())
s.logger.Error(err)
return err
return "", "", "", fmt.Errorf("failed to marshal pipelinerun data for fetching log, err: %w", err)
}
Comment on lines 126 to 130

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment make sense ; but at the same time, the error message was already there before this PR, so we can definitely fix this in a follow-up instead.


if data.Status.StartTime == nil {
err = errors.New("there's no startime in pipelinerun")
s.logger.Error(err)
return err
return "", "", "", errors.New("there's no startime in pipelinerun")
}
startTime = strconv.FormatInt(data.Status.StartTime.UTC().Unix(), 10)

if data.Status.CompletionTime == nil {
err = errors.New("there's no completion in pipelinerun")
s.logger.Error(err)
return err
return "", "", "", errors.New("there's no completion in pipelinerun")
}
endTime = strconv.FormatInt(data.Status.CompletionTime.Add(s.forwarderDelayDuration).UTC().Unix(), 10)
Comment on lines 137 to 140

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's by design though.


Expand All @@ -158,27 +144,36 @@ func getLokiLogs(s *LogServer, writer io.Writer, parent string, rec *db.Record)
data := &pipelinev1.TaskRun{}
err := json.Unmarshal(rec.Data, data)
if err != nil {
err = fmt.Errorf("failed to marshal taskrun data for fetching log, err: %s", err.Error())
s.logger.Error(err)
return err
return "", "", "", fmt.Errorf("failed to marshal taskrun data for fetching log, err: %w", err)
}
Comment on lines 144 to 148

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

if data.Status.StartTime == nil {
err = errors.New("there's no startime in taskrun")
s.logger.Error(err)
return err
return "", "", "", errors.New("there's no startime in taskrun")
}
startTime = strconv.FormatInt(data.Status.StartTime.UTC().Unix(), 10)

if data.Status.CompletionTime == nil {
err = errors.New("there's no completion in taskrun")
s.logger.Error(err)
return err
return "", "", "", errors.New("there's no completion in taskrun")
}
endTime = strconv.FormatInt(data.Status.CompletionTime.Add(s.forwarderDelayDuration).UTC().Unix(), 10)
Comment on lines 154 to 157

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's by design though.


default:
s.logger.Errorf("record type is invalid, record ID: %v, Name: %v, result Name: %v, result ID: %v", rec.ID, rec.Name, rec.ResultName, rec.ResultID)
return errors.New("record type is invalid")
return "", "", "", fmt.Errorf("record type is invalid, record ID: %v, Name: %v, result Name: %v, result ID: %v", rec.ID, rec.Name, rec.ResultName, rec.ResultID)
}
return startTime, endTime, uidKey, nil
}

func getLokiLogs(s *LogServer, writer io.Writer, parent string, rec *db.Record) error {
URL, err := url.Parse(s.config.LOGGING_PLUGIN_API_URL)
if err != nil {
s.logger.Error(err)
return err
}
URL.Path = path.Join(URL.Path, s.config.LOGGING_PLUGIN_PROXY_PATH, lokiQueryPath)

startTime, endTime, uidKey, err := s.getLogRequestParams(rec)
if err != nil {
s.logger.Error(err)
return err
}

parameters := url.Values{}
Expand Down Expand Up @@ -536,16 +531,12 @@ func getSplunkLogs(s *LogServer, writer io.Writer, parent string, rec *db.Record

}

var uidKey string
switch rec.Type {
case typePipelineRun:
uidKey = pipelineRunUIDKey
case typeTaskRun:
uidKey = taskRunUIDKey
default:
s.logger.Errorf("record type is invalid, record ID: %v, Name: %v, result Name: %v, result ID: %v, rec Type: %v", rec.ID, rec.Name, rec.ResultName, rec.ResultID, rec.Type)
return errors.New("record type is invalid")
startTime, endTime, uidKey, err := s.getLogRequestParams(rec)
if err != nil {
s.logger.Error(err)
return err
}

index, ok := s.queryParams["index"]
if !ok {
s.logger.Errorf("index not specified in queryParams: %v\n", s.queryParams)
Expand All @@ -560,6 +551,8 @@ func getSplunkLogs(s *LogServer, writer io.Writer, parent string, rec *db.Record

queryData := url.Values{}
queryData.Set("search", query)
queryData.Set("earliest_time", startTime)
queryData.Set("latest_time", endTime)

req, err := http.NewRequest("POST", URL.String()+splunkOutputFormat, bytes.NewReader([]byte(queryData.Encode())))
if err != nil {
Expand Down
20 changes: 18 additions & 2 deletions pkg/api/server/v1alpha2/plugin/plugin_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -254,6 +255,7 @@ func TestMergeLogParts(t *testing.T) {
}

func TestSplunkLogs(t *testing.T) {
var gotEarliestTime, gotLatestTime string

mockSplunk := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Log the received request for debugging
Expand All @@ -263,6 +265,9 @@ func TestSplunkLogs(t *testing.T) {
// Verify the request path and query parameters
switch r.URL.Path {
case "/services/search/v2/jobs":
r.ParseForm()
gotEarliestTime = r.FormValue("earliest_time")
gotLatestTime = r.FormValue("latest_time")
Comment on lines 257 to +270
w.WriteHeader(http.StatusCreated)
w.Write(json.RawMessage(`{"sid":"1234567"}`))
return
Expand Down Expand Up @@ -338,6 +343,9 @@ func TestSplunkLogs(t *testing.T) {
t.Fatalf("CreateResult: %v", err)
}

startTime := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
completionTime := time.Date(2024, 1, 1, 13, 0, 0, 0, time.UTC)

_, err = srv.CreateRecord(ctx, &pb.CreateRecordRequest{
Parent: res.GetName(),
Record: &pb.Record{
Expand All @@ -351,10 +359,10 @@ func TestSplunkLogs(t *testing.T) {
Status: pipelinev1.TaskRunStatus{
TaskRunStatusFields: pipelinev1.TaskRunStatusFields{
StartTime: &metav1.Time{
Time: time.Now().Add(-time.Hour),
Time: startTime,
},
CompletionTime: &metav1.Time{
Time: time.Now(),
Time: completionTime,
},
},
},
Expand Down Expand Up @@ -389,6 +397,14 @@ func TestSplunkLogs(t *testing.T) {
t.Errorf("expected to have received %q, got %q", expectedData, actualData)
}

wantEarliestTime := strconv.FormatInt(startTime.Unix(), 10)
wantLatestTime := strconv.FormatInt(completionTime.Unix(), 10)
if gotEarliestTime != wantEarliestTime {
t.Errorf("earliest_time = %q, want %q", gotEarliestTime, wantEarliestTime)
}
if gotLatestTime != wantLatestTime {
t.Errorf("latest_time = %q, want %q", gotLatestTime, wantLatestTime)
}
}

func TestGetLokiLogs_BuildsQueryWithConfiguredJSONMappingAndLineFormat(t *testing.T) {
Expand Down
Loading