Skip to content

Commit c536b8c

Browse files
authored
Merge pull request #76 from checkr/zz/add-context-logging
Add more context logging
2 parents 02f6346 + 7c34517 commit c536b8c

File tree

8 files changed

+86
-15
lines changed

8 files changed

+86
-15
lines changed

demo_templates/kafka.yaml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,40 @@
22
kind: Behavior
33
expect:
44
kafka:
5-
topic: hello_kafka_in
5+
topic: hello_kafka_in_1
66
actions:
77
- publish_kafka:
88
topic: hello_kafka_out
99
payload: >
1010
{
1111
"kafka": "OK",
12-
"data": {}
12+
"data": 1
13+
}
14+
15+
- key: test_kafka_2
16+
kind: Behavior
17+
expect:
18+
kafka:
19+
topic: hello_kafka_in_2
20+
actions:
21+
- publish_kafka:
22+
topic: hello_kafka_out
23+
payload: >
24+
{
25+
"kafka": "OK",
26+
"data": 2
27+
}
28+
29+
- key: test_kafka_3
30+
kind: Behavior
31+
expect:
32+
kafka:
33+
topic: hello_kafka_in_3
34+
actions:
35+
- publish_kafka:
36+
topic: hello_kafka_out
37+
payload: >
38+
{
39+
"kafka": "OK",
40+
"data": 3
1341
}

demo_templates/payload_from_file.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- key: test_kafka_2
1+
- key: test_kafka_payload_file
22
kind: Behavior
33
expect:
44
kafka:

