Skip to content

Instantly share code, notes, and snippets.

@dpb587-pivotal
Created January 31, 2018 18:52
Show Gist options
  • Save dpb587-pivotal/74a59c587c712b8b2fff39af8b0020c8 to your computer and use it in GitHub Desktop.
Save dpb587-pivotal/74a59c587c712b8b2fff39af8b0020c8 to your computer and use it in GitHub Desktop.
diff --git a/handler/topic.go b/handler/topic.go
index 06c53adc..dcea8a9c 100644
--- a/handler/topic.go
+++ b/handler/topic.go
@@ -1,9 +1,25 @@
package handler
-type Topic string
+type Topic interface {
+ String() string
+ SuppressLogs() bool
+}
-const (
- Heartbeat = Topic("heartbeat")
- Alert = Topic("alert")
- Shutdown = Topic("shutdown")
+type topic struct {
+ topicName string
+ suppressLogs bool
+}
+
+func (t topic) String() string {
+ return t.topicName
+}
+
+func (t topic) SuppressLogs() bool {
+ return t.suppressLogs
+}
+
+var (
+ Heartbeat = topic{topicName: "heartbeat", suppressLogs: true}
+ Alert = topic{topicName: "alert"}
+ Shutdown = topic{topicName: "shutdown"}
)
diff --git a/mbus/nats_handler.go b/mbus/nats_handler.go
index 60d261dd..638f580e 100644
--- a/mbus/nats_handler.go
+++ b/mbus/nats_handler.go
@@ -18,13 +18,14 @@ import (
"time"
"crypto/tls"
+ "regexp"
+
boshhandler "github.com/cloudfoundry/bosh-agent/handler"
boshplatform "github.com/cloudfoundry/bosh-agent/platform"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshretry "github.com/cloudfoundry/bosh-utils/retrystrategy"
- "regexp"
)
const (
@@ -157,8 +158,10 @@ func (h *natsHandler) Send(target boshhandler.Target, topic boshhandler.Topic, m
return bosherr.WrapErrorf(err, "Marshalling message (target=%s, topic=%s): %#v", target, topic, message)
}
- h.logger.Info(h.logTag, "Sending %s message '%s'", target, topic)
- h.logger.DebugWithDetails(h.logTag, "Message Payload", string(bytes))
+ if !topic.SuppressLogs() {
+ h.logger.Info(h.logTag, "Sending %s message '%s'", target, topic)
+ h.logger.DebugWithDetails(h.logTag, "Message Payload", string(bytes))
+ }
settings := h.settingsService.GetSettings()
diff --git a/mbus/nats_handler_test.go b/mbus/nats_handler_test.go
index c80ea318..ed76bb56 100644
--- a/mbus/nats_handler_test.go
+++ b/mbus/nats_handler_test.go
@@ -5,6 +5,7 @@ import (
"errors"
. "github.com/onsi/ginkgo"
+ . "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"github.com/cloudfoundry/yagnats"
@@ -43,7 +44,7 @@ func init() {
}
loggerOutBuf = bytes.NewBufferString("")
- logger = boshlog.NewWriterLogger(boshlog.LevelError, loggerOutBuf)
+ logger = boshlog.NewWriterLogger(boshlog.LevelDebug, loggerOutBuf)
client = fakeyagnats.New()
platform = fakeplatform.NewFakePlatform()
@@ -539,6 +540,38 @@ func init() {
[]byte("{\"key1\":\"value1\",\"keyA\":\"valueA\"}"),
))
})
+
+ FDescribeTable(
+ "logging behavior",
+ func(topic boshhandler.Topic, expected bool) {
+ errCh := make(chan error, 1)
+
+ payload := map[string]string{"key1": "value1", "keyA": "valueA"}
+
+ go func() {
+ errCh <- handler.Send(boshhandler.HealthMonitor, topic, payload)
+ }()
+
+ var err error
+ select {
+ case err = <-errCh:
+ }
+ Expect(err).ToNot(HaveOccurred())
+
+ infoMatch := ContainSubstring("INFO - Sending hm message")
+ debugMatch := ContainSubstring("DEBUG - Message Payload")
+
+ if !expected {
+ infoMatch = Not(infoMatch)
+ debugMatch = Not(debugMatch)
+ }
+
+ Expect(loggerOutBuf.String()).To(infoMatch)
+ Expect(loggerOutBuf.String()).To(debugMatch)
+ },
+ Entry("logs messages if the topic allows it", boshhandler.Alert, true),
+ Entry("suppresses messages if the topic disallows it", boshhandler.Heartbeat, false),
+ )
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment