Example #1
0
func initializeMetrics(batchIntervalMilliseconds uint) *metricbatcher.MetricBatcher {
	eventEmitter := dropsonde.AutowiredEmitter()
	metricSender := metric_sender.NewMetricSender(eventEmitter)
	metricBatcher := metricbatcher.New(metricSender, time.Duration(batchIntervalMilliseconds)*time.Millisecond)
	metrics.Initialize(metricSender, metricBatcher)
	return metricBatcher
}
Example #2
0
var _ = Describe("Autowire", func() {

	Describe("Initialize", func() {
		It("resets the HTTP default transport to be instrumented", func() {
			dropsonde.InitializeWithEmitter(&dropsonde.NullEventEmitter{})
			Expect(reflect.TypeOf(http.DefaultTransport).Elem().Name()).To(Equal("instrumentedCancelableRoundTripper"))
		})
	})

	Describe("CreateDefaultEmitter", func() {
		Context("with origin missing", func() {
			It("returns a NullEventEmitter", func() {
				err := dropsonde.Initialize("localhost:2343", "")
				Expect(err).To(HaveOccurred())

				emitter := dropsonde.AutowiredEmitter()
				Expect(emitter).ToNot(BeNil())
				nullEmitter := &dropsonde.NullEventEmitter{}
				Expect(emitter).To(BeAssignableToTypeOf(nullEmitter))
			})
		})
	})
})

type FakeHandler struct{}

func (fh FakeHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {}

type FakeRoundTripper struct{}

func (frt FakeRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	"net"
	"net/http"
	"strings"
	"sync"
	"time"
)

// these tests need to be invoked individually from an external script,
// since environment variables need to be set/unset before starting the tests
var _ = Describe("Autowire End-to-End", func() {
	Context("with standard initialization", func() {
		origin := []string{"test-origin"}

		BeforeEach(func() {
			dropsonde.Initialize("localhost:3457", origin...)
			metrics.Initialize(metric_sender.NewMetricSender(dropsonde.AutowiredEmitter()))
		})

		It("emits HTTP client/server events and heartbeats", func() {
			udpListener, err := net.ListenPacket("udp4", ":3457")
			Expect(err).ToNot(HaveOccurred())
			defer udpListener.Close()
			udpDataChan := make(chan []byte, 16)

			receivedEvents := make(map[string]bool)
			heartbeatUuidsChan := make(chan string, 1000)

			lock := sync.RWMutex{}
			heartbeatRequest := newHeartbeatRequest()
			marshalledHeartbeatRequest, _ := proto.Marshal(heartbeatRequest)
)

// these tests need to be invoked individually from an external script,
// since environment variables need to be set/unset before starting the tests
var _ = Describe("Autowire End-to-End", func() {
	Context("with standard initialization", func() {
		origin := []string{"test-origin"}

		BeforeEach(func() {
			var err error
			udpListener, err = net.ListenPacket("udp4", ":3457")
			Expect(err).ToNot(HaveOccurred())

			go listenForEvents(origin)
			dropsonde.Initialize("localhost:3457", origin...)
			sender := metric_sender.NewMetricSender(dropsonde.AutowiredEmitter())
			batcher := metricbatcher.New(sender, 100*time.Millisecond)
			metrics.Initialize(sender, batcher)
		})

		AfterEach(func() {
			udpListener.Close()
		})

		It("emits HTTP client/server events", func() {
			httpListener, err := net.Listen("tcp", "localhost:0")
			Expect(err).ToNot(HaveOccurred())
			defer httpListener.Close()
			httpHandler := dropsonde.InstrumentedHandler(FakeHandler{})
			go http.Serve(httpListener, httpHandler)
)

var _ = Describe("LogIntegration", func() {
	Context("with standard initialization", func() {
		origin := []string{"test-origin"}

		BeforeEach(func() {
			var err error
			logMessages = nil
			udpConn, err = net.ListenPacket("udp4", ":0")
			Expect(err).ToNot(HaveOccurred())

			go listenForLogs()
			udpAddr := udpConn.LocalAddr().(*net.UDPAddr)
			dropsonde.Initialize(fmt.Sprintf("localhost:%d", udpAddr.Port), origin...)
			sender := metric_sender.NewMetricSender(dropsonde.AutowiredEmitter())
			batcher := metricbatcher.New(sender, 100*time.Millisecond)
			metrics.Initialize(sender, batcher)
		})

		AfterEach(func() {
			udpConn.Close()
		})

		It("sends dropped error message for messages which are just under 64k and don't fit in UDP packet", func() {
			logSender := log_sender.NewLogSender(dropsonde.AutowiredEmitter(), time.Second, loggertesthelper.Logger())

			const length = 64*1024 - 1
			reader := strings.NewReader(strings.Repeat("s", length) + "\n")
			logSender.ScanErrorLogStream("someId", "app", "0", reader)