func (loggregatorClient *udpLoggregatorClient) Send(data []byte) {
	atomic.AddUint64(&loggregatorClient.receivedMessageCount, 1)
	atomic.AddUint64(&loggregatorClient.receivedByteCount, uint64(len(data)))

	metrics.IncrementCounter("receivedMessageCount")
	metrics.AddToCounter("receivedByteCount", uint64(len(data)))
	loggregatorClient.sendChannel <- data
	metrics.SendValue("currentBufferCount", float64(len(loggregatorClient.sendChannel)), "Msg")
}
func NewLoggregatorClient(loggregatorAddress string, logger generic_logger.GenericLogger, bufferSize int) LoggregatorClient {
	loggregatorClient := &udpLoggregatorClient{}

	la, err := net.ResolveUDPAddr("udp", loggregatorAddress)
	if err != nil {
		logger.Fatalf("Error resolving loggregator address %s, %s", loggregatorAddress, err)
	}

	connection, err := net.ListenPacket("udp", "")
	if err != nil {
		logger.Fatalf("Error opening udp stuff")
	}

	loggregatorClient.loggregatorAddress = la.IP.String()
	loggregatorClient.sendChannel = make(chan []byte, bufferSize)
	loggregatorClient.doneChannel = make(chan struct{})

	go func() {
		for dataToSend := range loggregatorClient.sendChannel {
			metrics.SendValue("currentBufferCount", float64(len(loggregatorClient.sendChannel)), "Msg")

			if len(dataToSend) > 0 {
				writeCount, err := connection.WriteTo(dataToSend, la)
				if err != nil {
					logger.Errorf("Writing to loggregator %s failed %s", loggregatorAddress, err)
					continue
				}
				logger.Debugf("Wrote %d bytes to %s", writeCount, loggregatorAddress)

				atomic.AddUint64(&loggregatorClient.sentMessageCount, 1)
				atomic.AddUint64(&loggregatorClient.sentByteCount, uint64(writeCount))

				metrics.IncrementCounter("sentMessageCount")
				metrics.AddToCounter("sentByteCount", uint64(writeCount))
			} else {
				logger.Debugf("Skipped writing of 0 byte message to %s", loggregatorAddress)
			}
		}
		close(loggregatorClient.doneChannel)
	}()

	return loggregatorClient
}
Example #3
0
		metrics.Initialize(fakeMetricSender)
	})

	It("delegates SendValue", func() {
		metrics.SendValue("metric", 42.42, "answers")

		Expect(fakeMetricSender.GetValue("metric").Value).To(Equal(42.42))
		Expect(fakeMetricSender.GetValue("metric").Unit).To(Equal("answers"))
	})

	It("delegates IncrementCounter", func() {
		metrics.IncrementCounter("count")

		Expect(fakeMetricSender.GetCounter("count")).To(BeEquivalentTo(1))

		metrics.IncrementCounter("count")

		Expect(fakeMetricSender.GetCounter("count")).To(BeEquivalentTo(2))
	})

	It("delegates AddToCounter", func() {
		metrics.AddToCounter("count", 5)

		Expect(fakeMetricSender.GetCounter("count")).To(BeEquivalentTo(5))

		metrics.AddToCounter("count", 10)

		Expect(fakeMetricSender.GetCounter("count")).To(BeEquivalentTo(15))
	})
})