RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
feat(observability): carry user_name/tenant_name through IngestionLog (#21968)
xugangqiang318b9281eb8d
diff --git a/internal/common/stub.go b/internal/common/stub.goindex 686b52421f..a8b2eb9f40 100644--- a/internal/common/stub.go+++ b/internal/common/stub.go@@ -75,6 +75,7 @@ type IngestionLog struct {DocID string `json:"doc_id"`UserID string `json:"user_id"`UserName string `json:"user_name,omitempty"`+ TenantName string `json:"tenant_name,omitempty"`Worker string `json:"worker"`TaskID string `json:"task_id"`EventType string `json:"event_type"`diff --git a/internal/observability/ingestion_log.go b/internal/observability/ingestion_log.goindex 4c1648b0e8..7df895c9b4 100644--- a/internal/observability/ingestion_log.go+++ b/internal/observability/ingestion_log.go@@ -86,16 +86,18 @@ func claimTime(doc *entity.Document, run *entity.PipelineOperationLog) *time.Tim// BuildIngestLog assembles a common.IngestionLog from the task, its document, and// the bound pipeline run. eventType is one of// created|scheduled|running|completed|failed|stopped.-func BuildIngestLog(task *entity.IngestionTask, doc *entity.Document, run *entity.PipelineOperationLog, eventType, status, errorInfo string) common.IngestionLog {+func BuildIngestLog(task *entity.IngestionTask, doc *entity.Document, run *entity.PipelineOperationLog, eventType, status, errorInfo, userName, tenantName string) common.IngestionLog {ev := common.IngestionLog{- EventTime: time.Now().UTC(),- TaskID: task.ID,- DocID: task.DocumentID,- DatasetID: task.DatasetID,- UserID: task.UserID,- EventType: eventType,- Status: status,- ErrorInfo: errorInfo,+ EventTime: time.Now().UTC(),+ TaskID: task.ID,+ DocID: task.DocumentID,+ DatasetID: task.DatasetID,+ UserID: task.UserID,+ UserName: userName,+ TenantName: tenantName,+ EventType: eventType,+ Status: status,+ ErrorInfo: errorInfo,}if doc != nil {name := ""diff --git a/internal/observability/ingestion_log_endtime_test.go b/internal/observability/ingestion_log_endtime_test.goindex 0986e09293..c18418027b 100644--- a/internal/observability/ingestion_log_endtime_test.go+++ b/internal/observability/ingestion_log_endtime_test.go@@ -23,7 +23,7 @@ func TestBuildIngestLogDerivesEndTimeFromBeginAndDuration(t *testing.T) {task := &entity.IngestionTask{ID: "task-1"}doc := &entity.Document{Name: ptrStr("d"), CreateDate: ptrTime(base)}- ev := BuildIngestLog(task, doc, run, "completed", "COMPLETED", "")+ ev := BuildIngestLog(task, doc, run, "completed", "COMPLETED", "", "", "")want := begin.Add(time.Duration(duration * float64(time.Second)))if !ev.EndTime.Equal(want) {t.Fatalf("EndTime = %v, want %v (ProcessBeginAt + ProcessDuration)", ev.EndTime, want)@@ -46,7 +46,7 @@ func TestBuildIngestLogZeroDurationFallsBackToEventTime(t *testing.T) {task := &entity.IngestionTask{ID: "task-1"}doc := &entity.Document{Name: ptrStr("d"), CreateDate: ptrTime(base), ProcessBeginAt: ptrTime(begin)}- ev := BuildIngestLog(task, doc, run, "completed", "COMPLETED", "")+ ev := BuildIngestLog(task, doc, run, "completed", "COMPLETED", "", "", "")if !ev.EndTime.Equal(ev.EventTime) {t.Fatalf("EndTime = %v, want EventTime %v (duration 0 fallback)", ev.EndTime, ev.EventTime)}diff --git a/internal/observability/ingestion_log_fix_test.go b/internal/observability/ingestion_log_fix_test.goindex 073ffaaf3d..7dece96e59 100644--- a/internal/observability/ingestion_log_fix_test.go+++ b/internal/observability/ingestion_log_fix_test.go@@ -16,7 +16,7 @@ func TestBuildIngestLogPrefersWorkerClaimTime(t *testing.T) {doc := &entity.Document{Name: ptrStr("r"), Suffix: ".pdf", Type: "pdf", Size: 10, ParserID: "pdf", ProcessBeginAt: ptrTime(claimTime)}run := &entity.PipelineOperationLog{RunCount: ptrInt(1), CreateDate: ptrTime(enqueue), ProcessBeginAt: ptrTime(openTime)}- log := BuildIngestLog(task, doc, run, "completed", "COMPLETED", "")+ log := BuildIngestLog(task, doc, run, "completed", "COMPLETED", "", "", "")if !log.BeginTime.Equal(claimTime) {t.Errorf("BeginTime = %v, want worker-claim time %v", log.BeginTime, claimTime)}@@ -31,7 +31,7 @@ func TestBuildIngestLogPopulatesDurationMS(t *testing.T) {doc := &entity.Document{Name: ptrStr("r"), Suffix: ".pdf", Type: "pdf", Size: 10, ParserID: "pdf", ProcessBeginAt: ptrTime(base.Add(10 * time.Minute))}run := &entity.PipelineOperationLog{RunCount: ptrInt(1), CreateDate: ptrTime(base), ProcessBeginAt: ptrTime(base.Add(1 * time.Minute)), ProcessDuration: 600}- log := BuildIngestLog(task, doc, run, "completed", "COMPLETED", "")+ log := BuildIngestLog(task, doc, run, "completed", "COMPLETED", "", "", "")want := int64(log.ParseDurationS * 1000)if want <= 0 {t.Fatalf("ParseDurationS = %v, want > 0 for setup", log.ParseDurationS)@@ -40,7 +40,7 @@ func TestBuildIngestLogPopulatesDurationMS(t *testing.T) {t.Errorf("DurationMS = %d, want %d", log.DurationMS, want)}- created := BuildIngestLog(task, doc, &entity.PipelineOperationLog{RunCount: ptrInt(1)}, "created", "CREATED", "")+ created := BuildIngestLog(task, doc, &entity.PipelineOperationLog{RunCount: ptrInt(1)}, "created", "CREATED", "", "", "")if created.DurationMS != 0 {t.Errorf("created DurationMS = %d, want 0", created.DurationMS)}diff --git a/internal/observability/ingestion_log_names_test.go b/internal/observability/ingestion_log_names_test.gonew file mode 100644index 0000000000..6fe90b18b3--- /dev/null+++ b/internal/observability/ingestion_log_names_test.go@@ -0,0 +1,52 @@+package observability++import (+ "testing"++ "ragflow/internal/entity"+)++// TestBuildIngestLogPropagatesUserAndTenantName asserts the caller-supplied+// display names are carried onto the event verbatim. This keeps the OSS+// IngestionLog / BuildIngestLog contract aligned with the EE build, which+// resolves the names from the user/tenant DAOs before emitting.+func TestBuildIngestLogPropagatesUserAndTenantName(t *testing.T) {+ task := &entity.IngestionTask{ID: "t", DocumentID: "d", DatasetID: "kb", UserID: "u1"}+ doc := &entity.Document{Name: ptrStr("r"), Suffix: ".pdf", Type: "pdf", Size: 10, ParserID: "pdf"}+ run := &entity.PipelineOperationLog{RunCount: ptrInt(1), TenantID: "tenant-1"}++ ev := BuildIngestLog(task, doc, run, "completed", "COMPLETED", "", "alice", "Acme Corp")++ if ev.UserName != "alice" {+ t.Errorf("UserName = %q, want %q", ev.UserName, "alice")+ }+ if ev.TenantName != "Acme Corp" {+ t.Errorf("TenantName = %q, want %q", ev.TenantName, "Acme Corp")+ }+ if ev.UserID != "u1" {+ t.Errorf("UserID = %q, want u1", ev.UserID)+ }+ if ev.TenantID != "tenant-1" {+ t.Errorf("TenantID = %q, want tenant-1", ev.TenantID)+ }+}++// TestBuildIngestLogEmptyNamesStaysEmpty verifies that empty display names do+// not corrupt the event (the EE-side best-effort resolution may yield "").+func TestBuildIngestLogEmptyNamesStaysEmpty(t *testing.T) {+ task := &entity.IngestionTask{ID: "t", DocumentID: "d", DatasetID: "kb", UserID: "u1"}+ doc := &entity.Document{Name: ptrStr("r"), Suffix: ".pdf", Type: "pdf", Size: 10, ParserID: "pdf"}+ run := &entity.PipelineOperationLog{RunCount: ptrInt(1), TenantID: "tenant-1"}++ ev := BuildIngestLog(task, doc, run, "running", "RUNNING", "", "", "")++ if ev.UserName != "" {+ t.Errorf("UserName = %q, want empty", ev.UserName)+ }+ if ev.TenantName != "" {+ t.Errorf("TenantName = %q, want empty", ev.TenantName)+ }+ if ev.EventTime.IsZero() {+ t.Errorf("EventTime should be set, got zero")+ }+}diff --git a/internal/observability/ingestion_log_tdd_test.go b/internal/observability/ingestion_log_tdd_test.goindex 016325c03d..2e320a03b2 100644--- a/internal/observability/ingestion_log_tdd_test.go+++ b/internal/observability/ingestion_log_tdd_test.go@@ -53,7 +53,7 @@ func TestBuildIngestLogPreClaimStoppedOmitsStaleClaimTime(t *testing.T) {doc := &entity.Document{Name: ptrStr("r"), Suffix: ".pdf", Type: "pdf", Size: 10, ParserID: "pdf", ProcessBeginAt: ptrTime(staleClaim)}run := &entity.PipelineOperationLog{RunCount: ptrInt(1), CreateDate: ptrTime(runOpen)}- ev := BuildIngestLog(task, doc, run, "stopped", "STOPPED", "stopped by user")+ ev := BuildIngestLog(task, doc, run, "stopped", "STOPPED", "stopped by user", "", "")if !ev.BeginTime.IsZero() {t.Errorf("pre-claim stopped must omit stale BeginTime, got %v", ev.BeginTime)}@@ -75,7 +75,7 @@ func TestBuildIngestLogPreClaimFailedOmitsStaleClaimTime(t *testing.T) {doc := &entity.Document{Name: ptrStr("r"), Suffix: ".pdf", Type: "pdf", Size: 10, ParserID: "pdf", ProcessBeginAt: ptrTime(staleClaim)}run := &entity.PipelineOperationLog{RunCount: ptrInt(1), CreateDate: ptrTime(runOpen)}- ev := BuildIngestLog(task, doc, run, "failed", "FAILED", "publish failed")+ ev := BuildIngestLog(task, doc, run, "failed", "FAILED", "publish failed", "", "")if !ev.BeginTime.IsZero() {t.Errorf("pre-claim failed must omit stale BeginTime, got %v", ev.BeginTime)}@@ -94,7 +94,7 @@ func TestBuildIngestLogRunningKeepsValidClaimTime(t *testing.T) {doc := &entity.Document{Name: ptrStr("r"), Suffix: ".pdf", Type: "pdf", Size: 10, ParserID: "pdf", ProcessBeginAt: ptrTime(claim)}run := &entity.PipelineOperationLog{RunCount: ptrInt(1), CreateDate: ptrTime(runOpen), ProcessBeginAt: ptrTime(runOpen), ProcessDuration: 1200}- ev := BuildIngestLog(task, doc, run, "running", "RUNNING", "")+ ev := BuildIngestLog(task, doc, run, "running", "RUNNING", "", "", "")if !ev.BeginTime.Equal(claim) {t.Errorf("running BeginTime = %v, want %v", ev.BeginTime, claim)}diff --git a/internal/observability/ingestion_log_test.go b/internal/observability/ingestion_log_test.goindex 1d022e8ecc..c61e49f8d6 100644--- a/internal/observability/ingestion_log_test.go+++ b/internal/observability/ingestion_log_test.go@@ -96,7 +96,7 @@ func TestBuildIngestLogHappy(t *testing.T) {ProcessBeginAt: ptrTime(now.Add(2 * time.Minute)),ProcessDuration: 600,}- log := BuildIngestLog(task, doc, run, "completed", "COMPLETED", "")+ log := BuildIngestLog(task, doc, run, "completed", "COMPLETED", "", "", "")if log.TaskID != "task-1" {t.Errorf("TaskID = %q, want task-1", log.TaskID)@@ -151,7 +151,7 @@ func TestBuildIngestLogCreatedSkipsClaimTime(t *testing.T) {ProcessBeginAt: ptrTime(now.Add(2 * time.Minute)),}run := &entity.PipelineOperationLog{RunCount: ptrInt(1), CreateDate: ptrTime(now)}- log := BuildIngestLog(task, doc, run, "created", "CREATED", "")+ log := BuildIngestLog(task, doc, run, "created", "CREATED", "", "", "")if !log.BeginTime.IsZero() {t.Errorf("created event BeginTime = %v, want zero", log.BeginTime)}@@ -162,7 +162,7 @@ func TestBuildIngestLogFailedCarriesErrorInfo(t *testing.T) {task := &entity.IngestionTask{ID: "task-1", DocumentID: "doc-1", DatasetID: "kb-1", UserID: "user-1"}doc := &entity.Document{Name: ptrStr("docname"), Suffix: ".pdf", Type: "pdf", Size: 10, ParserID: "pdf"}run := &entity.PipelineOperationLog{RunCount: ptrInt(1), CreateDate: ptrTime(now)}- log := BuildIngestLog(task, doc, run, "failed", "FAILED", "boom")+ log := BuildIngestLog(task, doc, run, "failed", "FAILED", "boom", "", "")if log.ErrorInfo != "boom" {t.Errorf("ErrorInfo = %q, want boom", log.ErrorInfo)}diff --git a/internal/service/ingestion_task_emit_names_test.go b/internal/service/ingestion_task_emit_names_test.gonew file mode 100644index 0000000000..467b5836ad--- /dev/null+++ b/internal/service/ingestion_task_emit_names_test.go@@ -0,0 +1,44 @@+package service++import (+ "context"+ "testing"++ "ragflow/internal/entity"+)++// TestEmitLifecycleEventPropagatesEmptyNames guards the OSS call site of+// BuildIngestLog. The OSS build forwards empty user/tenant names (the EE build+// resolves them from the user/tenant DAOs before emitting), so the emitted event+// must carry UserName == "" and TenantName == "" while still propagating the+// task identity. This also pins the call shape: BuildIngestLog must be invoked+// with the 8-arg form and EmitIngestLog must receive exactly one+// common.IngestionLog. The original bug passed the two extra "" args to+// EmitIngestLog (which takes only ctx+log) and omitted them from BuildIngestLog,+// which broke compilation of the whole service package.+func TestEmitLifecycleEventPropagatesEmptyNames(t *testing.T) {+ rec := &recordingEmitter{}+ svc := &IngestionTaskService{eventEmitter: rec}++ task := &entity.IngestionTask{+ ID: "task-n",+ DocumentID: "doc-n",+ DatasetID: "kb-n",+ UserID: "u-n",+ }+ svc.emitLifecycleEvent(context.Background(), task, "completed", "COMPLETED", "")++ if len(rec.events) != 1 {+ t.Fatalf("expected exactly 1 emitted event, got %d", len(rec.events))+ }+ got := rec.last()+ if got.UserName != "" {+ t.Errorf("UserName = %q, want empty (OSS forwards empty names)", got.UserName)+ }+ if got.TenantName != "" {+ t.Errorf("TenantName = %q, want empty (OSS forwards empty names)", got.TenantName)+ }+ if got.UserID != "u-n" {+ t.Errorf("UserID = %q, want u-n", got.UserID)+ }+}diff --git a/internal/service/ingestion_task_service.go b/internal/service/ingestion_task_service.goindex 1f77c31472..c23d355053 100644--- a/internal/service/ingestion_task_service.go+++ b/internal/service/ingestion_task_service.go@@ -185,7 +185,7 @@ func (s *IngestionTaskService) emitLifecycleEvent(ctx context.Context, task *entif logID != nil && *logID != "" && s.pipelineLogDAO != nil {run, _ = s.pipelineLogDAO.GetByID(ctx, dao.DB, *logID)}- s.eventEmitter.EmitIngestLog(ctx, observability.BuildIngestLog(task, doc, run, typ, status, errorInfo))+ s.eventEmitter.EmitIngestLog(ctx, observability.BuildIngestLog(task, doc, run, typ, status, errorInfo, "", ""))}func (s *IngestionTaskService) ListByUser(ctx context.Context, userID string, datasetID *string, page, pageSize int) ([]*entity.IngestionTask, error) {