func (r *MessageRouter) send(envelope *events.Envelope) { appId := envelope_extensions.GetAppId(envelope) r.logger.Debugf("MessageRouter:outgoingLogChan: Searching for sinks with appId [%s].", appId) r.sinkManager.SendTo(appId, envelope) r.logger.Debugf("MessageRouter:outgoingLogChan: Done sending message.") }
func (r *TruncatingBuffer) Run() { for msg := range r.inputChannel { r.lock.Lock() select { case r.outputChannel <- msg: default: messageCount := len(r.outputChannel) r.droppedMessageCount += int64(messageCount) r.outputChannel = make(chan *events.Envelope, cap(r.outputChannel)) appId := envelope_extensions.GetAppId(msg) lm := generateLogMessage(fmt.Sprintf("Log message output too high. We've dropped %d messages", messageCount), appId) env, err := emitter.Wrap(lm, r.dropsondeOrigin) if err == nil { r.outputChannel <- env } else { r.logger.Warnf("Error marshalling message: %v", err) } r.outputChannel <- msg if r.logger != nil { r.logger.Warn(fmt.Sprintf("TB: Output channel too full. Dropped %d messages for app %s.", messageCount, appId)) } } r.lock.Unlock() } close(r.outputChannel) }
func (sinkManager *SinkManager) listenForErrorMessages() { for { select { case <-sinkManager.doneChannel: return case errorMessage, ok := <-sinkManager.errorChannel: if !ok { return } appId := envelope_extensions.GetAppId(errorMessage) sinkManager.logger.Debugf("SinkManager:ErrorChannel: Searching for sinks with appId [%s].", appId) sinkManager.sinks.BroadcastError(appId, errorMessage) sinkManager.logger.Debugf("SinkManager:ErrorChannel: Done sending error message.") } } }
func FindMatchingEnvelopeById(id string, msgChan <-chan *events.Envelope) (*events.Envelope, error) { timeout := time.After(10 * time.Second) for { select { case receivedEnvelope := <-msgChan: receivedId := envelope_extensions.GetAppId(receivedEnvelope) if receivedId == id { return receivedEnvelope, nil } else { return nil, errors.New(fmt.Sprintf("Expected messages with app id: %s, got app id: %s", id, receivedId)) } case <-timeout: return nil, errors.New("Timed out while waiting for message") } } }
It("puts an error on the message channel when reading messages", func() { channelConnector := channel_group_connector.NewChannelGroupConnector(finder, listenerConstructor, marshaller.DropsondeLogMessage, logger) stopChan := make(chan struct{}) defer close(stopChan) dopplerEndpoint := doppler_endpoint.NewDopplerEndpoint("stream", "abc123", false) go channelConnector.Connect(dopplerEndpoint, messageChan1, stopChan) msg := &[]byte{} Eventually(messageChan1).Should(Receive(msg)) envelope := &events.Envelope{} err := proto.Unmarshal(*msg, envelope) Expect(err).NotTo(HaveOccurred()) Expect(envelope_extensions.GetAppId(envelope)).To(Equal("abc123")) Expect(envelope.GetLogMessage().GetMessage()).To(BeEquivalentTo("proxy: error connecting to 10.0.0.1:1234: failure")) }) }) Context("when streaming messages from a single server and a listener error occurrs", func() { BeforeEach(func() { messageChan1 <- expectedMessage1 fakeListeners[0].SetReadError(errors.New("boom")) finder.WebsocketServersOutput.ret0 <- []string{"10.0.0.1:1234"} }) AfterEach(func() { for _, l := range fakeListeners {
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 { return &events.Envelope{ EventType: events.Envelope_LogMessage.Enum(), Origin: proto.String(helpers.ORIGIN_NAME), Timestamp: proto.Int64(time.Now().UnixNano()), LogMessage: &events.LogMessage{ Message: []byte(message),
func (d *DefaultContext) AppID(envelope *events.Envelope) string { return envelope_extensions.GetAppId(envelope) }
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()) }) }) })
) var _ = Describe("EnvelopeExtensions", func() { var testAppUuid = &events.UUID{ Low: proto.Uint64(1), High: proto.Uint64(2), } Describe("GetAppId", func() { Context("HttpStart", func() { It("returns the App ID if it has one", func() { envelope := &events.Envelope{ EventType: events.Envelope_HttpStart.Enum(), HttpStart: &events.HttpStart{ApplicationId: testAppUuid}, } appId := envelope_extensions.GetAppId(envelope) Expect(appId).To(Equal("01000000-0000-0000-0200-000000000000")) }) It("returns system app ID if there isn't an App ID", func() { envelope := &events.Envelope{ EventType: events.Envelope_HttpStart.Enum(), HttpStart: &events.HttpStart{}, } appId := envelope_extensions.GetAppId(envelope) Expect(appId).To(Equal(envelope_extensions.SystemAppId)) }) }) Context("HttpStop", func() { It("returns the App ID if it has one", func() {