Esempio n. 1
0
		go uptimeMonitor.Start()
	})

	AfterEach(func() {
		fakeEventEmitter.Close()
	})

	Context("stops automatically", func() {

		AfterEach(func() {
			uptimeMonitor.Stop()
		})

		It("returns a value metric containing uptime after specified time", func() {
			Eventually(fakeEventEmitter.GetMessages).Should(HaveLen(1))
			Expect(fakeEventEmitter.GetMessages()[0].Event.(*events.ValueMetric)).To(Equal(&events.ValueMetric{
				Name:  proto.String("Uptime"),
				Value: proto.Float64(0),
				Unit:  proto.String("seconds"),
			}))
		})

		It("reports increasing uptime value", func() {
			Eventually(fakeEventEmitter.GetMessages).Should(HaveLen(1))
			Expect(fakeEventEmitter.GetMessages()[0].Event.(*events.ValueMetric)).To(Equal(&events.ValueMetric{
				Name:  proto.String("Uptime"),
				Value: proto.Float64(0),
				Unit:  proto.String("seconds"),
			}))

			Eventually(getLatestUptime).Should(Equal(1.0))
	Describe("event emission", func() {
		var requestId *uuid.UUID

		BeforeEach(func() {
			requestId, _ = uuid.NewV4()
			req.Header.Set("X-CF-RequestID", requestId.String())
		})

		Context("without an application ID or instanceIndex", func() {
			BeforeEach(func() {
				h.ServeHTTP(httptest.NewRecorder(), req)
			})

			It("should emit a start event with the right origin", func() {
				Expect(fakeEmitter.GetMessages()[0].Event).To(BeAssignableToTypeOf(new(events.HttpStart)))
				Expect(fakeEmitter.GetMessages()[0].Origin).To(Equal("testHandler/41"))
			})

			It("should emit a stop event", func() {
				Expect(fakeEmitter.GetMessages()[1].Event).To(BeAssignableToTypeOf(new(events.HttpStop)))
				stopEvent := fakeEmitter.GetMessages()[1].Event.(*events.HttpStop)
				Expect(stopEvent.GetStatusCode()).To(BeNumerically("==", 123))
				Expect(stopEvent.GetContentLength()).To(BeNumerically("==", 12))
			})
		})
	})

	Describe("satisfaction of interfaces", func() {

		var (
			close(runDone)
		}()
	}

	var getMetricNames = func() []string {
		var names []string
		for _, event := range fakeEventEmitter.GetEvents() {
			names = append(names, event.(*events.ValueMetric).GetName())
		}
		return names
	}

	It("periodically emits events", func() {
		perform()

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

	It("emits a NumCpu metric", func() {
		perform()

		Eventually(fakeEventEmitter.GetEvents).Should(ContainElement(&events.ValueMetric{
			Name:  proto.String("numCPUS"),
			Value: proto.Float64(float64(runtime.NumCPU())),
			Unit:  proto.String("count"),
		}))
	})

	It("emits a NumGoRoutines metric", func() {
		perform()
				It("logs the dropped messages", func() {
					logMessageNotification := <-buffer.GetOutputChannel()
					Expect(logMessageNotification.GetEventType()).To(Equal(events.Envelope_LogMessage))
					Expect(logMessageNotification.GetLogMessage().GetMessage()).To(ContainSubstring(fmt.Sprintf("Log message output is too high. %d messages dropped (Total %d messages dropped) to test-sink-name.", delta, total)))

					counterEventNotification := <-buffer.GetOutputChannel()
					Expect(counterEventNotification.GetEventType()).To(Equal(events.Envelope_CounterEvent))
					counterEvent := counterEventNotification.GetCounterEvent()
					Expect(counterEvent.GetName()).To(Equal("TruncatingBuffer.DroppedMessages"))
					Expect(counterEvent.GetDelta()).To(BeEquivalentTo(delta))
					Expect(counterEvent.GetTotal()).To(BeEquivalentTo(total))
				})

				It("keeps track of dropped messages", func() {
					Eventually(fakeEventEmitter.GetMessages).Should(HaveLen(1))
					Expect(fakeEventEmitter.GetMessages()[0].Event.(*events.CounterEvent)).To(Equal(&events.CounterEvent{
						Name:  proto.String("TruncatingBuffer.totalDroppedMessages"),
						Delta: proto.Uint64(uint64(total)),
					}))
				})
			}

			Context("when the buffer fills once", func() {
				JustBeforeEach(func() {
					Expect(buffer.GetDroppedMessageCount()).To(BeZero())

					firstBuffer := buffer.GetOutputChannel()
					sendLogMessages("message 1", inMessageChan)
					sendLogMessages("message 2", inMessageChan)
					sendLogMessages("message 3", inMessageChan)
					sendLogMessages("message 4", inMessageChan)
			Expect(readMessage.GetLogMessage().GetMessage()).To(ContainSubstring("message 1"))

			sendLogMessages("message 2", inMessageChan)

			Eventually(buffer.GetOutputChannel).Should(Receive(&readMessage))
			Expect(readMessage.GetLogMessage().GetMessage()).To(ContainSubstring("message 2"))

		})

		Context("tracking dropped messages", func() {
			var fakeEventEmitter *fake.FakeEventEmitter

			receiveDroppedMessages := func() uint64 {
				var t uint64
				for i, _ := range fakeEventEmitter.GetMessages() {
					e := fakeEventEmitter.GetMessages()[i].Event.(*events.CounterEvent)

					t += e.GetDelta()
				}

				return t
			}

			tracksDroppedMessagesAnd := func(itMsg string, delta, total int) {
				It(itMsg, func() {
					var logMessageNotification *events.Envelope
					Eventually(buffer.GetOutputChannel).Should(Receive(&logMessageNotification))
					Expect(logMessageNotification.GetEventType()).To(Equal(events.Envelope_LogMessage))
					Expect(logMessageNotification.GetLogMessage().GetAppId()).To(Equal("fake-app-id"))
					Expect(logMessageNotification.GetLogMessage().GetMessage()).To(ContainSubstring(fmt.Sprintf("Log message output is too high. %d messages dropped (Total %d messages dropped) from doppler to test-sink-name.", delta, total)))
Esempio n. 6
0
			It("sends an error when log error messages cannot be emitted", func() {
				err := sender.SendAppErrorLog("app-id", "custom-log-error-message", "App", "0")
				Expect(err).To(HaveOccurred())
			})

		})
	})

	Describe("ScanLogStream", func() {

		It("sends lines from stream to emitter", func() {
			buf := bytes.NewBufferString("line 1\nline 2\n")

			sender.ScanLogStream("someId", "app", "0", buf)

			messages := emitter.GetMessages()
			Expect(messages).To(HaveLen(2))

			log := emitter.Messages[0].Event.(*events.LogMessage)
			Expect(log.GetMessage()).To(BeEquivalentTo("line 1"))
			Expect(log.GetMessageType()).To(Equal(events.LogMessage_OUT))
			Expect(log.GetAppId()).To(Equal("someId"))
			Expect(log.GetSourceType()).To(Equal("app"))
			Expect(log.GetSourceInstance()).To(Equal("0"))

			log = emitter.Messages[1].Event.(*events.LogMessage)
			Expect(log.GetMessage()).To(BeEquivalentTo("line 2"))
			Expect(log.GetMessageType()).To(Equal(events.LogMessage_OUT))
		})

		It("logs a message and returns on read errors", func() {
Esempio n. 7
0
var _ = Describe("MetricSender", func() {
	var (
		emitter *fake.FakeEventEmitter
		sender  metric_sender.MetricSender
	)

	BeforeEach(func() {
		emitter = fake.NewFakeEventEmitter("origin")
		sender = metric_sender.NewMetricSender(emitter)
	})

	It("sends a metric to its emitter", func() {
		err := sender.SendValue("metric-name", 42, "answers")
		Expect(err).NotTo(HaveOccurred())

		Expect(emitter.GetMessages()).To(HaveLen(1))
		metric := emitter.GetMessages()[0].Event.(*events.ValueMetric)
		Expect(metric.GetName()).To(Equal("metric-name"))
		Expect(metric.GetValue()).To(BeNumerically("==", 42))
		Expect(metric.GetUnit()).To(Equal("answers"))
	})

	It("returns an error if it can't send metric value", func() {
		emitter.ReturnError = errors.New("some error")

		err := sender.SendValue("stuff", 12, "no answer")
		Expect(emitter.GetMessages()).To(HaveLen(0))
		Expect(err.Error()).To(Equal("some error"))
	})

	It("sends an update counter event to its emitter", func() {
Esempio n. 8
0
		sender = log_sender.NewLogSender(emitter, loggertesthelper.Logger())
	})

	AfterEach(func() {
		emitter.Close()
		for !emitter.IsClosed() {
			time.Sleep(10 * time.Millisecond)
		}
	})

	Describe("SendAppLog", func() {
		It("sends a log message event to its emitter", func() {
			err := sender.SendAppLog("app-id", "custom-log-message", "App", "0")
			Expect(err).NotTo(HaveOccurred())

			Expect(emitter.GetMessages()).To(HaveLen(1))
			log := emitter.GetMessages()[0].Event.(*events.LogMessage)
			Expect(log.GetMessageType()).To(Equal(events.LogMessage_OUT))
			Expect(log.GetMessage()).To(BeEquivalentTo("custom-log-message"))
			Expect(log.GetAppId()).To(Equal("app-id"))
			Expect(log.GetSourceType()).To(Equal("App"))
			Expect(log.GetSourceInstance()).To(Equal("0"))
			Expect(log.GetTimestamp()).ToNot(BeNil())
		})

		It("totals number of log messages sent to emitter", func() {
			sender.SendAppLog("app-id", "custom-log-message", "App", "0")
			sender.SendAppLog("app-id", "custom-log-message", "App", "0")

			Eventually(emitter.GetEvents).Should(ContainElement(&events.CounterEvent{Name: proto.String("logSenderTotalMessagesRead"), Delta: proto.Uint64(2)}))
		})
Esempio n. 9
0
	)

	BeforeEach(func() {
		envOrigin = "original-origin"
		emitter = fake.NewFakeEventEmitter("origin")
		sender = envelope_sender.NewEnvelopeSender(emitter)
	})

	It("sends an Envelope to its emitter", func() {
		err := sender.SendEnvelope(createTestEnvelope(envOrigin))
		Expect(err).NotTo(HaveOccurred())

		Expect(emitter.GetEnvelopes()).To(HaveLen(1))
		envelope := emitter.GetEnvelopes()[0]
		metric := envelope.ValueMetric
		Expect(metric.GetName()).To(Equal("metric-name"))
		Expect(metric.GetValue()).To(BeNumerically("==", 42))
		Expect(metric.GetUnit()).To(Equal("answers"))
		Expect(envelope.Origin).To(Equal(proto.String(envOrigin)))
	})

	It("returns an error if it can't send metric value", func() {
		emitter.ReturnError = errors.New("some error")

		err := sender.SendEnvelope(createTestEnvelope(envOrigin))
		Expect(emitter.GetMessages()).To(HaveLen(0))
		Expect(err.Error()).To(Equal("some error"))
	})

})