Example #1
0
func (r *Router) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
	r.registry.StartPruningCycle()

	r.RegisterComponent()

	// Subscribe register/unregister router
	r.SubscribeRegister()
	r.HandleGreetings()
	r.SubscribeUnregister()

	// Kickstart sending start messages
	r.SendStartMessage()

	r.mbusClient.AddReconnectedCB(func(conn *nats.Conn) {
		r.logger.Info(fmt.Sprintf("Reconnecting to NATS server %s...", conn.Opts.Url))
		r.SendStartMessage()
	})

	// Schedule flushing active app's app_id
	r.ScheduleFlushApps()

	// Wait for one start message send interval, such that the router's registry
	// can be populated before serving requests.
	if r.config.StartResponseDelayInterval != 0 {
		r.logger.Info(fmt.Sprintf("Waiting %s before listening...", r.config.StartResponseDelayInterval))
		time.Sleep(r.config.StartResponseDelayInterval)
	}

	handler := gorouterHandler{handler: dropsonde.InstrumentedHandler(r.proxy), logger: r.logger}

	server := &http.Server{
		Handler:   &handler,
		ConnState: r.HandleConnState,
	}

	err := r.serveHTTP(server, r.errChan)
	if err != nil {
		r.errChan <- err
		return err
	}
	err = r.serveHTTPS(server, r.errChan)
	if err != nil {
		r.errChan <- err
		return err
	}

	// create pid file
	err = r.writePidFile(r.config.PidFile)
	if err != nil {
		return err
	}

	r.logger.Info("gorouter.started")

	close(ready)

	r.OnErrOrSignal(signals, r.errChan)

	return nil
}
Example #2
0
File: handlers.go Project: Gerg/bbs
func logWrap(loggable loggableHandler, logger lager.Logger) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		requestLog := logger.Session("request", lager.Data{
			"method":  r.Method,
			"request": r.URL.String(),
		})

		handler := dropsonde.InstrumentedHandler(loggable.WithLogger(requestLog))

		requestLog.Info("serving")
		handler.ServeHTTP(w, r)
		requestLog.Info("done")
	}
}
Example #3
0
func LogWrap(handler http.Handler, logger lager.Logger) http.HandlerFunc {
	handler = dropsonde.InstrumentedHandler(handler)

	return func(w http.ResponseWriter, r *http.Request) {
		requestLog := logger.Session("request", lager.Data{
			"method":  r.Method,
			"request": r.URL.String(),
		})

		requestLog.Info("serving", lager.Data{"request-headers": filter(r.Header)})
		handler.ServeHTTP(w, r)
		requestLog.Info("done", lager.Data{"response-headers": w.Header()})
	}
}
Example #4
0
func (r *Router) Run() <-chan error {
	r.registry.StartPruningCycle()

	r.RegisterComponent()

	// Subscribe register/unregister router
	r.SubscribeRegister()
	r.HandleGreetings()
	r.SubscribeUnregister()

	// Kickstart sending start messages
	r.SendStartMessage()

	r.mbusClient.AddReconnectedCB(func(conn *nats.Conn) {
		r.logger.Infof("Reconnecting to NATS server %s...", conn.Opts.Url)
		r.SendStartMessage()
	})

	// Schedule flushing active app's app_id
	r.ScheduleFlushApps()

	// Wait for one start message send interval, such that the router's registry
	// can be populated before serving requests.
	if r.config.StartResponseDelayInterval != 0 {
		r.logger.Infof("Waiting %s before listening...", r.config.StartResponseDelayInterval)
		time.Sleep(r.config.StartResponseDelayInterval)
	}

	server := &http.Server{
		Handler:   dropsonde.InstrumentedHandler(r.proxy),
		ConnState: r.HandleConnState,
	}

	errChan := make(chan error, 2)

	err := r.serveHTTP(server, errChan)
	if err != nil {
		errChan <- err
		return errChan
	}
	err = r.serveHTTPS(server, errChan)
	if err != nil {
		errChan <- err
		return errChan
	}

	return errChan
}
Example #5
0
func (r *Router) Run() <-chan error {

	r.RegisterAllEndpoint()
	// Subscribe register/unregister router
	go r.UpdateEndpoints()

	// Kickstart sending start messages
	//r.SendStartMessage()

	// Schedule flushing active app's app_id
	r.ScheduleFlushApps()

	// Wait for one start message send interval, such that the router's registry
	// can be populated before serving requests.
	if r.config.StartResponseDelayInterval != 0 {
		r.logger.Infof("Waiting %s before listening...", r.config.StartResponseDelayInterval)
		time.Sleep(r.config.StartResponseDelayInterval)
	}

	server := &http.Server{
		Handler:   dropsonde.InstrumentedHandler(r.proxy),
		ConnState: r.HandleConnState,
	}

	errChan := make(chan error, 2)

	err := r.serveHTTP(server, errChan)
	if err != nil {
		errChan <- err
		return errChan
	}
	err = r.serveHTTPS(server, errChan)
	if err != nil {
		errChan <- err
		return errChan
	}

	return errChan
}
					if envelope.GetOrigin() != strings.Join(origin, "/") {
						panic("origin not as expected")
					}

					func() {
						lock.Lock()
						defer lock.Unlock()
						receivedEvents[eventId] = true
					}()
				}
			}()

			httpListener, err := net.Listen("tcp", "localhost:0")
			Expect(err).ToNot(HaveOccurred())
			defer httpListener.Close()
			httpHandler := dropsonde.InstrumentedHandler(FakeHandler{})
			go http.Serve(httpListener, httpHandler)

			_, err = http.Get("http://" + httpListener.Addr().String())
			Expect(err).ToNot(HaveOccurred())

			metrics.SendValue("TestMetric", 0, "")
			metrics.IncrementCounter("TestIncrementCounter")

			expectedEventTypes := []string{"HttpStartClient", "HttpStartServer", "HttpStopServer", "HttpStopClient", "ValueMetricnumCPUS", "ValueMetricTestMetric", "CounterEventTestIncrementCounter"}

			for _, eventType := range expectedEventTypes {
				Eventually(func() bool {
					lock.RLock()
					defer lock.RUnlock()
					_, ok := receivedEvents[eventType]
	rw.WriteHeader(123)
}

var _ = Describe("InstrumentedHandler", func() {
	var fakeEmitter *fake.FakeEventEmitter
	var h http.Handler
	var req *http.Request

	var origin = "testHandler/41"

	BeforeEach(func() {
		fakeEmitter = fake.NewFakeEventEmitter(origin)

		var err error
		fh := FakeHandler{}
		h = dropsonde.InstrumentedHandler(fh, fakeEmitter)
		req, err = http.NewRequest("GET", "http://foo.example.com/", nil)
		Expect(err).ToNot(HaveOccurred())
		req.RemoteAddr = "127.0.0.1"
		req.Header.Set("User-Agent", "our-testing-client")
	})

	AfterEach(func() {
		dropsonde.GenerateUuid = uuid.NewV4
	})

	Describe("request ID", func() {
		It("should add it to the request", func() {
			h.ServeHTTP(httptest.NewRecorder(), req)
			Expect(req.Header.Get("X-CF-RequestID")).ToNot(BeEmpty())
		})
Example #8
0
// InstrumentedHandler returns a Handler pre-configured to emit HTTP server
// request metrics to autowire's Emitter.
func InstrumentedHandler(handler http.Handler) http.Handler {
	return dropsonde.InstrumentedHandler(handler, autowiredEmitter)
}
Example #9
0
func (r *Router) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
	r.registry.StartPruningCycle()

	r.RegisterComponent()

	// Schedule flushing active app's app_id
	r.ScheduleFlushApps()

	lbOKDelay := r.config.StartResponseDelayInterval - r.config.LoadBalancerHealthyThreshold

	totalWaitDelay := r.config.LoadBalancerHealthyThreshold
	if lbOKDelay > 0 {
		totalWaitDelay = r.config.StartResponseDelayInterval
	}

	r.logger.Info(fmt.Sprintf("Waiting %s before listening...", totalWaitDelay),
		lager.Data{"route_registration_interval": r.config.StartResponseDelayInterval.String(),
			"load_balancer_healthy_threshold": r.config.LoadBalancerHealthyThreshold.String()})

	if lbOKDelay > 0 {
		r.logger.Debug(fmt.Sprintf("Sleeping for %d, before enabling /health endpoint", lbOKDelay))
		time.Sleep(lbOKDelay)
	}

	atomic.StoreInt32(r.HeartbeatOK, 1)
	r.logger.Debug("Gorouter reporting healthy")
	time.Sleep(r.config.LoadBalancerHealthyThreshold)

	r.logger.Info("completed-wait")

	handler := gorouterHandler{handler: dropsonde.InstrumentedHandler(r.proxy), logger: r.logger}

	server := &http.Server{
		Handler:   &handler,
		ConnState: r.HandleConnState,
	}

	err := r.serveHTTP(server, r.errChan)
	if err != nil {
		r.errChan <- err
		return err
	}
	err = r.serveHTTPS(server, r.errChan)
	if err != nil {
		r.errChan <- err
		return err
	}

	// create pid file
	err = r.writePidFile(r.config.PidFile)
	if err != nil {
		return err
	}

	r.logger.Info("gorouter.started")
	go r.uptimeMonitor.Start()

	close(ready)

	r.OnErrOrSignal(signals, r.errChan)

	return nil
}