1- package events
1+ package capture
22
33import (
44 "sync"
55 "time"
6+
7+ "github.com/kernel/kernel-images/server/lib/events"
68)
79
810// CaptureConfig holds caller-supplied capture preferences. All fields are
911// optional; zero values mean "use server defaults" (all categories).
1012type CaptureConfig struct {
1113 // Categories limits which event categories are captured.
1214 // nil or empty includes all categories.
13- Categories []EventCategory
15+ Categories []events. EventCategory
1416}
1517
1618// CaptureSession manages a capture session against a shared EventStream.
1719// It is responsible for (a) category-filtering Publish calls and (b) tracking
1820// session-scoped metadata (ID, config, timestamps).
1921type CaptureSession struct {
20- es * EventStream
22+ es * events. EventStream
2123 mu sync.Mutex
2224 captureSessionID string
2325 sessionStartSeq uint64
24- categories map [EventCategory ]struct {}
26+ categories map [events. EventCategory ]struct {}
2527 createdAt time.Time
2628}
2729
28- func NewCaptureSession (es * EventStream ) * CaptureSession {
29- cats := make (map [EventCategory ]struct {}, len (allCategories ))
30- for _ , c := range allCategories {
30+ func NewCaptureSession (es * events. EventStream ) * CaptureSession {
31+ cats := make (map [events. EventCategory ]struct {}, len (events . AllCategories () ))
32+ for _ , c := range events . AllCategories () {
3133 cats [c ] = struct {}{}
3234 }
3335 return & CaptureSession {es : es , categories : cats }
@@ -36,8 +38,8 @@ func NewCaptureSession(es *EventStream) *CaptureSession {
3638// Start begins a new capture session with the given ID and config. Sequence
3739// numbers are process-monotonic and do not reset between sessions; a
3840// Last-Event-ID from any previous session is valid for resuming the SSE stream.
39- // Events from different sessions are interleaved in the same per-category JSONL
40- // files and distinguished by their envelope's captureSessionID .
41+ // Events from different sessions are distinguished by their envelope's
42+ // CaptureSessionID .
4143func (s * CaptureSession ) Start (captureSessionID string , cfg CaptureConfig ) {
4244 s .mu .Lock ()
4345 defer s .mu .Unlock ()
@@ -46,28 +48,28 @@ func (s *CaptureSession) Start(captureSessionID string, cfg CaptureConfig) {
4648 s .createdAt = time .Now ()
4749 cats := cfg .Categories
4850 if len (cats ) == 0 {
49- cats = allCategories
51+ cats = events . AllCategories ()
5052 }
51- s .categories = make (map [EventCategory ]struct {}, len (cats ))
53+ s .categories = make (map [events. EventCategory ]struct {}, len (cats ))
5254 for _ , c := range cats {
5355 s .categories [c ] = struct {}{}
5456 }
5557}
5658
5759// publishLocked builds an envelope and forwards it to the EventStream.
5860// Requires s.mu to be held.
59- func (s * CaptureSession ) publishLocked (ev Event ) Envelope {
61+ func (s * CaptureSession ) publishLocked (ev events. Event ) events. Envelope {
6062 if ev .Ts == 0 {
6163 ev .Ts = time .Now ().UnixMicro ()
6264 }
63- return s .es .publish ( Envelope {
65+ return s .es .Publish (events. Envelope {
6466 CaptureSessionID : s .captureSessionID ,
6567 Event : ev ,
6668 })
6769}
6870
6971// Publish applies the category filter then forwards ev to the EventStream.
70- func (s * CaptureSession ) Publish (ev Event ) {
72+ func (s * CaptureSession ) Publish (ev events. Event ) {
7173 s .mu .Lock ()
7274 defer s .mu .Unlock ()
7375 if s .captureSessionID == "" {
@@ -80,18 +82,18 @@ func (s *CaptureSession) Publish(ev Event) {
8082}
8183
8284// PublishUnfiltered forwards ev to the EventStream without applying the category
83- // filter. Returns the assigned Envelope, or a zero Envelope if no session is active .
84- func (s * CaptureSession ) PublishUnfiltered (ev Event ) Envelope {
85+ // filter. Returns the assigned Envelope.
86+ func (s * CaptureSession ) PublishUnfiltered (ev events. Event ) events. Envelope {
8587 s .mu .Lock ()
8688 defer s .mu .Unlock ()
8789 if s .captureSessionID == "" {
88- return Envelope {}
90+ return events. Envelope {}
8991 }
9092 return s .publishLocked (ev )
9193}
9294
9395// NewReader returns a Reader from the EventStream positioned after afterSeq.
94- func (s * CaptureSession ) NewReader (afterSeq uint64 ) * Reader {
96+ func (s * CaptureSession ) NewReader (afterSeq uint64 ) * events. Reader {
9597 return s .es .NewReader (afterSeq )
9698}
9799
@@ -102,7 +104,6 @@ func (s *CaptureSession) ID() string {
102104 return s .captureSessionID
103105}
104106
105- // Seq returns the sequence number of the last published event.
106107func (s * CaptureSession ) Seq () uint64 {
107108 return s .es .Seq ()
108109}
@@ -115,39 +116,35 @@ func (s *CaptureSession) SessionStartSeq() uint64 {
115116 return s .sessionStartSeq
116117}
117118
118- // Config returns the current capture configuration.
119119func (s * CaptureSession ) Config () CaptureConfig {
120120 s .mu .Lock ()
121121 defer s .mu .Unlock ()
122- cats := make ([]EventCategory , 0 , len (s .categories ))
122+ cats := make ([]events. EventCategory , 0 , len (s .categories ))
123123 for c := range s .categories {
124124 cats = append (cats , c )
125125 }
126126 return CaptureConfig {Categories : cats }
127127}
128128
129- // CreatedAt returns when the current session was started.
130129func (s * CaptureSession ) CreatedAt () time.Time {
131130 s .mu .Lock ()
132131 defer s .mu .Unlock ()
133132 return s .createdAt
134133}
135134
136- // UpdateConfig applies a new CaptureConfig to the running session.
137135func (s * CaptureSession ) UpdateConfig (cfg CaptureConfig ) {
138136 s .mu .Lock ()
139137 defer s .mu .Unlock ()
140138 cats := cfg .Categories
141139 if len (cats ) == 0 {
142- cats = allCategories
140+ cats = events . AllCategories ()
143141 }
144- s .categories = make (map [EventCategory ]struct {}, len (cats ))
142+ s .categories = make (map [events. EventCategory ]struct {}, len (cats ))
145143 for _ , c := range cats {
146144 s .categories [c ] = struct {}{}
147145 }
148146}
149147
150- // Active reports whether a capture session is currently running.
151148func (s * CaptureSession ) Active () bool {
152149 s .mu .Lock ()
153150 defer s .mu .Unlock ()
@@ -163,11 +160,10 @@ func (s *CaptureSession) Stop() {
163160 if s .captureSessionID == "" {
164161 return
165162 }
166- s .publishLocked (Event {
167- Type : TypeSessionEnded ,
168- Category : CategorySystem ,
169- Source : Source {Kind : KindKernelAPI },
163+ s .publishLocked (events. Event {
164+ Type : events . TypeSessionEnded ,
165+ Category : events . CategorySystem ,
166+ Source : events. Source {Kind : events . KindKernelAPI },
170167 })
171168 s .captureSessionID = ""
172169}
173-
0 commit comments