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 }
var _ = Describe("EventUnmarshaller", func() { var ( mockWriter *mocks.MockEnvelopeWriter unmarshaller *eventunmarshaller.EventUnmarshaller event *events.Envelope message []byte fakeSender *fake.FakeMetricSender ) BeforeEach(func() { mockWriter = &mocks.MockEnvelopeWriter{} unmarshaller = eventunmarshaller.New(mockWriter, loggertesthelper.Logger()) event = &events.Envelope{ Origin: proto.String("fake-origin-3"), EventType: events.Envelope_ValueMetric.Enum(), ValueMetric: factories.NewValueMetric("value-name", 1.0, "units"), } message, _ = proto.Marshal(event) fakeSender = fake.NewFakeMetricSender() metricBatcher := metricbatcher.New(fakeSender, time.Millisecond) metrics.Initialize(fakeSender, metricBatcher) }) Context("UnmarshallMessage", func() { It("unmarshalls bytes", func() { output, _ := unmarshaller.UnmarshallMessage(message) Expect(output).To(Equal(event)) })
It("sends input messages to the syslog writer", func(done Done) { logMessage := factories.NewLogMessage(events.LogMessage_OUT, "test message", "appId", "App") logMessage.SourceInstance = proto.String("123") envelope, _ := emitter.Wrap(logMessage, "origin") inputChan <- envelope data := <-sysLogger.receivedChannel expectedSyslogMessage := fmt.Sprintf(`<14>1 test message ts: \d+ src: App srcId: 123`) Expect(string(data)).To(MatchRegexp(expectedSyslogMessage)) close(done) }) It("does not send non-log messages to the syslog writer", func(done Done) { nonLogMessage := factories.NewValueMetric("value-name", 2.0, "value-unit") envelope, _ := emitter.Wrap(nonLogMessage, "origin") inputChan <- envelope Consistently(sysLogger.receivedChannel).ShouldNot(Receive()) close(done) }) It("stops sending messages when the disconnect comes in", func(done Done) { logMessage, _ := emitter.Wrap(factories.NewLogMessage(events.LogMessage_OUT, "test message", "appId", "App"), "origin") inputChan <- logMessage _, ok := <-sysLogger.receivedChannel Expect(ok).To(BeTrue())
Expect(metrics).NotTo(BeNil()) metricsNameMap := make(map[string]int) for _, m := range metrics { metricsNameMap[m.Name]++ } }) It("emits event type metrics concatenated from the different unmarshallers", func() { collection.Run(inputChan, outputChan, waitGroup) for n := 0; n < 7; n++ { envelope := &events.Envelope{ Origin: proto.String("fake-origin-1"), EventType: events.Envelope_ValueMetric.Enum(), ValueMetric: factories.NewValueMetric("metricName", 1.0, "unit"), } message, _ := proto.Marshal(envelope) inputChan <- message } for n := 0; n < 7; n++ { <-outputChan } metrics := collection.Emit().Metrics Expect(metrics).NotTo(BeNil()) metricsNameMap := make(map[string]int)
"github.com/cloudfoundry/dropsonde/factories" "github.com/cloudfoundry/sonde-go/events" "github.com/gogo/protobuf/proto" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("EventEmitter", func() { Describe("Emit", func() { Context("without an origin", func() { It("returns an error", func() { innerEmitter := fake.NewFakeByteEmitter() eventEmitter := emitter.NewEventEmitter(innerEmitter, "") testEvent := factories.NewValueMetric("metric-name", 2.0, "metric-unit") err := eventEmitter.Emit(testEvent) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("Wrap: ")) }) }) It("marshals events and delegates to the inner emitter", func() { innerEmitter := fake.NewFakeByteEmitter() origin := "fake-origin" eventEmitter := emitter.NewEventEmitter(innerEmitter, origin) testEvent := factories.NewValueMetric("metric-name", 2.0, "metric-unit") err := eventEmitter.Emit(testEvent) Expect(err).ToNot(HaveOccurred())