func newCounterAndMockWriter() (*batch.DroppedCounter, *mockBatchChainByteWriter) {
	byteWriter := newMockBatchChainByteWriter()
	conf := &config.Config{
		Deployment: "some-deployment",
		Job:        "some-job",
		Index:      "some-index",
	}
	counter := batch.NewDroppedCounter(byteWriter, newMockBatchCounterIncrementer(), origin, "some-ip", conf)

	return counter, byteWriter
}
Exemplo n.º 2
0
func initializeDopplerPool(conf *config.Config, batcher *metricbatcher.MetricBatcher, logger *gosteno.Logger) (*eventmarshaller.EventMarshaller, error) {
	adapter, err := storeAdapterProvider(conf)
	if err != nil {
		return nil, err
	}

	backoffStrategy := retrystrategy.Exponential()
	err = backoff.Connect(adapter, backoffStrategy, logger, connectionRetries)
	if err != nil {
		return nil, err
	}

	var protocols []string
	clientPool := make(map[string]clientreader.ClientPool)
	writers := make(map[string]eventmarshaller.BatchChainByteWriter)

	ip, err := localip.LocalIP()
	if err != nil {
		return nil, err
	}

	for protocol := range conf.Protocols {
		proto := string(protocol)
		protocols = append(protocols, proto)
		switch proto {
		case "udp":
			udpCreator := clientpool.NewUDPClientCreator(logger)
			udpWrapper := dopplerforwarder.NewUDPWrapper([]byte(conf.SharedSecret), logger)
			udpPool := clientpool.NewDopplerPool(logger, udpCreator)
			udpForwarder := dopplerforwarder.New(udpWrapper, udpPool, logger)
			clientPool[proto] = udpPool
			writers[proto] = udpForwarder
		case "tcp":
			tcpCreator := clientpool.NewTCPClientCreator(logger, nil)
			tcpWrapper := dopplerforwarder.NewWrapper(logger, proto)
			tcpPool := clientpool.NewDopplerPool(logger, tcpCreator)
			tcpForwarder := dopplerforwarder.New(tcpWrapper, tcpPool, logger)

			tcpBatchInterval := time.Duration(conf.TCPBatchIntervalMilliseconds) * time.Millisecond

			dropCounter := batch.NewDroppedCounter(tcpForwarder, batcher, origin, ip, conf)
			batchWriter, err := batch.NewWriter(
				"tcp",
				tcpForwarder,
				dropCounter,
				conf.TCPBatchSizeBytes,
				tcpBatchInterval,
				logger,
			)
			if err != nil {
				return nil, err
			}
			clientPool[proto] = tcpPool
			writers[proto] = batchWriter
		case "tls":
			c := conf.TLSConfig
			tlsConfig, err := listeners.NewTLSConfig(c.CertFile, c.KeyFile, c.CAFile)
			if err != nil {
				return nil, err
			}
			tlsConfig.ServerName = "doppler"
			tlsCreator := clientpool.NewTCPClientCreator(logger, tlsConfig)
			tlsWrapper := dopplerforwarder.NewWrapper(logger, proto)
			tlsPool := clientpool.NewDopplerPool(logger, tlsCreator)
			tlsForwarder := dopplerforwarder.New(tlsWrapper, tlsPool, logger)
			tcpBatchInterval := time.Duration(conf.TCPBatchIntervalMilliseconds) * time.Millisecond

			dropCounter := batch.NewDroppedCounter(tlsForwarder, batcher, origin, ip, conf)
			batchWriter, err := batch.NewWriter(
				"tls",
				tlsForwarder,
				dropCounter,
				conf.TCPBatchSizeBytes,
				tcpBatchInterval,
				logger,
			)
			if err != nil {
				return nil, err
			}
			clientPool[proto] = tlsPool
			writers[proto] = batchWriter
		}
	}

	finder := dopplerservice.NewFinder(adapter, conf.LoggregatorDropsondePort, conf.Protocols.Strings(), conf.Zone, logger)
	finder.Start()

	marshaller := eventmarshaller.New(batcher, logger)

	go func() {
		for {
			protocol := clientreader.Read(clientPool, conf.Protocols.Strings(), finder.Next())
			logger.Infof("Chose protocol %s from last etcd event, updating writer...", protocol)
			marshaller.SetWriter(writers[protocol])
		}
	}()

	return marshaller, nil
}
		Expect(actualCounter.GetOrigin()).To(Equal(origin))
		Expect(actualCounter.EventType).To(Equal(events.Envelope_CounterEvent.Enum()))
		Expect(actualCounter.CounterEvent.GetName()).To(ContainSubstring("DroppedCounter.droppedMessageCount"))
		Expect(actualCounter.CounterEvent.GetDelta()).To(BeNumerically("==", 5))

		actualLog := actualEnvelopes[1]
		Expect(actualLog.GetOrigin()).To(Equal(origin))
		Expect(actualLog.EventType).To(Equal(events.Envelope_LogMessage.Enum()))
		Expect(actualLog.LogMessage.Message).To(ContainSubstring("Dropped 5 message(s) from MetronAgent to Doppler"))
	})

	It("retries sending messages when sending errors", func() {
		incrementer := newMockBatchCounterIncrementer()
		byteWriter := newMockBatchChainByteWriter()
		counter := batch.NewDroppedCounter(byteWriter, incrementer, origin, "some-ip", new(config.Config))

		byteWriter.WriteOutput.SentLength <- 0
		byteWriter.WriteOutput.Err <- errors.New("boom")

		counter.Drop(20)

		Eventually(incrementer.BatchIncrementCounterInput).Should(BeCalled(
			With("droppedCounter.sendErrors"),
		))
		Expect(byteWriter.WriteInput.Message).To(Receive())

		var messages []byte
		Eventually(byteWriter.WriteInput.Message).Should(Receive(&messages))

		actualEnvelopes, err := unmarshalMessages(messages)