Skip to content

Commit da24738

Browse files
committed
core/event: add context.Context to events
1 parent 3e6d365 commit da24738

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

core/connection/connection.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,13 @@ func canMonitor(cmd string) bool {
358358
return true
359359
}
360360

361-
func (c *connection) commandStartedEvent(wm wiremessage.WireMessage) error {
361+
func (c *connection) commandStartedEvent(ctx context.Context, wm wiremessage.WireMessage) error {
362362
if c.cmdMonitor == nil || c.cmdMonitor.Started == nil {
363363
return nil
364364
}
365365

366366
startedEvent := &event.CommandStartedEvent{
367+
Context: ctx,
367368
ConnectionID: c.id,
368369
}
369370

@@ -426,6 +427,7 @@ func (c *connection) commandStartedEvent(wm wiremessage.WireMessage) error {
426427

427428
// unack writes must provide a CommandSucceededEvent with an { ok: 1 } reply
428429
finishedEvent := event.CommandFinishedEvent{
430+
Context: ctx,
429431
DurationNanos: 0,
430432
CommandName: startedEvent.CommandName,
431433
RequestID: startedEvent.RequestID,
@@ -489,7 +491,7 @@ func processReply(reply *bson.Document) (bool, string) {
489491
return false, fullErrMsg
490492
}
491493

492-
func (c *connection) commandFinishedEvent(wm wiremessage.WireMessage) error {
494+
func (c *connection) commandFinishedEvent(ctx context.Context, wm wiremessage.WireMessage) error {
493495
if c.cmdMonitor == nil {
494496
return nil
495497
}
@@ -520,6 +522,7 @@ func (c *connection) commandFinishedEvent(wm wiremessage.WireMessage) error {
520522
}
521523

522524
finishedEvent := event.CommandFinishedEvent{
525+
Context: ctx,
523526
DurationNanos: cmdMetadata.TimeDifference(),
524527
CommandName: cmdMetadata.Name,
525528
RequestID: requestID,
@@ -639,7 +642,7 @@ func (c *connection) WriteWireMessage(ctx context.Context, wm wiremessage.WireMe
639642
}
640643

641644
c.bumpIdleDeadline()
642-
err = c.commandStartedEvent(wm)
645+
err = c.commandStartedEvent(ctx, wm)
643646
if err != nil {
644647
return err
645648
}
@@ -790,7 +793,7 @@ func (c *connection) ReadWireMessage(ctx context.Context) (wiremessage.WireMessa
790793
}
791794

792795
c.bumpIdleDeadline()
793-
err = c.commandFinishedEvent(wm)
796+
err = c.commandFinishedEvent(ctx, wm)
794797
if err != nil {
795798
return nil, err // TODO: do we care if monitoring fails?
796799
}

core/event/monitoring.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package event
22

33
import (
4+
"context"
45
"time"
56

67
"github.com/mongodb/mongo-go-driver/bson"
@@ -29,6 +30,7 @@ func (cm *CommandMetadata) TimeDifference() int64 {
2930

3031
// CommandStartedEvent represents an event generated when a command is sent to a server.
3132
type CommandStartedEvent struct {
33+
Context context.Context
3234
Command *bson.Document
3335
DatabaseName string
3436
CommandName string
@@ -38,6 +40,7 @@ type CommandStartedEvent struct {
3840

3941
// CommandFinishedEvent represents a generic command finishing.
4042
type CommandFinishedEvent struct {
43+
Context context.Context
4144
DurationNanos int64
4245
CommandName string
4346
RequestID int64

0 commit comments

Comments
 (0)