@@ -17,6 +17,40 @@ type MongoDBReader struct {
1717 collection * mongo.Collection
1818}
1919
20+ type mongoLogRow struct {
21+ ID string `bson:"_id"`
22+ Timestamp time.Time `bson:"timestamp"`
23+ DurationNs int64 `bson:"duration_ns"`
24+ Model string `bson:"model"`
25+ Provider string `bson:"provider"`
26+ StatusCode int `bson:"status_code"`
27+ RequestID string `bson:"request_id"`
28+ ClientIP string `bson:"client_ip"`
29+ Method string `bson:"method"`
30+ Path string `bson:"path"`
31+ Stream bool `bson:"stream"`
32+ ErrorType string `bson:"error_type"`
33+ Data * LogData `bson:"data"`
34+ }
35+
36+ func (r mongoLogRow ) toLogEntry () * LogEntry {
37+ return & LogEntry {
38+ ID : r .ID ,
39+ Timestamp : r .Timestamp ,
40+ DurationNs : r .DurationNs ,
41+ Model : r .Model ,
42+ Provider : r .Provider ,
43+ StatusCode : r .StatusCode ,
44+ RequestID : r .RequestID ,
45+ ClientIP : r .ClientIP ,
46+ Method : r .Method ,
47+ Path : r .Path ,
48+ Stream : r .Stream ,
49+ ErrorType : r .ErrorType ,
50+ Data : r .Data ,
51+ }
52+ }
53+
2054// NewMongoDBReader creates a new MongoDB audit log reader.
2155func NewMongoDBReader (database * mongo.Database ) (* MongoDBReader , error ) {
2256 if database == nil {
@@ -180,21 +214,7 @@ func (r *MongoDBReader) GetLogs(ctx context.Context, params LogQueryParams) (*Lo
180214
181215// GetLogByID returns a single audit log entry by ID.
182216func (r * MongoDBReader ) GetLogByID (ctx context.Context , id string ) (* LogEntry , error ) {
183- var row struct {
184- ID string `bson:"_id"`
185- Timestamp time.Time `bson:"timestamp"`
186- DurationNs int64 `bson:"duration_ns"`
187- Model string `bson:"model"`
188- Provider string `bson:"provider"`
189- StatusCode int `bson:"status_code"`
190- RequestID string `bson:"request_id"`
191- ClientIP string `bson:"client_ip"`
192- Method string `bson:"method"`
193- Path string `bson:"path"`
194- Stream bool `bson:"stream"`
195- ErrorType string `bson:"error_type"`
196- Data * LogData `bson:"data"`
197- }
217+ var row mongoLogRow
198218
199219 err := r .collection .FindOne (ctx , bson.D {{Key : "_id" , Value : id }}).Decode (& row )
200220 if err != nil {
@@ -204,21 +224,7 @@ func (r *MongoDBReader) GetLogByID(ctx context.Context, id string) (*LogEntry, e
204224 return nil , fmt .Errorf ("failed to query audit log by id: %w" , err )
205225 }
206226
207- return & LogEntry {
208- ID : row .ID ,
209- Timestamp : row .Timestamp ,
210- DurationNs : row .DurationNs ,
211- Model : row .Model ,
212- Provider : row .Provider ,
213- StatusCode : row .StatusCode ,
214- RequestID : row .RequestID ,
215- ClientIP : row .ClientIP ,
216- Method : row .Method ,
217- Path : row .Path ,
218- Stream : row .Stream ,
219- ErrorType : row .ErrorType ,
220- Data : row .Data ,
221- }, nil
227+ return row .toLogEntry (), nil
222228}
223229
224230// GetConversation returns a linear conversation thread around a seed log entry.
@@ -318,21 +324,7 @@ func (r *MongoDBReader) findByResponseID(ctx context.Context, responseID string)
318324 filter := bson.D {{Key : "data.response_body.id" , Value : responseID }}
319325 opts := options .FindOne ().SetSort (bson.D {{Key : "timestamp" , Value : 1 }})
320326
321- var row struct {
322- ID string `bson:"_id"`
323- Timestamp time.Time `bson:"timestamp"`
324- DurationNs int64 `bson:"duration_ns"`
325- Model string `bson:"model"`
326- Provider string `bson:"provider"`
327- StatusCode int `bson:"status_code"`
328- RequestID string `bson:"request_id"`
329- ClientIP string `bson:"client_ip"`
330- Method string `bson:"method"`
331- Path string `bson:"path"`
332- Stream bool `bson:"stream"`
333- ErrorType string `bson:"error_type"`
334- Data * LogData `bson:"data"`
335- }
327+ var row mongoLogRow
336328
337329 if err := r .collection .FindOne (ctx , filter , opts ).Decode (& row ); err != nil {
338330 if err == mongo .ErrNoDocuments {
@@ -341,42 +333,14 @@ func (r *MongoDBReader) findByResponseID(ctx context.Context, responseID string)
341333 return nil , fmt .Errorf ("failed to query audit log by response id: %w" , err )
342334 }
343335
344- return & LogEntry {
345- ID : row .ID ,
346- Timestamp : row .Timestamp ,
347- DurationNs : row .DurationNs ,
348- Model : row .Model ,
349- Provider : row .Provider ,
350- StatusCode : row .StatusCode ,
351- RequestID : row .RequestID ,
352- ClientIP : row .ClientIP ,
353- Method : row .Method ,
354- Path : row .Path ,
355- Stream : row .Stream ,
356- ErrorType : row .ErrorType ,
357- Data : row .Data ,
358- }, nil
336+ return row .toLogEntry (), nil
359337}
360338
361339func (r * MongoDBReader ) findByPreviousResponseID (ctx context.Context , previousResponseID string ) (* LogEntry , error ) {
362340 filter := bson.D {{Key : "data.request_body.previous_response_id" , Value : previousResponseID }}
363341 opts := options .FindOne ().SetSort (bson.D {{Key : "timestamp" , Value : 1 }})
364342
365- var row struct {
366- ID string `bson:"_id"`
367- Timestamp time.Time `bson:"timestamp"`
368- DurationNs int64 `bson:"duration_ns"`
369- Model string `bson:"model"`
370- Provider string `bson:"provider"`
371- StatusCode int `bson:"status_code"`
372- RequestID string `bson:"request_id"`
373- ClientIP string `bson:"client_ip"`
374- Method string `bson:"method"`
375- Path string `bson:"path"`
376- Stream bool `bson:"stream"`
377- ErrorType string `bson:"error_type"`
378- Data * LogData `bson:"data"`
379- }
343+ var row mongoLogRow
380344
381345 if err := r .collection .FindOne (ctx , filter , opts ).Decode (& row ); err != nil {
382346 if err == mongo .ErrNoDocuments {
@@ -385,19 +349,5 @@ func (r *MongoDBReader) findByPreviousResponseID(ctx context.Context, previousRe
385349 return nil , fmt .Errorf ("failed to query audit log by previous_response_id: %w" , err )
386350 }
387351
388- return & LogEntry {
389- ID : row .ID ,
390- Timestamp : row .Timestamp ,
391- DurationNs : row .DurationNs ,
392- Model : row .Model ,
393- Provider : row .Provider ,
394- StatusCode : row .StatusCode ,
395- RequestID : row .RequestID ,
396- ClientIP : row .ClientIP ,
397- Method : row .Method ,
398- Path : row .Path ,
399- Stream : row .Stream ,
400- ErrorType : row .ErrorType ,
401- Data : row .Data ,
402- }, nil
352+ return row .toLogEntry (), nil
403353}
0 commit comments