func init() {
	metricNames = make(map[events.Envelope_EventType]string)
	for eventType, eventName := range events.Envelope_EventType_name {
		r, n := utf8.DecodeRuneInString(eventName)
		modifiedName := string(unicode.ToLower(r)) + eventName[n:]
		metricName := "dropsondeMarshaller." + modifiedName + "Marshalled"
		metricNames[events.Envelope_EventType(eventType)] = metricName
	}
}
Exemple #2
0
// NewDropsondeMarshaller instantiates a DropsondeMarshaller and logs to the
// provided logger.
func NewDropsondeMarshaller(logger *gosteno.Logger) DropsondeMarshaller {
	messageCounts := make(map[events.Envelope_EventType]*uint64)
	for key := range events.Envelope_EventType_name {
		var count uint64
		messageCounts[events.Envelope_EventType(key)] = &count
	}
	return &dropsondeMarshaller{
		logger:        logger,
		messageCounts: messageCounts,
	}
}
// New instantiates a EventMarshaller and logs to the provided logger.
func New(outputWriter writers.ByteArrayWriter, logger *gosteno.Logger) *EventMarshaller {
	messageCounts := make(map[events.Envelope_EventType]*uint64)
	for key := range events.Envelope_EventType_name {
		var count uint64
		messageCounts[events.Envelope_EventType(key)] = &count
	}
	return &EventMarshaller{
		logger:        logger,
		outputWriter:  outputWriter,
		messageCounts: messageCounts,
	}
}
// New instantiates a EventUnmarshaller and logs to the
// provided logger.
func New(outputWriter writers.EnvelopeWriter, logger *gosteno.Logger) *EventUnmarshaller {
	receiveCounts := make(map[events.Envelope_EventType]*uint64)
	for key := range events.Envelope_EventType_name {
		var count uint64
		receiveCounts[events.Envelope_EventType(key)] = &count
	}

	return &EventUnmarshaller{
		logger:        logger,
		outputWriter:  outputWriter,
		receiveCounts: receiveCounts,
	}
}
// NewDropsondeUnmarshaller instantiates a DropsondeUnmarshaller and logs to the
// provided logger.
func NewDropsondeUnmarshaller(logger *gosteno.Logger) DropsondeUnmarshaller {
	receiveCounts := make(map[events.Envelope_EventType]*uint64)
	for key := range events.Envelope_EventType_name {
		var count uint64
		receiveCounts[events.Envelope_EventType(key)] = &count
	}

	return &dropsondeUnmarshaller{
		logger:                  logger,
		receiveCounts:           receiveCounts,
		logMessageReceiveCounts: make(map[string]*uint64),
	}
}
func (m *EventMarshaller) metrics() []instrumentation.Metric {
	var metrics []instrumentation.Metric

	for eventType, eventName := range events.Envelope_EventType_name {
		modifiedEventName := []rune(eventName)
		modifiedEventName[0] = unicode.ToLower(modifiedEventName[0])
		metricName := string(modifiedEventName) + "Marshalled"

		metricValue := atomic.LoadUint64(m.messageCounts[events.Envelope_EventType(eventType)])
		metrics = append(metrics, instrumentation.Metric{Name: metricName, Value: metricValue})
	}

	metrics = append(metrics, instrumentation.Metric{
		Name:  "marshalErrors",
		Value: atomic.LoadUint64(&m.marshalErrorCount),
	})

	return metrics
}
		go tlsListener.Start()
	})

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

	Context("dropsonde metric emission", func() {
		BeforeEach(func() {
			fakeEventEmitter.Reset()
			metricBatcher.Reset()
		})

		It("sends all types of messages as a protobuf", func() {
			for name, eventType := range events.Envelope_EventType_value {
				envelope := createEnvelope(events.Envelope_EventType(eventType))
				conn := openTLSConnection(tlsListener.Address())

				err := send(conn, envelope)
				Expect(err).ToNot(HaveOccurred())

				Eventually(envelopeChan).Should(Receive(Equal(envelope)), fmt.Sprintf("did not receive expected event: %s", name))
				conn.Close()
			}
		})

		It("sends all types of messages over multiple connections", func() {
			for _, eventType := range events.Envelope_EventType_value {
				envelope1 := createEnvelope(events.Envelope_EventType(eventType))
				conn1 := openTLSConnection(tlsListener.Address())
		It("unmarshalls bytes", func() {
			output, _ := unmarshaller.UnmarshallMessage(message)

			Expect(output).To(Equal(event))
		})

		It("handles bad input gracefully", func() {
			output, err := unmarshaller.UnmarshallMessage(make([]byte, 4))
			Expect(output).To(BeNil())
			Expect(err).To(HaveOccurred())
		})

		It("doesn't write unknown event types", func() {
			unknownEventTypeMessage := &events.Envelope{
				Origin:    proto.String("fake-origin-2"),
				EventType: events.Envelope_EventType(2000).Enum(),
				ValueMetric: &events.ValueMetric{
					Name:  proto.String("fake-metric-name"),
					Value: proto.Float64(42),
					Unit:  proto.String("fake-unit"),
				},
			}
			message, err := proto.Marshal(unknownEventTypeMessage)
			Expect(err).ToNot(HaveOccurred())

			output, err := unmarshaller.UnmarshallMessage(message)
			Expect(output).To(BeNil())
			Expect(err).To(HaveOccurred())
		})
	})
	. "github.com/onsi/gomega"
)

var _ = Describe("BufferContext", func() {
	Context("DefaultContext", func() {
		var defaultContext *DefaultContext

		BeforeEach(func() {
			defaultContext = NewDefaultContext("origin", "testIdentifier")
		})

		It("Should return a valid properties", func() {
			Expect(defaultContext.Origin()).To(Equal("origin"))
			Expect(defaultContext.Destination()).To(Equal("testIdentifier"))
			for _, event := range events.Envelope_EventType_value {
				Expect(defaultContext.EventAllowed(events.Envelope_EventType(event))).To(BeTrue())
			}
			message := factories.NewLogMessage(events.LogMessage_OUT, "hello", "appID", "source")
			envelope := &events.Envelope{
				Origin:     proto.String("origin"),
				EventType:  events.Envelope_LogMessage.Enum(),
				LogMessage: message,
			}
			Expect(defaultContext.AppID(envelope)).To(Equal("appID"))
		})

	})

	Context("LogAllowedContext", func() {
		var logAllowedContext *LogAllowedContext
				Origin:    proto.String("MetronAgent"),
				EventType: events.Envelope_CounterEvent.Enum(),
				CounterEvent: &events.CounterEvent{
					Name:  proto.String("dropsondeUnmarshaller.logMessageTotal"),
					Delta: proto.Uint64(1),
					Total: proto.Uint64(1),
				},
			}

			Eventually(testDoppler.MessageChan, 2).Should(Receive(MatchSpecifiedContents(&expected)))
		})

		It("counts unknown event types", func() {
			metronInput, _ := net.Dial("udp4", "localhost:51161")
			message := basicValueMessageEnvelope()
			message.EventType = events.Envelope_EventType(2000).Enum()
			bytes, err := proto.Marshal(message)
			Expect(err).ToNot(HaveOccurred())

			metronInput.Write(bytes)

			message = basicValueMessageEnvelope()
			badEventType := events.Envelope_EventType(1000)
			message.EventType = &badEventType
			bytes, err = proto.Marshal(message)
			Expect(err).ToNot(HaveOccurred())

			metronInput.Write(bytes)

			expected := events.Envelope{
				Origin:    proto.String("MetronAgent"),
		})

		It("emits a value metric counter", func() {
			envelope := &events.Envelope{
				Origin:      proto.String("fake-origin-3"),
				EventType:   events.Envelope_ValueMetric.Enum(),
				ValueMetric: factories.NewValueMetric("value-name", 1.0, "units"),
			}

			inputChan <- envelope

			eventuallyExpectCounter("dropsondeMarshaller.valueMetricReceived", 1)
		})

		It("counts unknown message types", func() {
			unexpectedMessageType := events.Envelope_EventType(1)
			envelope1 := &events.Envelope{
				Origin:     proto.String("fake-origin-3"),
				EventType:  &unexpectedMessageType,
				LogMessage: factories.NewLogMessage(events.LogMessage_OUT, "test log message 1", "fake-app-id-1", "DEA"),
			}

			inputChan <- envelope1

			eventuallyExpectCounter("dropsondeMarshaller.unknownEventTypeReceived", 1)
		})

		Context("when a http start stop message is received", func() {
			It("emits a counter message with a delta value of 1", func() {
				envelope := &events.Envelope{
					Origin:        proto.String("fake-origin-1"),
Exemple #12
0
func (e *eventTypeValue) String() string {
	return events.Envelope_EventType(*e).String()
}