예제 #1
0
func Initialize() {
	http.DefaultTransport = &http.Transport{Proxy: http.ProxyFromEnvironment}
	autowiredEmitter = nil

	origin := os.Getenv("DROPSONDE_ORIGIN")
	if len(origin) == 0 {
		log.Println("Failed to auto-initialize dropsonde: DROPSONDE_ORIGIN environment variable not set")
		return
	}

	destination = os.Getenv("DROPSONDE_DESTINATION")
	if len(destination) == 0 {
		log.Println("DROPSONDE_DESTINATION not set. Using " + defaultDestination)
		destination = defaultDestination
	}

	udpEmitter, err := emitter.NewUdpEmitter(destination)
	if err != nil {
		log.Printf("Failed to auto-initialize dropsonde: %v\n", err)
		return
	}

	hbEmitter, err := emitter.NewHeartbeatEmitter(udpEmitter, origin)
	if err != nil {
		log.Printf("Failed to auto-initialize dropsonde: %v\n", err)
		return
	}

	autowiredEmitter = emitter.NewEventEmitter(hbEmitter, origin)

	http.DefaultTransport = InstrumentedRoundTripper(http.DefaultTransport)
}
)

var _ = Describe("HeartbeatEmitter", func() {
	var (
		wrappedEmitter *fake.FakeByteEmitter
		origin         = "testHeartbeatEmitter/0"
	)

	BeforeEach(func() {
		emitter.HeartbeatInterval = 10 * time.Millisecond
		wrappedEmitter = fake.NewFakeByteEmitter()
	})

	Describe("NewHeartbeatEmitter", func() {
		It("requires non-nil args", func() {
			hbEmitter, err := emitter.NewHeartbeatEmitter(nil, origin)
			Expect(err).To(HaveOccurred())
			Expect(err.Error()).To(Equal("wrappedEmitter is nil"))
			Expect(hbEmitter).To(BeNil())
		})

		It("starts periodic heartbeat emission", func() {
			hbEmitter, err := emitter.NewHeartbeatEmitter(wrappedEmitter, origin)
			Expect(err).NotTo(HaveOccurred())
			Expect(hbEmitter).NotTo(BeNil())

			Eventually(func() int { return len(wrappedEmitter.GetMessages()) }).Should(BeNumerically(">=", 2))
		})

		It("logs an error when heartbeat emission fails", func() {
			wrappedEmitter.ReturnError = errors.New("fake error")