func createEnvelope(eventType events.Envelope_EventType) *events.Envelope {
	envelope := &events.Envelope{Origin: proto.String("origin"), EventType: &eventType, Timestamp: proto.Int64(time.Now().UnixNano())}

	switch eventType {
	case events.Envelope_HttpStartStop:
		req, _ := http.NewRequest("GET", "http://www.example.com", nil)
		req.RemoteAddr = "www.example.com"
		req.Header.Add("User-Agent", "user-agent")
		uuid, _ := uuid.NewV4()
		envelope.HttpStartStop = factories.NewHttpStartStop(req, http.StatusOK, 128, events.PeerType_Client, uuid)
	case events.Envelope_ValueMetric:
		envelope.ValueMetric = factories.NewValueMetric("some-value-metric", 123, "km")
	case events.Envelope_CounterEvent:
		envelope.CounterEvent = factories.NewCounterEvent("some-counter-event", 123)
	case events.Envelope_LogMessage:
		envelope.LogMessage = factories.NewLogMessage(events.LogMessage_OUT, "some message", "appId", "source")
	case events.Envelope_ContainerMetric:
		envelope.ContainerMetric = factories.NewContainerMetric("appID", 123, 1, 5, 5)
	case events.Envelope_Error:
		envelope.Error = factories.NewError("source", 123, "message")
	default:
		panic(fmt.Sprintf("Unknown event %v\n", eventType))
	}

	return envelope
}
	})

	It("dumps buffer data to the websocket client with /recentlogs", func(done Done) {
		lm, _ := emitter.Wrap(factories.NewLogMessage(events.LogMessage_OUT, "my message", appId, "App"), "origin")
		sinkManager.SendTo(appId, lm)

		AddWSSink(wsReceivedChan, fmt.Sprintf("ws://%s/apps/%s/recentlogs", apiEndpoint, appId))

		rlm, err := receiveEnvelope(wsReceivedChan)
		Expect(err).NotTo(HaveOccurred())
		Expect(rlm.GetLogMessage().GetMessage()).To(Equal(lm.GetLogMessage().GetMessage()))
		close(done)
	})

	It("dumps container metric data to the websocket client with /containermetrics", func(done Done) {
		cm := factories.NewContainerMetric(appId, 0, 42.42, 1234, 123412341234)
		envelope, _ := emitter.Wrap(cm, "origin")
		sinkManager.SendTo(appId, envelope)

		AddWSSink(wsReceivedChan, fmt.Sprintf("ws://%s/apps/%s/containermetrics", apiEndpoint, appId))

		rcm, err := receiveEnvelope(wsReceivedChan)
		Expect(err).NotTo(HaveOccurred())
		Expect(rcm.GetContainerMetric()).To(Equal(cm))
		close(done)
	})

	It("sends data to the websocket client with /stream", func(done Done) {
		stopKeepAlive, _ := AddWSSink(wsReceivedChan, fmt.Sprintf("ws://%s/apps/%s/stream", apiEndpoint, appId))
		lm, _ := emitter.Wrap(factories.NewLogMessage(events.LogMessage_OUT, "my message", appId, "App"), "origin")
		sinkManager.SendTo(appId, lm)
				},
			}

			message, err := proto.Marshal(logMessage)
			Expect(err).ToNot(HaveOccurred())
			_, err = unmarshaller.UnmarshallMessage(message)
			Expect(err).ToNot(HaveOccurred())
			Eventually(func() uint64 { return fakeSender.GetCounter("dropsondeUnmarshaller.logMessageTotal") }).Should(BeEquivalentTo(1))
		})

		Describe("emits counters for", func() {
			It("container metrics", func() {
				containerMetric := &events.Envelope{
					Origin:          proto.String("origin"),
					EventType:       events.Envelope_ContainerMetric.Enum(),
					ContainerMetric: factories.NewContainerMetric("appId", 0, 3.0, 23, 23),
				}
				message, err := proto.Marshal(containerMetric)
				Expect(err).ToNot(HaveOccurred())
				_, err = unmarshaller.UnmarshallMessage(message)
				Expect(err).ToNot(HaveOccurred())
				Eventually(func() uint64 { return fakeSender.GetCounter("dropsondeUnmarshaller.containerMetricReceived") }).Should(BeEquivalentTo(1))

			})

			It("HTTP start event", func() {
				uuid, _ := uuid.NewV4()
				request, _ := http.NewRequest("GET", "https://example.com", nil)
				httpStart := &events.Envelope{
					Origin:    proto.String("origin"),
					EventType: events.Envelope_HttpStart.Enum(),
	. "integration_tests/doppler/helpers"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
)

var _ = Describe("Container Metrics", func() {
	var receivedChan chan []byte
	var inputConnection net.Conn
	var appID string

	itDoesContainerMetrics := func(send func(events.Event, net.Conn) error) {
		It("returns container metrics for an app", func() {
			containerMetric := factories.NewContainerMetric(appID, 0, 1, 2, 3)

			send(containerMetric, inputConnection)

			Eventually(dopplerSession).Should(gbytes.Say(`Done sending`))

			receivedChan = make(chan []byte)
			ws, _ := AddWSSink(receivedChan, "4567", "/apps/"+appID+"/containermetrics")
			defer ws.Close()

			var receivedMessageBytes []byte
			Eventually(receivedChan).Should(Receive(&receivedMessageBytes))

			receivedEnvelope := UnmarshalMessage(receivedMessageBytes)

			Expect(receivedEnvelope.GetEventType()).To(Equal(events.Envelope_ContainerMetric))
		Expect(err).NotTo(HaveOccurred())

		err = SendAppLog("otherAppId", "message 2", inputConnection)
		Expect(err).NotTo(HaveOccurred())

		receivedMessageBytes := []byte{}
		Eventually(receivedChan).Should(Receive(&receivedMessageBytes))
		receivedMessage := DecodeProtoBufLogMessage(receivedMessageBytes)

		Expect(receivedMessage.GetAppId()).To(Equal(appID))
		Expect(string(receivedMessage.GetMessage())).To(Equal("message 1"))
		Expect(receivedChan).To(BeEmpty())
	})

	It("does not recieve non-log messages", func() {
		metricEvent := factories.NewContainerMetric(appID, 0, 10, 10, 10)
		SendEvent(metricEvent, inputConnection)

		Expect(receivedChan).To(BeEmpty())
	})

	It("drops invalid log envelopes", func() {
		unmarshalledLogMessage := factories.NewLogMessage(events.LogMessage_OUT, "Some Data", appID, "App")
		expectedMessage := MarshalEvent(unmarshalledLogMessage, "invalid")

		_, err := inputConnection.Write(expectedMessage)
		Expect(err).To(BeNil())
		Expect(receivedChan).To(BeEmpty())
	})
})
Exemple #6
0
					case <-done:
						return
					}
				}
			}()

			var receivedMessageBytes []byte
			Eventually(receiveChan).Should(Receive(&receivedMessageBytes))
			close(done)

			Expect(DecodeProtoBufEnvelope(receivedMessageBytes).GetEventType()).To(Equal(events.Envelope_LogMessage))
		})

		It("receives container metrics", func() {
			done := make(chan struct{})
			containerMetric := factories.NewContainerMetric(appID, 0, 10, 2, 3)
			go func() {
				for {
					SendEvent(containerMetric, inputConnection)
					select {
					case <-time.After(500 * time.Millisecond):
					case <-done:
						return
					}
				}
			}()

			var receivedMessageBytes []byte
			Eventually(receiveChan).Should(Receive(&receivedMessageBytes))
			close(done)
			Expect(DecodeProtoBufEnvelope(receivedMessageBytes).GetEventType()).To(Equal(events.Envelope_ContainerMetric))
				AppId:       proto.String("app-id"),
				MessageType: events.LogMessage_OUT.Enum(),
				SourceType:  proto.String("App"),
			}

			logEvent := factories.NewLogMessage(events.LogMessage_OUT, "hello", "app-id", "App")

			Expect(logEvent.GetTimestamp()).ToNot(BeZero())
			logEvent.Timestamp = nil

			Expect(logEvent).To(Equal(expectedLogEvent))
		})
	})

	Describe("NewContainerMetric", func() {
		It("should set the appropriate fields", func() {
			expectedContainerMetric := &events.ContainerMetric{
				ApplicationId: proto.String("some_app_id"),
				InstanceIndex: proto.Int32(7),
				CpuPercentage: proto.Float64(42.24),
				MemoryBytes:   proto.Uint64(1234),
				DiskBytes:     proto.Uint64(13231231),
			}

			containerMetric := factories.NewContainerMetric("some_app_id", 7, 42.24, 1234, 13231231)

			Expect(containerMetric).To(Equal(expectedContainerMetric))
		})
	})
})
	It("returns correct path for firehose", func() {
		dopplerEndpoint := doppler_endpoint.NewDopplerEndpoint("firehose", "subscription-123", true)
		Expect(dopplerEndpoint.GetPath()).To(Equal("/firehose/subscription-123"))
	})

	It("returns correct path for recentlogs", func() {
		dopplerEndpoint := doppler_endpoint.NewDopplerEndpoint("recentlogs", "abc123", true)
		Expect(dopplerEndpoint.GetPath()).To(Equal("/apps/abc123/recentlogs"))
	})
})

var _ = Describe("ContainerMetricsHandler", func() {
	It("removes duplicate app container metrics", func() {
		messagesChan := make(chan []byte, 2)

		env1, _ := emitter.Wrap(factories.NewContainerMetric("1", 1, 123, 123, 123), "origin")
		env1.Timestamp = proto.Int64(10000)

		env2, _ := emitter.Wrap(factories.NewContainerMetric("1", 1, 123, 123, 123), "origin")
		env2.Timestamp = proto.Int64(20000)

		bytes1, _ := proto.Marshal(env1)
		bytes2, _ := proto.Marshal(env2)

		messagesChan <- bytes2
		messagesChan <- bytes1
		close(messagesChan)

		outputChan := doppler_endpoint.DeDupe(messagesChan)

		Expect(outputChan).To(HaveLen(1))