-
Notifications
You must be signed in to change notification settings - Fork 102
plugin(logs): support time based queries for Splunk #1317
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
| } | ||
|
|
||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's by design though. |
||
|
|
||
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{} | ||
|
|
@@ -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) | ||
|
|
@@ -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 { | ||
|
|
||
There was a problem hiding this comment.
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.