feat: sample recent business events
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
)
|
||||
|
||||
const operationalMetricSampleLimit = 10000
|
||||
const operationalMetricEventWindow = 5 * time.Minute
|
||||
|
||||
// OperationalSnapshot 是进程观测使用的有限数据库快照。
|
||||
// Counts 只包含固定状态的有界计数;达到上限时 Truncated 为 true,不能解释为精确总量。
|
||||
@@ -26,12 +27,15 @@ type OperationalSnapshot struct {
|
||||
WorkflowOldestAgeSeconds float64
|
||||
// Truncated 表示至少一个状态达到有界采样上限。
|
||||
Truncated bool
|
||||
// RecentEvents 是固定事件类别在最近窗口内的有界数量。
|
||||
RecentEvents map[string]uint64
|
||||
}
|
||||
|
||||
var (
|
||||
operationalTaskStates = []string{"WAITING", "LEASED", "RUNNING", "NEEDS_REVIEW", "SUCCEEDED", "FAILED", "CANCELLED"}
|
||||
operationalDeliveryStates = []string{"PENDING", "LEASED", "ACKED", "EXPIRED"}
|
||||
operationalWorkflowStates = []string{"RUNNING", "WAITING", "CANCELLING", "NEEDS_REVIEW", "SUCCEEDED", "FAILED", "CANCELLED"}
|
||||
operationalEventTypes = []string{"task.accepted", "task.started", "task.succeeded", "task.failed", "task.needs_review", "task.requeued", "workflow.started", "workflow.advanced", "workflow.recovered", "workflow.blocked", "workflow.stopped"}
|
||||
)
|
||||
|
||||
// ReadOperationalSnapshot performs only bounded, indexed reads. It is intended
|
||||
@@ -68,11 +72,16 @@ func ReadOperationalSnapshot(ctx context.Context, db *sql.DB) (OperationalSnapsh
|
||||
if err != nil {
|
||||
return OperationalSnapshot{}, err
|
||||
}
|
||||
events, eventTruncated, err := boundedRecentEventCounts(ctx, db, now)
|
||||
if err != nil {
|
||||
return OperationalSnapshot{}, err
|
||||
}
|
||||
return OperationalSnapshot{
|
||||
TaskStates: tasks, TaskOldestAgeSeconds: taskAge,
|
||||
DeliveryStates: deliveries, DeliveryOldestAgeSeconds: deliveryAge,
|
||||
WorkflowStates: workflows, WorkflowOldestAgeSeconds: workflowAge,
|
||||
Truncated: taskTruncated || deliveryTruncated || workflowTruncated,
|
||||
RecentEvents: events,
|
||||
Truncated: taskTruncated || deliveryTruncated || workflowTruncated || eventTruncated,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -102,6 +111,25 @@ func boundedStateCounts(ctx context.Context, db *sql.DB, table, index string, st
|
||||
return counts, truncated, nil
|
||||
}
|
||||
|
||||
func boundedRecentEventCounts(ctx context.Context, db *sql.DB, now time.Time) (map[string]uint64, bool, error) {
|
||||
counts := make(map[string]uint64, len(operationalEventTypes))
|
||||
truncated := false
|
||||
cutoff := now.Add(-operationalMetricEventWindow)
|
||||
for _, eventType := range operationalEventTypes {
|
||||
query := "SELECT COUNT(*) FROM (SELECT id FROM orbit_events FORCE INDEX (ix_event_observe_type_occurred) WHERE type=? AND occurred_at>=? LIMIT ?) AS bounded"
|
||||
var n uint64
|
||||
if err := db.QueryRowContext(ctx, query, eventType, cutoff, operationalMetricSampleLimit+1).Scan(&n); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if n > operationalMetricSampleLimit {
|
||||
n = operationalMetricSampleLimit
|
||||
truncated = true
|
||||
}
|
||||
counts[eventType] = n
|
||||
}
|
||||
return counts, truncated, nil
|
||||
}
|
||||
|
||||
func oldestAge(ctx context.Context, db *sql.DB, table, index string, states []string, column string, now time.Time) (float64, error) {
|
||||
placeholders := "?"
|
||||
args := make([]any, 0, len(states))
|
||||
|
||||
Reference in New Issue
Block a user