Example #1
0
// Initialize creates default emitters and instruments the default HTTP
// transport.
//
// The DROPSONDE_ORIGIN environment variable is required and specifies the
// source name for all metrics emitted by this process. If it is not set, the
// program will run normally but will not emit metrics.
//
// The DROPSONDE_DESTINATION environment variable sets the host and port to
// which metrics are sent. It is optional, and defaults to DefaultDestination.
func Initialize() {
	http.DefaultTransport = &http.Transport{Proxy: http.ProxyFromEnvironment}
	autowiredEmitter = &nullEventEmitter{}

	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)

	go runtime_stats.NewRuntimeStats(autowiredEmitter, runtimeStatsInterval).Run(nil)

	http.DefaultTransport = InstrumentedRoundTripper(http.DefaultTransport)
}
Example #2
0
func CreateDefaultEmitter() (emitter.EventEmitter, string) {
	origin := os.Getenv("DROPSONDE_ORIGIN")
	if len(origin) == 0 {
		log.Println("Failed to auto-initialize dropsonde: DROPSONDE_ORIGIN environment variable not set")
		return nil, ""
	}

	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 nil, destination
	}

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

	return emitter.NewEventEmitter(hbEmitter, origin), destination
}
)

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")