diff --git a/pkg/api/server/config/config.go b/pkg/api/server/config/config.go index e658553c10..c73d6bd4a7 100644 --- a/pkg/api/server/config/config.go +++ b/pkg/api/server/config/config.go @@ -72,6 +72,8 @@ type Config struct { LOGGING_PLUGIN_MULTIPART_REGEX string `mapstructure:"LOGGING_PLUGIN_MULTIPART_REGEX"` LOGGING_PLUGIN_JSON_MAP string `mapstructure:"LOGGING_PLUGIN_JSON_MAP"` LOGGING_PLUGIN_LINE_FORMAT string `mapstructure:"LOGGING_PLUGIN_LINE_FORMAT"` + LOGGING_PLUGIN_PIPELINERUN_UID_KEY string `mapstructure:"LOGGING_PLUGIN_PIPELINERUN_UID_KEY"` + LOGGING_PLUGIN_TASKRUN_UID_KEY string `mapstructure:"LOGGING_PLUGIN_TASKRUN_UID_KEY"` } func Get() *Config { diff --git a/pkg/api/server/v1alpha2/plugin/plugin_logs.go b/pkg/api/server/v1alpha2/plugin/plugin_logs.go index 6ce32d0d60..e828aadf14 100644 --- a/pkg/api/server/v1alpha2/plugin/plugin_logs.go +++ b/pkg/api/server/v1alpha2/plugin/plugin_logs.go @@ -122,7 +122,10 @@ func (s *LogServer) GetLog(req *pb3.GetLogRequest, srv pb3.Logs_GetLogServer) er func (s *LogServer) getLogRequestParams(rec *db.Record) (startTime, endTime, uidKey string, err error) { switch rec.Type { case typePipelineRun: - uidKey = pipelineRunUIDKey + uidKey = s.config.LOGGING_PLUGIN_PIPELINERUN_UID_KEY + if uidKey == "" { + uidKey = pipelineRunUIDKey + } data := &pipelinev1.PipelineRun{} err := json.Unmarshal(rec.Data, data) if err != nil { @@ -140,7 +143,10 @@ func (s *LogServer) getLogRequestParams(rec *db.Record) (startTime, endTime, uid endTime = strconv.FormatInt(data.Status.CompletionTime.Add(s.forwarderDelayDuration).UTC().Unix(), 10) case typeTaskRun: - uidKey = taskRunUIDKey + uidKey = s.config.LOGGING_PLUGIN_TASKRUN_UID_KEY + if uidKey == "" { + uidKey = taskRunUIDKey + } data := &pipelinev1.TaskRun{} err := json.Unmarshal(rec.Data, data) if err != nil { diff --git a/pkg/api/server/v1alpha2/plugin/plugin_logs_test.go b/pkg/api/server/v1alpha2/plugin/plugin_logs_test.go index 921ddf35c4..5a3347f17f 100644 --- a/pkg/api/server/v1alpha2/plugin/plugin_logs_test.go +++ b/pkg/api/server/v1alpha2/plugin/plugin_logs_test.go @@ -172,6 +172,75 @@ func TestLogPluginServer_GetLog(t *testing.T) { } +func TestLogPluginServer_GetLog_WithConfigurableUIDKey(t *testing.T) { + // Create a mock Loki server + mockLoki := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Verify that the custom UID key is used in the query + if !strings.Contains(r.URL.String(), "custom_pipelinerun_uid_key") { + t.Errorf("expected query to contain custom_pipelinerun_uid_key, got %s", r.URL.String()) + } + + response := map[string]interface{}{ + "status": "success", + "data": map[string]interface{}{ + "result": []map[string]interface{}{ + { + "stream": map[string]string{}, + "values": [][]string{ + {"1625081600000000000", "Log Message 0"}, + }, + }, + }, + }, + } + json.NewEncoder(w).Encode(response) + })) + defer mockLoki.Close() + + tokenDir := t.TempDir() + tokenPath := filepath.Join(tokenDir, "token") + os.WriteFile(tokenPath, []byte("dummytoken"), 0600) + + srv, _ := server.New(&config.Config{ + LOGS_API: true, + LOGS_TYPE: "Loki", + DB_ENABLE_AUTO_MIGRATION: true, + LOGGING_PLUGIN_TOKEN_PATH: tokenPath, + LOGGING_PLUGIN_API_URL: mockLoki.URL, + LOGGING_PLUGIN_TLS_VERIFICATION_DISABLE: true, + LOGGING_PLUGIN_PIPELINERUN_UID_KEY: "custom_pipelinerun_uid_key", + }, logger.Get("info"), test.NewDB(t)) + + ctx := context.Background() + mockServer := &mockGetLogServer{ctx: ctx} + + res, _ := srv.CreateResult(ctx, &pb.CreateResultRequest{ + Parent: "foo", + Result: &pb.Result{Name: "foo/results/bar"}, + }) + + srv.CreateRecord(ctx, &pb.CreateRecordRequest{ + Parent: res.GetName(), + Record: &pb.Record{ + Name: record.FormatName(res.GetName(), "baz"), + Data: &pb.Any{ + Type: "tekton.dev/v1.PipelineRun", + Value: jsonutil.AnyBytes(t, pipelinev1.PipelineRun{ + Status: pipelinev1.PipelineRunStatus{ + PipelineRunStatusFields: pipelinev1.PipelineRunStatusFields{ + StartTime: &metav1.Time{Time: time.Now()}, + CompletionTime: &metav1.Time{Time: time.Now()}, + }, + }, + }), + }, + }, + }) + + req := &pb3.GetLogRequest{Name: log.FormatName(res.GetName(), "baz")} + srv.LogPluginServer.GetLog(req, mockServer) +} + func TestMergeLogParts(t *testing.T) { tests := []struct { name string