Skip to content

Commit

Permalink
Runtime: use instance logger for connectors (#3862)
Browse files Browse the repository at this point in the history
  • Loading branch information
begelundmuller authored Jan 17, 2024
1 parent e36c8ac commit e6fdae2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
9 changes: 5 additions & 4 deletions runtime/connection_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,18 @@ func (r *Runtime) evictInstanceConnections(instanceID string) {
// openAndMigrate opens a connection and migrates it.
func (r *Runtime) openAndMigrate(ctx context.Context, cfg cachedConnectionConfig) (drivers.Handle, error) {
logger := r.logger
if cfg.instanceID != "default" {
logger = r.logger.With(zap.String("instance_id", cfg.instanceID), zap.String("driver", cfg.driver))
}

activityClient := r.activity
if !cfg.shared {
inst, err := r.Instance(ctx, cfg.instanceID)
if err != nil {
return nil, err
}

logger, err = r.InstanceLogger(ctx, cfg.instanceID)
if err != nil {
return nil, err
}

activityDims := instanceAnnotationsToAttribs(inst)
if activityClient != nil {
activityClient = activityClient.With(activityDims...)
Expand Down
25 changes: 21 additions & 4 deletions runtime/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ func (r *Runtime) Instance(ctx context.Context, instanceID string) (*drivers.Ins
return r.registryCache.get(instanceID)
}

// InstanceLogger returns a logger scoped for the given instance. Logs emitted to the logger will also be available in the instance's log buffer.
func (r *Runtime) InstanceLogger(ctx context.Context, instanceID string) (*zap.Logger, error) {
return r.registryCache.getLogger(instanceID)
}

// InstanceLogs returns an in-memory buffer of recent logs related to the given instance.
func (r *Runtime) InstanceLogs(ctx context.Context, instanceID string) (*logbuffer.Buffer, error) {
return r.registryCache.getLogbuffer(instanceID)
}

// Controller returns the controller for the given instance.
// If the controller is currently initializing, the call will wait until the controller is ready.
// If the controller has closed with a fatal error, that error will be returned here until it's restarted.
Expand Down Expand Up @@ -110,10 +120,6 @@ func (r *Runtime) DeleteInstance(ctx context.Context, instanceID string, dropDB
return nil
}

func (r *Runtime) InstanceLogs(ctx context.Context, instanceID string) (*logbuffer.Buffer, error) {
return r.registryCache.getLogbuffer(instanceID)
}

// registryCache caches all the runtime's instances and manages the life-cycle of their controllers.
// It ensures that a controller is started for every instance, and that a controller is completely stopped before getting restarted when edited.
type registryCache struct {
Expand Down Expand Up @@ -252,6 +258,17 @@ func (r *registryCache) getController(ctx context.Context, instanceID string) (*
return iwc.controller, nil
}

func (r *registryCache) getLogger(instanceID string) (*zap.Logger, error) {
r.mu.RLock()
defer r.mu.RUnlock()

iwc := r.instances[instanceID]
if iwc == nil {
return nil, drivers.ErrNotFound
}
return iwc.logger, nil
}

func (r *registryCache) getLogbuffer(instanceID string) (*logbuffer.Buffer, error) {
r.mu.RLock()
defer r.mu.RUnlock()
Expand Down

0 comments on commit e6fdae2

Please sign in to comment.