Skip to content

Commit

Permalink
Merge pull request #76 from checkr/zz/add-context-logging
Browse files Browse the repository at this point in the history
Add more context logging
  • Loading branch information
zhouzhuojie authored May 13, 2020
2 parents 02f6346 + 7c34517 commit c536b8c
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 15 deletions.
32 changes: 30 additions & 2 deletions demo_templates/kafka.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,40 @@
kind: Behavior
expect:
kafka:
topic: hello_kafka_in
topic: hello_kafka_in_1
actions:
- publish_kafka:
topic: hello_kafka_out
payload: >
{
"kafka": "OK",
"data": {}
"data": 1
}
- key: test_kafka_2
kind: Behavior
expect:
kafka:
topic: hello_kafka_in_2
actions:
- publish_kafka:
topic: hello_kafka_out
payload: >
{
"kafka": "OK",
"data": 2
}
- key: test_kafka_3
kind: Behavior
expect:
kafka:
topic: hello_kafka_in_3
actions:
- publish_kafka:
topic: hello_kafka_out
payload: >
{
"kafka": "OK",
"data": 3
}
2 changes: 1 addition & 1 deletion demo_templates/payload_from_file.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- key: test_kafka_2
- key: test_kafka_payload_file
kind: Behavior
expect:
kafka:
Expand Down
18 changes: 11 additions & 7 deletions handle_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ func (ms MocksArray) DoActions(ctx Context) error {

func (m *Mock) DoActions(ctx Context) error {
ctx.Values = m.Values
ctx.currentMock = m
if !ctx.MatchCondition(m.Expect.Condition) {
return nil
}
logger := newOmLogger(ctx)
logger.Info("doing actions")
for _, actionDispatcher := range m.Actions {
actualAction := getActualAction(actionDispatcher)
if err := actualAction.Perform(ctx); err != nil {
logrus.WithFields(logrus.Fields{
logger.WithFields(logrus.Fields{
"err": err,
"action": fmt.Sprintf("%T", actualAction),
}).Errorf("failed to do action")
Expand All @@ -47,7 +50,7 @@ func (a ActionSendHTTP) Perform(ctx Context) error {
}

request := gorequest.New().
SetDebug(true).
SetLogger(newOmLogger(ctx)).
CustomMethod(a.Method, urlStr)

a.Headers, err = renderHeaders(ctx, a.Headers)
Expand Down Expand Up @@ -84,7 +87,7 @@ func (a ActionReplyHTTP) Perform(ctx Context) (err error) {

msg, err := ctx.Render(a.Body)
if err != nil {
logrus.WithField("err", err).Error("failed to render template for http body")
newOmLogger(ctx).WithField("err", err).Error("failed to render template for http body")
return err
}

Expand Down Expand Up @@ -125,23 +128,24 @@ func (a ActionSleep) Perform(ctx Context) error {
}

func (a ActionPublishKafka) Perform(ctx Context) error {
logger := newOmLogger(ctx)
msg := a.Payload
msg, err := ctx.Render(msg)
if err != nil {
logrus.WithField("err", err).Error("failed to render template for kafka payload")
logger.WithField("err", err).Error("failed to render template for kafka payload")
return err
}
err = ctx.om.kafkaClient.sendMessage(a.Topic, []byte(msg))
if err != nil {
logrus.WithField("err", err).Error("failed to publish to kafka")
logger.WithField("err", err).Error("failed to publish to kafka")
}
return err
}

func (a ActionPublishAMQP) Perform(ctx Context) error {
msg, err := ctx.Render(a.Payload)
if err != nil {
logrus.WithField("err", err).Error("failed to render template for amqp")
newOmLogger(ctx).WithField("err", err).Error("failed to render template for amqp")
return err
}
publishToAMQP(
Expand All @@ -158,7 +162,7 @@ func renderHeaders(ctx Context, headers map[string]string) (map[string]string, e
for k, v := range headers {
msg, err := ctx.Render(v)
if err != nil {
logrus.WithField("err", err).Error("failed to render template for http headers")
newOmLogger(ctx).WithField("err", err).Error("failed to render template for http headers")
return nil, err
}
ret[k] = msg
Expand Down
7 changes: 5 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ func (om *OpenMock) startHTTP() {
e.Use(em.Logrus())
e.Use(middleware.BodyDump(func(c echo.Context, reqBody, resBody []byte) {
logrus.WithFields(logrus.Fields{
"http_req": string(reqBody),
"http_res": string(resBody),
"http_path": c.Path(),
"http_method": c.Request().Method,
"http_host": c.Request().Host,
"http_req": string(reqBody),
"http_res": string(resBody),
}).Info()
}))
if om.CorsEnabled {
Expand Down
6 changes: 6 additions & 0 deletions kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ func (om *OpenMock) startKafka() {
return
}
c.KafkaPayload = string(payload)

newOmLogger(c).WithFields(logrus.Fields{
"topic": msg.Topic,
"payload": c.KafkaPayload,
}).Info("start_consuming_message")

if err := ms.DoActions(c); err != nil {
logrus.WithFields(logrus.Fields{
"err": err,
Expand Down
26 changes: 26 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package openmock

import "github.com/sirupsen/logrus"

type omLogger struct {
*logrus.Entry
ctx Context
}

func newOmLogger(ctx Context) *omLogger {
currentMock := &Mock{}
if ctx.currentMock != nil {
currentMock = ctx.currentMock
}
entry := logrus.NewEntry(logrus.StandardLogger()).WithFields(logrus.Fields{
"current_mock_key": currentMock.Key,
})
return &omLogger{
Entry: entry,
ctx: ctx,
}
}

func (l *omLogger) SetPrefix(prefix string) {
l.Entry = l.Entry.WithFields(logrus.Fields{"logger_prefix": prefix})
}
1 change: 1 addition & 0 deletions openmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (om *OpenMock) SetupLogrus() {
}
logrus.SetLevel(l)
logrus.SetOutput(os.Stdout)
logrus.SetReportCaller(true)
}

func (om *OpenMock) SetupRepo() {
Expand Down
9 changes: 6 additions & 3 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type Context struct {

Values map[string]interface{}

om *OpenMock
om *OpenMock
currentMock *Mock
}

var globalTemplate = template.New("__global__")
Expand Down Expand Up @@ -59,9 +60,11 @@ func (c Context) Render(raw string) (out string, err error) {

// MatchCondition checks the condition given the context
func (c Context) MatchCondition(condition string) (r bool) {
logger := newOmLogger(c)
defer func() {
if r {
logrus.WithFields(logrus.Fields{
logger.Info("ok match condition")
logger.WithFields(logrus.Fields{
"HTTPHeader": c.HTTPHeader,
"HTTPBody": c.HTTPBody,
"KafkaPayload": c.KafkaPayload,
Expand All @@ -78,7 +81,7 @@ func (c Context) MatchCondition(condition string) (r bool) {

result, err := c.Render(condition)
if err != nil {
logrus.WithField("err", err).Errorf("failed to render condition: %s", condition)
logger.WithField("err", err).Errorf("failed to render condition: %s", condition)
return false
}
return result == "true"
Expand Down

0 comments on commit c536b8c

Please sign in to comment.