func (d *DopplerForwarder) networkWrite(message *events.Envelope) {
	client, err := d.clientPool.RandomClient()
	if err != nil {
		d.logger.Errord(map[string]interface{}{
			"error": err.Error(),
		}, "DopplerForwarder: can't forward message")
		return
	}

	messageBytes, err := proto.Marshal(message)
	if err != nil {
		d.logger.Errorf("DopplerForwarder: marshal error %v", err)
		metrics.BatchIncrementCounter("dropsondeMarshaller.marshalErrors")
		return
	}

	switch client.Scheme() {
	case "udp":
		signedMessage := signature.SignMessage(messageBytes, d.sharedSecret)
		bytesWritten, err := client.Write(signedMessage)
		if err != nil {
			metrics.BatchIncrementCounter("udp.sendErrorCount")
			d.logger.Debugd(map[string]interface{}{
				"scheme":  client.Scheme(),
				"address": client.Address(),
			}, "Error writing legacy message")
			return
		}
		metrics.BatchIncrementCounter("udp.sentMessageCount")
		metrics.BatchAddCounter("udp.sentByteCount", uint64(bytesWritten))
	case "tls":
		var bytesWritten int
		err = binary.Write(client, binary.LittleEndian, uint32(len(messageBytes)))
		if err == nil {
			bytesWritten, err = client.Write(messageBytes)
		}

		if err != nil {
			metrics.BatchIncrementCounter("tls.retryCount")
			d.inputChan <- message
			client.Close()

			d.logger.Errord(map[string]interface{}{
				"scheme":  client.Scheme(),
				"address": client.Address(),
				"error":   err.Error(),
			}, "DopplerForwarder: streaming error")
			return
		}
		metrics.BatchIncrementCounter("tls.sentMessageCount")
		metrics.BatchAddCounter("tls.sentByteCount", uint64(bytesWritten+4))
	default:
		d.logger.Errorf("DopplerForwarder: unknown protocol, %s for %s", client.Scheme(), client.Address())
		return
	}

	d.incrementMessageCount(message.GetEventType())
	metrics.BatchIncrementCounter("DopplerForwarder.sentMessages")
}
Example #2
0
func (udp *udpListener) Start() {
	connection, err := net.ListenPacket("udp", udp.host)
	if err != nil {
		udp.Fatalf("Failed to listen on port. %s", err)
	}

	udp.Infof("Listening on port %s", udp.host)
	udp.Lock()
	udp.connection = connection
	udp.Unlock()

	messageCountMetricName := udp.contextName + ".receivedMessageCount"
	listenerTotalMetricName := "listeners.totalReceivedMessageCount"
	receivedByteCountMetricName := udp.contextName + ".receivedByteCount"

	readBuffer := make([]byte, 65535) //buffer with size = max theoretical UDP size
	defer close(udp.dataChannel)
	for {
		readCount, senderAddr, err := connection.ReadFrom(readBuffer)
		if err != nil {
			udp.Debugf("Error while reading: %s", err)
			return
		}
		udp.Debugf("AgentListener: Read %d bytes from address %s", readCount, senderAddr)

		readData := make([]byte, readCount) //pass on buffer in size only of read data
		copy(readData, readBuffer[:readCount])

		metrics.BatchIncrementCounter(messageCountMetricName)
		metrics.BatchIncrementCounter(listenerTotalMetricName)
		metrics.BatchAddCounter(receivedByteCountMetricName, uint64(readCount))

		udp.dataChannel <- readData
	}
}
Example #3
0
func (fms *FakeMetricSender) AddToCounter(name string, delta uint64) error {
	metrics.BatchAddCounter("bogus-counter", 1)
	if name == "count1" {
		close(*fms.done)
	}
	return nil
}
func (nr *NetworkReader) Start() {
	connection, err := net.ListenPacket("udp4", nr.host)
	if err != nil {
		nr.logger.Fatalf("Failed to listen on port. %s", err)
	}
	nr.logger.Infof("Listening on port %s", nr.host)
	nr.lock.Lock()
	nr.connection = connection
	nr.lock.Unlock()

	readBuffer := make([]byte, 65535) //buffer with size = max theoretical UDP size
	for {
		readCount, senderAddr, err := connection.ReadFrom(readBuffer)
		if err != nil {
			nr.logger.Debugf("Error while reading. %s", err)
			return
		}
		nr.logger.Debugf("NetworkReader: Read %d bytes from address %s", readCount, senderAddr)
		readData := make([]byte, readCount) //pass on buffer in size only of read data
		copy(readData, readBuffer[:readCount])

		atomic.AddUint64(&nr.receivedMessageCount, 1)
		atomic.AddUint64(&nr.receivedByteCount, uint64(readCount))
		metrics.BatchIncrementCounter(nr.contextName + ".receivedMessageCount")
		metrics.BatchAddCounter(nr.contextName+".receivedByteCount", uint64(readCount))
		nr.writer.Write(readData)
	}
}
Example #5
0
func (w *Writer) Write(msgBytes []byte) (int, error) {
	w.msgBufferLock.Lock()
	defer w.msgBufferLock.Unlock()

	prefixedBytes, err := w.prefixMessage(msgBytes)
	if err != nil {
		w.logger.Errorf("Error encoding message length: %v\n", err)
		metrics.BatchIncrementCounter("tls.sendErrorCount")
		return 0, err
	}
	switch {
	case w.msgBuffer.Len()+len(prefixedBytes) > w.msgBuffer.Cap():
		_, err := w.retryWrites(prefixedBytes)
		if err != nil {
			dropped := w.msgBuffer.messages + 1
			atomic.AddUint64(&w.droppedMessages, dropped)
			metrics.BatchAddCounter("MessageBuffer.droppedMessageCount", dropped)
			w.msgBuffer.Reset()

			w.msgBuffer.writeNonMessage(w.droppedLogMessage())
			w.timer.Reset(w.flushDuration)
			return 0, err
		}
		return len(msgBytes), nil
	default:
		if w.msgBuffer.Len() == 0 {
			w.timer.Reset(w.flushDuration)
		}
		_, err := w.msgBuffer.Write(prefixedBytes)
		return len(msgBytes), err
	}
}
func (agentListener *agentListener) Start() {
	connection, err := net.ListenPacket("udp", agentListener.host)
	if err != nil {
		agentListener.Fatalf("Failed to listen on port. %s", err)
	}

	agentListener.Infof("Listening on port %s", agentListener.host)
	agentListener.Lock()
	agentListener.connection = connection
	agentListener.Unlock()

	readBuffer := make([]byte, 65535) //buffer with size = max theoretical UDP size
	defer close(agentListener.dataChannel)
	for {
		readCount, senderAddr, err := connection.ReadFrom(readBuffer)
		if err != nil {
			agentListener.Debugf("Error while reading. %s", err)
			return
		}
		agentListener.Debugf("AgentListener: Read %d bytes from address %s", readCount, senderAddr)

		readData := make([]byte, readCount) //pass on buffer in size only of read data
		copy(readData, readBuffer[:readCount])

		metrics.BatchIncrementCounter(agentListener.contextName + ".receivedMessageCount")
		metrics.BatchAddCounter(agentListener.contextName+".receivedByteCount", uint64(readCount))

		agentListener.dataChannel <- readData
	}

}
Example #7
0
func (t *TCPListener) handleConnection(conn net.Conn) {
	defer conn.Close()
	defer t.removeConnection(conn)

	if tlsConn, ok := conn.(*tls.Conn); ok {
		if err := tlsConn.Handshake(); err != nil {
			t.logger.Warnd(map[string]interface{}{
				"error":   err.Error(),
				"address": conn.RemoteAddr().String(),
			}, "TLS handshake error")
			metrics.BatchIncrementCounter(t.receiveErrorCountMetricName)
			return
		}
	}

	var (
		n     uint32
		bytes []byte
		err   error
	)

	for {
		err = binary.Read(conn, binary.LittleEndian, &n)
		if err != nil {
			if err != io.EOF {
				metrics.BatchIncrementCounter(t.receiveErrorCountMetricName)
				t.logger.Errorf("Error while decoding: %v", err)
			}
			break
		}

		read := bytes
		if cap(bytes) < int(n) {
			bytes = make([]byte, int(n))
		}
		read = bytes[:n]

		_, err = io.ReadFull(conn, read)
		if err != nil {
			metrics.BatchIncrementCounter(t.receiveErrorCountMetricName)
			t.logger.Errorf("Error during i/o read: %v", err)
			break
		}

		envelope, err := t.unmarshaller.UnmarshallMessage(read)
		if err != nil {
			continue
		}
		metrics.BatchIncrementCounter(t.listenerTotalMetricName)
		metrics.BatchIncrementCounter(t.receivedMessageCountMetricName)
		metrics.BatchAddCounter(t.receivedByteCountMetricName, uint64(n+4))

		select {
		case t.envelopeChan <- envelope:
		case <-t.stopped:
			return
		}
	}
}
func (r *TruncatingBuffer) notifyMessagesDropped(outputChannel chan *events.Envelope, deltaDropped, totalDropped uint64, appId string) {
	metrics.BatchAddCounter("TruncatingBuffer.totalDroppedMessages", deltaDropped)
	if r.eventAllowed(events.Envelope_LogMessage) {
		r.emitMessage(outputChannel, generateLogMessage(deltaDropped, totalDropped, appId, r.context.Origin(), r.context.Destination()))
	}
	if r.eventAllowed(events.Envelope_CounterEvent) {
		r.emitMessage(outputChannel, generateCounterEvent(deltaDropped, totalDropped))
	}
}
func NewSinkManagerMetrics(sinkDropUpdateChannel <-chan int64) *SinkManagerMetrics {
	m := SinkManagerMetrics{
		sinkDropUpdateChannel: sinkDropUpdateChannel,
	}

	go func() {
		for delta := range m.sinkDropUpdateChannel {
			metrics.BatchAddCounter("messageRouter.totalDroppedMessages", uint64(delta))
		}
	}()

	return &m
}
Example #10
0
func (w *Wrapper) Write(client Client, message []byte) error {
	sentBytes, err := client.Write(message)

	if err != nil {
		w.logger.Errorf("Error writing to %s client %v\n", w.protocol, err)
		metrics.BatchIncrementCounter(w.protocol + ".sendErrorCount")
		client.Close()
		return err
	}

	metrics.BatchAddCounter(w.protocol+".sentByteCount", uint64(sentBytes))
	metrics.BatchIncrementCounter(w.protocol + ".sentMessageCount")

	return nil
}
Example #11
0
func (w *Wrapper) Write(client Client, message []byte, chainers ...metricbatcher.BatchCounterChainer) error {
	sentBytes, err := client.Write(message)

	if err != nil {
		w.logger.Errorf("Error writing to %s client %v\n", w.protocol, err)
		metrics.BatchIncrementCounter(w.protocol + ".sendErrorCount")
		client.Close()
		return err
	}

	metrics.BatchAddCounter(w.protocol+".sentByteCount", uint64(sentBytes))
	for _, chainer := range chainers {
		chainer.SetTag("protocol", w.protocol).Increment()
	}

	return nil
}
Example #12
0
func (u *UDPWrapper) Write(client Client, message []byte) error {
	signedMessage := signature.SignMessage(message, u.sharedSecret)

	sentLength, err := client.Write(signedMessage)
	if err != nil {
		u.logger.Errorf("Error writing to UDP client %v\n", err)
		metrics.BatchIncrementCounter("udp.sendErrorCount")
		return err
	}
	metrics.BatchIncrementCounter("udp.sentMessageCount")
	metrics.BatchAddCounter("udp.sentByteCount", uint64(sentLength))

	// The TLS side writes this metric in the batch.Writer.  For UDP,
	// it needs to be done here.
	metrics.BatchIncrementCounter("DopplerForwarder.sentMessages")
	return nil
}
func (t *TLSListener) handleConnection(conn net.Conn) {
	var n uint32
	var bytes []byte
	var err error

	for {
		err = binary.Read(conn, binary.LittleEndian, &n)
		if err != nil {
			break
		}

		read := bytes
		if cap(bytes) < int(n) {
			bytes = make([]byte, int(n))
		}
		read = bytes[:n]

		_, err = io.ReadFull(conn, read)
		if err != nil {
			break
		}

		envelope, err := t.unmarshaller.UnmarshallMessage(read)
		if err != nil {
			continue
		}

		metrics.BatchIncrementCounter(t.messageCountMetricName)
		metrics.BatchAddCounter(t.receivedByteCountMetricName, uint64(n+4))

		t.logger.Debugf("Received envelope: %#v", envelope)
		select {
		case t.envelopeChan <- envelope:
		case <-t.stopped:
			return
		}
	}

	t.logger.Debugf("Error while decoding: %v", err)
	conn.Close()
	t.removeConnection(conn)
}
func (nr *NetworkReader) Start() {
	receivedMessageCountName := nr.contextName + ".receivedMessageCount"
	receivedByteCountName := nr.contextName + ".receivedByteCount"

	readBuffer := make([]byte, 65535) //buffer with size = max theoretical UDP size
	for {
		readCount, senderAddr, err := nr.connection.ReadFrom(readBuffer)
		if err != nil {
			nr.logger.Errorf("Error while reading. %s", err)
			return
		}
		logging.Debugf(nr.logger, "NetworkReader: Read %d bytes from address %s", readCount, senderAddr)
		readData := make([]byte, readCount) //pass on buffer in size only of read data
		copy(readData, readBuffer[:readCount])

		metrics.BatchIncrementCounter(receivedMessageCountName)
		metrics.BatchAddCounter(receivedByteCountName, uint64(readCount))
		nr.writer.Write(readData)
	}
}
Example #15
0
func (w *Writer) flushWrite(bytes []byte) (int, error) {
	w.writerLock.Lock()
	defer w.writerLock.Unlock()

	toWrite := make([]byte, 0, w.msgBuffer.Len()+len(bytes))
	toWrite = append(toWrite, w.msgBuffer.Bytes()...)
	toWrite = append(toWrite, bytes...)

	bufferMessageCount := w.msgBuffer.messages
	if len(bytes) > 0 {
		bufferMessageCount++
	}
	sent, err := w.outWriter.Write(toWrite)
	if err != nil {
		w.logger.Warnf("Received error while trying to flush TCP bytes: %s", err)
		return 0, err
	}

	metrics.BatchAddCounter("DopplerForwarder.sentMessages", bufferMessageCount)
	atomic.StoreUint64(&w.droppedMessages, 0)
	w.msgBuffer.Reset()
	return sent, nil
}
func (s *SinkManagerMetrics) UpdateDroppedMessageCount(delta int64) {
	metrics.BatchAddCounter("messageRouter.totalDroppedMessages", uint64(delta))
}
Example #17
0
		time.Sleep(2 * time.Millisecond)
		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))
	})

	It("delegates BatchAddCounter", func() {
		metrics.BatchAddCounter("count", 3)
		time.Sleep(2 * time.Millisecond)
		Expect(fakeMetricSender.GetCounter("count")).To(BeEquivalentTo(3))

		metrics.BatchAddCounter("count", 7)
		time.Sleep(2 * time.Millisecond)
		Expect(fakeMetricSender.GetCounter("count")).To(BeEquivalentTo(10))
	})

	It("delegates SendContainerMetric", func() {
		appGuid := "some_app_guid"
		metrics.SendContainerMetric(appGuid, 7, 42.42, 1234, 123412341234)

		Expect(fakeMetricSender.GetContainerMetric(appGuid).ApplicationId).To(Equal(appGuid))
		Expect(fakeMetricSender.GetContainerMetric(appGuid).InstanceIndex).To(BeEquivalentTo(7))
		Expect(fakeMetricSender.GetContainerMetric(appGuid).CpuPercentage).To(BeEquivalentTo(42.42))