Example #1
0
		getRecentLogs := func() []*events.LogMessage {
			envelopes, err := consumer.RecentLogs("foo", "")
			Expect(err).NotTo(HaveOccurred())
			return envelopes
		}

		Eventually(getRecentLogs).Should(ContainElement(env.LogMessage))
	})

	It("sends log messages for a specific app through the stream endpoint", func() {
		msgChan, errorChan := helpers.ConnectToStream("foo")

		helpers.EmitToMetron(createLogEnvelope("Stream message", "bar"))
		helpers.EmitToMetron(createLogEnvelope("Stream message", "foo"))

		receivedEnvelope, err := helpers.FindMatchingEnvelopeById("foo", msgChan)
		Expect(err).NotTo(HaveOccurred())
		Expect(receivedEnvelope).NotTo(BeNil())
		Expect(receivedEnvelope.GetEventType()).To(Equal(events.Envelope_LogMessage))

		event := receivedEnvelope.GetLogMessage()
		Expect(envelope_extensions.GetAppId(receivedEnvelope)).To(Equal("foo"))
		Expect(string(event.GetMessage())).To(Equal("Stream message"))
		Expect(event.GetMessageType().String()).To(Equal(events.LogMessage_OUT.Enum().String()))
		Expect(event.GetTimestamp()).ToNot(BeZero())

		Expect(errorChan).To(BeEmpty())
	})
})

func createLogEnvelope(message, appId string) *events.Envelope {
			emitter := emitter.NewEventEmitter(udpEmitter, helpers.ORIGIN_NAME)
			r, err := http.NewRequest("HEAD", "/", nil)
			Expect(err).ToNot(HaveOccurred())
			r.Header.Add("User-Agent", "Superman")
			r.Header.Add("X-CF-ApplicationID", id.String())
			done := make(chan struct{})
			handler := instrumented_handler.InstrumentedHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				w.WriteHeader(http.StatusTeapot)
				close(done)
			}), emitter)

			w := httptest.NewRecorder()
			handler.ServeHTTP(w, r)
			Eventually(done).Should(BeClosed())

			receivedEnvelope, err := helpers.FindMatchingEnvelopeById(id.String(), msgChan)
			Expect(err).NotTo(HaveOccurred())
			Expect(receivedEnvelope).NotTo(BeNil())
			Expect(receivedEnvelope.GetEventType()).To(Equal(events.Envelope_HttpStartStop))

			event := receivedEnvelope.GetHttpStartStop()
			Expect(envelope_extensions.GetAppId(receivedEnvelope)).To(Equal(id.String()))
			Expect(event.GetPeerType().String()).To(Equal(events.PeerType_Server.Enum().String()))
			Expect(event.GetMethod().String()).To(Equal(events.Method_HEAD.Enum().String()))
			Expect(event.GetStartTimestamp()).ToNot(BeZero())
			Expect(event.GetStopTimestamp()).ToNot(BeZero())
			Expect(event.GetUserAgent()).To(Equal("Superman"))
			Expect(event.GetStatusCode()).To(BeEquivalentTo(http.StatusTeapot))

			Expect(errorChan).To(BeEmpty())
		})