Skip to content

Commit

Permalink
Utilize actions for built in logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
fr33r committed Jul 26, 2020
1 parent 2cbbd04 commit 4113c84
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 20 deletions.
17 changes: 3 additions & 14 deletions v3/sql_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ func NewSQLUnit(

// set defaults.
o := UnitOptions{
Logger: zap.NewNop(),
Scope: tally.NoopScope,
Logger: zap.NewNop(),
Scope: tally.NoopScope,
Actions: make(map[UnitActionType][]UnitAction),
}

// apply options.
Expand Down Expand Up @@ -123,7 +124,6 @@ func (u *sqlUnit) rollback(tx *sql.Tx) (err error) {
}

func (u *sqlUnit) applyInserts(tx *sql.Tx) (err error) {
u.logger.Debug("attempting to insert entities", zap.Int("count", u.additionCount))
for typeName, additions := range u.additions {
if err = u.mappers[typeName].Insert(tx, additions...); err != nil {
u.executeActions(UnitActionTypeBeforeRollback)
Expand All @@ -140,7 +140,6 @@ func (u *sqlUnit) applyInserts(tx *sql.Tx) (err error) {
}

func (u *sqlUnit) applyUpdates(tx *sql.Tx) (err error) {
u.logger.Debug("attempting to update entities", zap.Int("count", u.alterationCount))
for typeName, alterations := range u.alterations {
if err = u.mappers[typeName].Update(tx, alterations...); err != nil {
u.executeActions(UnitActionTypeBeforeRollback)
Expand All @@ -157,7 +156,6 @@ func (u *sqlUnit) applyUpdates(tx *sql.Tx) (err error) {
}

func (u *sqlUnit) applyDeletes(tx *sql.Tx) (err error) {
u.logger.Debug("attempting to remove entities", zap.Int("count", u.removalCount))
for typeName, removals := range u.removals {
if err = u.mappers[typeName].Delete(tx, removals...); err != nil {
u.executeActions(UnitActionTypeBeforeRollback)
Expand Down Expand Up @@ -241,14 +239,5 @@ func (u *sqlUnit) Save() (err error) {
u.logger.Error(err.Error())
return
}

totalCount :=
u.additionCount + u.alterationCount + u.removalCount + u.registerCount
u.logger.Info("successfully saved unit",
zap.Int("insertCount", u.additionCount),
zap.Int("updateCount", u.alterationCount),
zap.Int("deleteCount", u.removalCount),
zap.Int("registerCount", u.registerCount),
zap.Int("totalCount", totalCount))
return
}
13 changes: 12 additions & 1 deletion v3/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ type unit struct {
}

func newUnit(options UnitOptions) unit {
if !options.DisableDefaultLoggingActions {
UnitDefaultLoggingActions()(&options)
}

u := unit{
additions: make(map[TypeName][]interface{}),
alterations: make(map[TypeName][]interface{}),
Expand Down Expand Up @@ -170,6 +174,13 @@ func (u *unit) remove(checker func(t TypeName) bool, entities ...interface{}) er

func (u *unit) executeActions(actionType UnitActionType) {
for _, action := range u.actions[actionType] {
action(UnitContext{logger: u.logger})
action(UnitContext{
Logger: u.logger,
Scope: u.scope,
AdditionCount: u.additionCount,
AlterationCount: u.alterationCount,
RemovalCount: u.removalCount,
RegisterCount: u.registerCount,
})
}
}
18 changes: 16 additions & 2 deletions v3/unit_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,23 @@

package work

import "go.uber.org/zap"
import (
"github.com/uber-go/tally"
"go.uber.org/zap"
)

// UnitContext represents context about a work unit passed through to work unit actions.
type UnitContext struct {
logger *zap.Logger
// Logger is the work units configured logger.
Logger *zap.Logger
// Scope is the work units configured metrics scope.
Scope tally.Scope
// AdditionCount represents the number of entities indicated as new.
AdditionCount int
// AlterationCount represents the number of entities indicated as modified.
AlterationCount int
// RemovalCount represents the number of entities indicated as removed.
RemovalCount int
// RegisterCount represents the number of entities indicated as registered.
RegisterCount int
}
78 changes: 75 additions & 3 deletions v3/unit_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
// UnitOptions represents the configuration options
// for the work unit.
type UnitOptions struct {
Logger *zap.Logger
Scope tally.Scope
Actions map[UnitActionType][]UnitAction
Logger *zap.Logger
Scope tally.Scope
Actions map[UnitActionType][]UnitAction
DisableDefaultLoggingActions bool
}

// Option applies an option to the provided configuration.
Expand Down Expand Up @@ -138,4 +139,75 @@ var (
UnitBeforeSaveActions = func(a ...UnitAction) Option {
return setActions(UnitActionTypeBeforeSave, a...)
}

// UnitDefaultLoggingActions specifies all of the default logging actions.
UnitDefaultLoggingActions = func() Option {
beforeInsertLogAction := func(ctx UnitContext) {
ctx.Logger.Debug(
"attempting to insert entities",
zap.Int("count", ctx.AdditionCount),
)
}
afterInsertLogAction := func(ctx UnitContext) {
ctx.Logger.Debug(
"successfully inserted entities",
zap.Int("count", ctx.AdditionCount),
)
}
beforeUpdateLogAction := func(ctx UnitContext) {
ctx.Logger.Debug(
"attempting to update entities",
zap.Int("count", ctx.AlterationCount),
)
}
afterUpdateLogAction := func(ctx UnitContext) {
ctx.Logger.Debug(
"successfully updated entities",
zap.Int("count", ctx.AlterationCount),
)
}
beforeDeleteLogAction := func(ctx UnitContext) {
ctx.Logger.Debug(
"attempting to delete entities",
zap.Int("count", ctx.RemovalCount),
)
}
afterDeleteLogAction := func(ctx UnitContext) {
ctx.Logger.Debug(
"successfully deleted entities",
zap.Int("count", ctx.RemovalCount),
)
}
afterSaveLogAction := func(ctx UnitContext) {
totalCount :=
ctx.AdditionCount + ctx.AlterationCount + ctx.RemovalCount
ctx.Logger.Info("successfully saved unit",
zap.Int("insertCount", ctx.AdditionCount),
zap.Int("updateCount", ctx.AlterationCount),
zap.Int("deleteCount", ctx.RemovalCount),
zap.Int("registerCount", ctx.RegisterCount),
zap.Int("totalUpdateCount", totalCount))
}
return func(o *UnitOptions) {
subOpts := []Option{
setActions(UnitActionTypeBeforeInserts, beforeInsertLogAction),
setActions(UnitActionTypeBeforeInserts, afterInsertLogAction),
setActions(UnitActionTypeBeforeUpdates, beforeUpdateLogAction),
setActions(UnitActionTypeBeforeUpdates, afterUpdateLogAction),
setActions(UnitActionTypeBeforeDeletes, beforeDeleteLogAction),
setActions(UnitActionTypeBeforeDeletes, afterDeleteLogAction),
setActions(UnitActionTypeAfterSave, afterSaveLogAction),
}
for _, opt := range subOpts {
opt(o)
}
}
}

// DisableDefaultLoggingActions disables the default logging actions.
DisableDefaultLoggingActions = func() Option {
return func(o *UnitOptions) {
o.DisableDefaultLoggingActions = true
}
}
)

0 comments on commit 4113c84

Please sign in to comment.