diff --git a/demo_templates/kafka.yaml b/demo_templates/kafka.yaml index dfaa95ae..1a3d5bcd 100644 --- a/demo_templates/kafka.yaml +++ b/demo_templates/kafka.yaml @@ -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 } diff --git a/demo_templates/payload_from_file.yaml b/demo_templates/payload_from_file.yaml index 11cd837c..f0b8ae02 100644 --- a/demo_templates/payload_from_file.yaml +++ b/demo_templates/payload_from_file.yaml @@ -1,4 +1,4 @@ -- key: test_kafka_2 +- key: test_kafka_payload_file kind: Behavior expect: kafka: diff --git a/handle_actions.go b/handle_actions.go index 6906a8c1..227e86c8 100644 --- a/handle_actions.go +++ b/handle_actions.go @@ -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") @@ -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) @@ -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 } @@ -125,15 +128,16 @@ 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 } @@ -141,7 +145,7 @@ func (a ActionPublishKafka) Perform(ctx Context) error { 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( @@ -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 diff --git a/http.go b/http.go index 4d03b626..46748d11 100644 --- a/http.go +++ b/http.go @@ -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 { diff --git a/kafka.go b/kafka.go index dbf5a767..e66fb8fb 100644 --- a/kafka.go +++ b/kafka.go @@ -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, diff --git a/logger.go b/logger.go new file mode 100644 index 00000000..f53c29fd --- /dev/null +++ b/logger.go @@ -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}) +} diff --git a/openmock.go b/openmock.go index df9784db..a45fdff8 100644 --- a/openmock.go +++ b/openmock.go @@ -63,6 +63,7 @@ func (om *OpenMock) SetupLogrus() { } logrus.SetLevel(l) logrus.SetOutput(os.Stdout) + logrus.SetReportCaller(true) } func (om *OpenMock) SetupRepo() { diff --git a/template.go b/template.go index 88ed5ff0..7a0651ab 100644 --- a/template.go +++ b/template.go @@ -29,7 +29,8 @@ type Context struct { Values map[string]interface{} - om *OpenMock + om *OpenMock + currentMock *Mock } var globalTemplate = template.New("__global__") @@ -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, @@ -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"