handle_actions.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ func (ms MocksArray) DoActions(ctx Context) error {
2020

2121
func (m *Mock) DoActions(ctx Context) error {
2222
ctx.Values = m.Values
23+
ctx.currentMock = m
2324
if !ctx.MatchCondition(m.Expect.Condition) {
2425
return nil
2526
}
27+
logger := newOmLogger(ctx)
28+
logger.Info("doing actions")
2629
for _, actionDispatcher := range m.Actions {
2730
actualAction := getActualAction(actionDispatcher)
2831
if err := actualAction.Perform(ctx); err != nil {
29-
logrus.WithFields(logrus.Fields{
32+
logger.WithFields(logrus.Fields{
3033
"err": err,
3134
"action": fmt.Sprintf("%T", actualAction),
3235
}).Errorf("failed to do action")
@@ -47,7 +50,7 @@ func (a ActionSendHTTP) Perform(ctx Context) error {
4750
}
4851

4952
request := gorequest.New().
50-
SetDebug(true).
53+
SetLogger(newOmLogger(ctx)).
5154
CustomMethod(a.Method, urlStr)
5255

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

8588
msg, err := ctx.Render(a.Body)
8689
if err != nil {
87-
logrus.WithField("err", err).Error("failed to render template for http body")
90+
newOmLogger(ctx).WithField("err", err).Error("failed to render template for http body")
8891
return err
8992
}
9093

@@ -125,23 +128,24 @@ func (a ActionSleep) Perform(ctx Context) error {
125128
}
126129

127130
func (a ActionPublishKafka) Perform(ctx Context) error {
131+
logger := newOmLogger(ctx)
128132
msg := a.Payload
129133
msg, err := ctx.Render(msg)
130134
if err != nil {
131-
logrus.WithField("err", err).Error("failed to render template for kafka payload")
135+
logger.WithField("err", err).Error("failed to render template for kafka payload")
132136
return err
133137
}
134138
err = ctx.om.kafkaClient.sendMessage(a.Topic, []byte(msg))
135139
if err != nil {
136-
logrus.WithField("err", err).Error("failed to publish to kafka")
140+
logger.WithField("err", err).Error("failed to publish to kafka")
137141
}
138142
return err
139143
}
140144

141145
func (a ActionPublishAMQP) Perform(ctx Context) error {
142146
msg, err := ctx.Render(a.Payload)
143147
if err != nil {
144-
logrus.WithField("err", err).Error("failed to render template for amqp")
148+
newOmLogger(ctx).WithField("err", err).Error("failed to render template for amqp")
145149
return err
146150
}
147151
publishToAMQP(
@@ -158,7 +162,7 @@ func renderHeaders(ctx Context, headers map[string]string) (map[string]string, e
158162
for k, v := range headers {
159163
msg, err := ctx.Render(v)
160164
if err != nil {
161-
logrus.WithField("err", err).Error("failed to render template for http headers")
165+
newOmLogger(ctx).WithField("err", err).Error("failed to render template for http headers")
162166
return nil, err
163167
}
164168
ret[k] = msg

http.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ func (om *OpenMock) startHTTP() {
1616
e.Use(em.Logrus())
1717
e.Use(middleware.BodyDump(func(c echo.Context, reqBody, resBody []byte) {
1818
logrus.WithFields(logrus.Fields{
19-
"http_req": string(reqBody),
20-
"http_res": string(resBody),
19+
"http_path": c.Path(),
20+
"http_method": c.Request().Method,
21+
"http_host": c.Request().Host,
22+
"http_req": string(reqBody),
23+
"http_res": string(resBody),
2124
}).Info()
2225
}))
2326
if om.CorsEnabled {

kafka.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ func (om *OpenMock) startKafka() {
134134
return
135135
}
136136
c.KafkaPayload = string(payload)
137+
138+
newOmLogger(c).WithFields(logrus.Fields{
139+
"topic": msg.Topic,
140+
"payload": c.KafkaPayload,
141+
}).Info("start_consuming_message")
142+
137143
if err := ms.DoActions(c); err != nil {
138144
logrus.WithFields(logrus.Fields{
139145
"err": err,

logger.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package openmock
2+
3+
import "github.com/sirupsen/logrus"
4+
5+
type omLogger struct {
6+
*logrus.Entry
7+
ctx Context
8+
}
9+
10+
func newOmLogger(ctx Context) *omLogger {
11+
currentMock := &Mock{}
12+
if ctx.currentMock != nil {
13+
currentMock = ctx.currentMock
14+
}
15+
entry := logrus.NewEntry(logrus.StandardLogger()).WithFields(logrus.Fields{
16+
"current_mock_key": currentMock.Key,
17+
})
18+
return &omLogger{
19+
Entry: entry,
20+
ctx: ctx,
21+
}
22+
}
23+
24+
func (l *omLogger) SetPrefix(prefix string) {
25+
l.Entry = l.Entry.WithFields(logrus.Fields{"logger_prefix": prefix})
26+
}

openmock.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func (om *OpenMock) SetupLogrus() {
6363
}
6464
logrus.SetLevel(l)
6565
logrus.SetOutput(os.Stdout)
66+
logrus.SetReportCaller(true)
6667
}
6768

6869
func (om *OpenMock) SetupRepo() {

template.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ type Context struct {
2929

3030
Values map[string]interface{}
3131

32-
om *OpenMock
32+
om *OpenMock
33+
currentMock *Mock
3334
}
3435

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

6061
// MatchCondition checks the condition given the context
6162
func (c Context) MatchCondition(condition string) (r bool) {
63+
logger := newOmLogger(c)
6264
defer func() {
6365
if r {
64-
logrus.WithFields(logrus.Fields{
66+
logger.Info("ok match condition")
67+
logger.WithFields(logrus.Fields{
6568
"HTTPHeader": c.HTTPHeader,
6669
"HTTPBody": c.HTTPBody,
6770
"KafkaPayload": c.KafkaPayload,
@@ -78,7 +81,7 @@ func (c Context) MatchCondition(condition string) (r bool) {
7881

7982
result, err := c.Render(condition)
8083
if err != nil {
81-
logrus.WithField("err", err).Errorf("failed to render condition: %s", condition)
84+
logger.WithField("err", err).Errorf("failed to render condition: %s", condition)
8285
return false
8386
}
8487
return result == "true"

0 commit comments

Comments
 (0)