Skip to content

Commit 80161c6

Browse files
author
Guy Baron
authored
fixed logging issues (#167)
1 parent f308b4b commit 80161c6

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

gbus/abstractions.go

+1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ type TxProvider interface {
260260

261261
//TxOutbox abstracts the transactional outgoing channel type
262262
type TxOutbox interface {
263+
Logged
263264
Save(tx *sql.Tx, exchange, routingKey string, amqpMessage amqp.Publishing) error
264265
Start(amqpOut *AMQPOutbox) error
265266
Stop() error

gbus/builder/builder.go

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func (builder *defaultBuilder) Build(svcName string) gbus.Bus {
8989
}
9090
}
9191
gb.Outbox = mysql.NewOutbox(gb.SvcName, mysqltx, builder.purgeOnStartup)
92+
gb.Outbox.SetLogger(gb.Log())
9293
timeoutManager = mysql.NewTimeoutManager(gb, gb.TxProvider, gb.Log, svcName, builder.purgeOnStartup)
9394

9495
default:
@@ -107,6 +108,7 @@ func (builder *defaultBuilder) Build(svcName string) gbus.Bus {
107108
}
108109
}
109110
glue := saga.NewGlue(gb, sagaStore, svcName, gb.TxProvider, gb.Log, timeoutManager)
111+
glue.SetLogger(gb.Log())
110112
gb.Glue = glue
111113
return gb
112114
}

gbus/saga/glue.go

+3
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ func (imsm *Glue) invokeSagaInstance(def *Def, instance *Instance, invocation gb
229229
}))
230230

231231
exchange, routingKey := invocation.Routing()
232+
instance.logger = imsm.Log()
232233
err := instance.invoke(exchange, routingKey, sginv, message)
233234
if err != nil {
234235
span.LogFields(slog.Error(err))
@@ -321,6 +322,8 @@ func NewGlue(bus gbus.Bus, sagaStore Store, svcName string, txp gbus.TxProvider,
321322
timeoutManager: timeoutManager,
322323
}
323324

325+
logged := &gbus.Glogged{}
326+
g.Glogged = logged
324327
timeoutManager.SetTimeoutFunction(g.TimeoutSaga)
325328
return g
326329
}

gbus/saga/instance.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Instance struct {
1919
ConcurrencyCtrl int
2020
UnderlyingInstance gbus.Saga
2121
MsgToMethodMap []*MsgToFuncPair
22-
Log logrus.FieldLogger
22+
logger logrus.FieldLogger
2323
/*
2424
Will hold the service name that sent the command or event that started the saga
2525
*/
@@ -36,6 +36,14 @@ type Instance struct {
3636
StartedByRPCID string
3737
}
3838

39+
func (si *Instance) log() logrus.FieldLogger {
40+
if si.logger == nil {
41+
return logrus.WithField("id", si.ID)
42+
}
43+
44+
return si.logger
45+
}
46+
3947
func (si *Instance) invoke(exchange, routingKey string, invocation *sagaInvocation, message *gbus.BusMessage) error {
4048

4149
methodsToInvoke := si.getSagaMethodNameToInvoke(exchange, routingKey, message)
@@ -55,10 +63,10 @@ func (si *Instance) invoke(exchange, routingKey string, invocation *sagaInvocati
5563
params := make([]reflect.Value, 0)
5664
params = append(params, reflect.ValueOf(invocation), valueOfMessage)
5765
method := reflectedVal.MethodByName(methodName)
58-
if invocation.Log() == nil {
66+
if si.log() == nil {
5967
panic("here")
6068
}
61-
invocation.Log().WithFields(logrus.Fields{
69+
si.log().WithFields(logrus.Fields{
6270
"method_name": methodName, "saga_id": si.ID,
6371
}).Info("invoking method on saga")
6472

@@ -78,13 +86,13 @@ func (si *Instance) invoke(exchange, routingKey string, invocation *sagaInvocati
7886
return val.Interface().(error)
7987
}
8088
return nil
81-
}, methodName, message.PayloadFQN, invocation.Log())
89+
}, methodName, message.PayloadFQN, si.log())
8290

8391
if err != nil {
8492
return err
8593
}
8694

87-
invocation.Log().WithFields(logrus.Fields{
95+
si.log().WithFields(logrus.Fields{
8896
"method_name": methodName, "saga_id": si.ID,
8997
}).Info("saga instance invoked")
9098
}

gbus/tx/mysql/txoutbox.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var (
3131

3232
//TxOutbox is a mysql based transactional outbox
3333
type TxOutbox struct {
34+
*gbus.Glogged
3435
svcName string
3536
txProv gbus.TxProvider
3637
purgeOnStartup bool
@@ -44,7 +45,7 @@ type TxOutbox struct {
4445
}
4546

4647
func (outbox *TxOutbox) log() *log.Entry {
47-
return log.WithField("tx", "mysql")
48+
return outbox.Log().WithField("tx", "mysql")
4849
}
4950

5051
//Start starts the transactional outbox that is used to send messages in sync with domain object change
@@ -128,6 +129,7 @@ func NewOutbox(svcName string, txProv gbus.TxProvider, purgeOnStartup bool) *TxO
128129
ack: make(chan uint64, 1000000),
129130
nack: make(chan uint64, 1000000),
130131
exit: make(chan bool)}
132+
txo.Glogged = &gbus.Glogged{}
131133
return txo
132134
}
133135

0 commit comments

Comments
 (0)