Exemplo n.º 1
0
func (r *RouteFetcher) deleteEndpoints(validRoutes []db.Route) {
	var diff []db.Route

	for _, curRoute := range r.endpoints {
		routeFound := false

		for _, validRoute := range validRoutes {
			if routeEquals(curRoute, validRoute) {
				routeFound = true
				break
			}
		}

		if !routeFound {
			diff = append(diff, curRoute)
			r.endpoints = r.endpoints
		}
	}

	for _, aRoute := range diff {
		r.RouteRegistry.Unregister(
			route.Uri(aRoute.Route),
			route.NewEndpoint(
				aRoute.LogGuid,
				aRoute.IP,
				uint16(aRoute.Port),
				aRoute.LogGuid,
				nil,
				aRoute.TTL,
				aRoute.RouteServiceUrl,
			))
	}
}
Exemplo n.º 2
0
func registerAddr(reg *registry.RouteRegistry, path string, routeServiceUrl string, addr net.Addr, instanceId string) {
	host, portStr, err := net.SplitHostPort(addr.String())
	Ω(err).NotTo(HaveOccurred())

	port, err := strconv.Atoi(portStr)
	Ω(err).NotTo(HaveOccurred())

	reg.Register(route.Uri(path), route.NewEndpoint("", host, uint16(port), instanceId, nil, -1, routeServiceUrl))
}
Exemplo n.º 3
0
func (e *EtcdEndpoint) MakeRouteEndPoints(eps ...EndPoint) []*route.Endpoint {

	var routeEPs []*route.Endpoint
	for _, ep := range eps {
		routeEP := route.NewEndpoint(e.Service, ep.Host, uint16(ep.Port), ep.Host, nil, 10, "")
		routeEPs = append(routeEPs, routeEP)
	}

	return routeEPs
}
Exemplo n.º 4
0
func (r *RouteFetcher) HandleEvent(e routing_api.Event) error {
	r.logger.Infof("Handling event: %v", e)
	eventRoute := e.Route
	uri := route.Uri(eventRoute.Route)
	endpoint := route.NewEndpoint(eventRoute.LogGuid, eventRoute.IP, uint16(eventRoute.Port), eventRoute.LogGuid, nil, eventRoute.TTL, eventRoute.RouteServiceUrl)
	switch e.Action {
	case "Delete":
		r.RouteRegistry.Unregister(uri, endpoint)
	case "Upsert":
		r.RouteRegistry.Register(uri, endpoint)
	}

	r.logger.Infof("Successfully handled event: %v", e)
	return nil
}
Exemplo n.º 5
0
func (r *RouteFetcher) refreshEndpoints(validRoutes []db.Route) {
	r.deleteEndpoints(validRoutes)

	r.endpoints = validRoutes

	for _, aRoute := range r.endpoints {
		r.RouteRegistry.Register(
			route.Uri(aRoute.Route),
			route.NewEndpoint(
				aRoute.LogGuid,
				aRoute.IP,
				uint16(aRoute.Port),
				aRoute.LogGuid,
				nil,
				aRoute.TTL,
				aRoute.RouteServiceUrl,
			))
	}
}
func CreateAccessLogRecord() *AccessLogRecord {
	u, err := url.Parse("http://foo.bar:1234/quz?wat")
	if err != nil {
		panic(err)
	}

	req := &http.Request{
		Method:     "GET",
		URL:        u,
		Proto:      "HTTP/1.1",
		Header:     make(http.Header),
		Host:       "foo.bar",
		RemoteAddr: "1.2.3.4:5678",
	}

	req.Header.Set("Referer", "referer")
	req.Header.Set("User-Agent", "user-agent")

	res := &http.Response{
		StatusCode: http.StatusOK,
	}

	b := route.NewEndpoint("my_awesome_id", "127.0.0.1", 4567, "", nil, -1, "")

	r := AccessLogRecord{
		Request:       req,
		StatusCode:    res.StatusCode,
		RouteEndpoint: b,
		StartedAt:     time.Unix(10, 100000000),
		FirstByteAt:   time.Unix(10, 200000000),
		FinishedAt:    time.Unix(10, 300000000),
		BodyBytesSent: 42,
	}

	return &r
}
Exemplo n.º 7
0
	})

	It("reports seconds since last registry update", func() {
		Registry.Register("foo", &route.Endpoint{})

		time.Sleep(10 * time.Millisecond)

		timeSince := findValue(Varz, "ms_since_last_registry_update").(float64)
		Expect(timeSince).To(BeNumerically("<", 1000))
		Expect(timeSince).To(BeNumerically(">=", 10))
	})

	It("has urls", func() {
		Expect(findValue(Varz, "urls")).To(Equal(float64(0)))

		var fooReg = route.NewEndpoint("12345", "192.168.1.1", 1234, "", map[string]string{}, -1, "")

		// Add a route
		Registry.Register("foo.vcap.me", fooReg)
		Registry.Register("fooo.vcap.me", fooReg)

		Expect(findValue(Varz, "urls")).To(Equal(float64(2)))
	})

	It("updates bad requests", func() {
		r := http.Request{}

		Varz.CaptureBadRequest(&r)
		Expect(findValue(Varz, "bad_requests")).To(Equal(float64(1)))

		Varz.CaptureBadRequest(&r)
Exemplo n.º 8
0
		r.Insert("/foo/bar/baz", p1)
		r.Insert("/foo/bar", p2)

		Expect(r.PoolCount()).To(Equal(2))
	})

	Describe(".PruneDeadLeaves", func() {
		It("removes dead leaves", func() {
			segments := make([]string, 0)
			count := 0
			f := func(r *Trie) {
				segments = append(segments, r.Segment)
				count += 1
			}

			e1 := route.NewEndpoint("", "192.168.1.1", 1234, "", nil, -1, "")
			e2 := route.NewEndpoint("", "192.168.1.1", 4321, "", nil, -1, "")
			p1 := route.NewPool(42, "")
			p2 := route.NewPool(42, "")
			p3 := route.NewPool(42, "")
			p4 := route.NewPool(42, "")
			p1.Put(e1)
			p2.Put(e2)
			r.Insert("/foo", p1)
			r.Insert("/foo/bar/baz", p2)
			r.Insert("/zoo", p3)
			r.Insert("/foo/bar/zak", p4)

			r.EachNodeWithPool(f)
			Expect(segments).To(ConsistOf("foo", "baz", "zak", "zoo"))
			Expect(count).To(Equal(4))
Exemplo n.º 9
0
func (rm *RegistryMessage) makeEndpoint() *route.Endpoint {
	return route.NewEndpoint(rm.App, rm.Host, rm.Port, rm.PrivateInstanceId, rm.Tags, rm.StaleThresholdInSeconds, rm.RouteServiceUrl)
}
Exemplo n.º 10
0
	var fooEndpoint, barEndpoint, bar2Endpoint *route.Endpoint
	var configObj *config.Config

	BeforeEach(func() {
		configObj = config.DefaultConfig()
		configObj.PruneStaleDropletsInterval = 50 * time.Millisecond
		configObj.DropletStaleThreshold = 10 * time.Millisecond

		messageBus = fakeyagnats.Connect()
		reporter = new(fakes.FakeRouteReporter)

		r = NewRouteRegistry(configObj, messageBus, reporter)
		fooEndpoint = route.NewEndpoint("12345", "192.168.1.1", 1234,
			"id1", map[string]string{
				"runtime":   "ruby18",
				"framework": "sinatra",
			}, -1, "")

		barEndpoint = route.NewEndpoint("54321", "192.168.1.2", 4321,
			"id2", map[string]string{
				"runtime":   "javascript",
				"framework": "node",
			}, -1, "https://my-rs.com")

		bar2Endpoint = route.NewEndpoint("54321", "192.168.1.3", 1234,
			"id3", map[string]string{
				"runtime":   "javascript",
				"framework": "node",
			}, -1, "")
	})
Exemplo n.º 11
0
	. "github.com/onsi/gomega"
	"net/http"
	"time"
	"vulcan/router/route"
)

var _ = Describe("MetricsReporter", func() {
	var metricsReporter *metrics.MetricsReporter
	var req *http.Request
	var endpoint *route.Endpoint
	var sender *fake.FakeMetricSender

	BeforeEach(func() {
		metricsReporter = metrics.NewMetricsReporter()
		req, _ = http.NewRequest("GET", "https://example.com", nil)
		endpoint = route.NewEndpoint("someId", "host", 2222, "privateId", map[string]string{}, 30, "")
		sender = fake.NewFakeMetricSender()
		batcher := metricbatcher.New(sender, time.Millisecond)
		dropsondeMetrics.Initialize(sender, batcher)
	})

	It("increments the bad_requests metric", func() {
		metricsReporter.CaptureBadRequest(req)
		Eventually(func() uint64 { return sender.GetCounter("rejected_requests") }).Should(BeEquivalentTo(1))

		metricsReporter.CaptureBadRequest(req)
		Eventually(func() uint64 { return sender.GetCounter("rejected_requests") }).Should(BeEquivalentTo(2))
	})

	It("increments the bad_gateway metric", func() {
		metricsReporter.CaptureBadGateway(req)
			Expect(fakeLogSender.GetLogs()[0].Message).To(MatchRegexp("^.*foo.bar.*\n"))
			Expect(fakeLogSender.GetLogs()[0].SourceType).To(Equal("RTR"))
			Expect(fakeLogSender.GetLogs()[0].SourceInstance).To(Equal("42"))
			Expect(fakeLogSender.GetLogs()[0].MessageType).To(Equal("OUT"))

			accessLogger.Stop()
		})

		It("a record with no app id is not logged to dropsonde", func() {

			fakeLogSender := fake.NewFakeLogSender()
			logs.Initialize(fakeLogSender)

			accessLogger := NewFileAndLoggregatorAccessLogger(nil, "43")

			routeEndpoint := route.NewEndpoint("", "127.0.0.1", 4567, "", nil, -1, "")

			accessLogRecord := CreateAccessLogRecord()
			accessLogRecord.RouteEndpoint = routeEndpoint
			accessLogger.Log(*accessLogRecord)
			go accessLogger.Run()

			Consistently(fakeLogSender.GetLogs).Should(HaveLen(0))

			accessLogger.Stop()
		})

	})

	Context("with a file", func() {
		It("writes to the log file", func() {
Exemplo n.º 13
0
var _ = Describe("AccessLogRecord", func() {
	Measure("Register", func(b Benchmarker) {
		c := config.DefaultConfig()
		mbus := fakeyagnats.Connect()
		r := registry.NewRouteRegistry(c, mbus, new(fakes.FakeRouteReporter))

		accesslog, err := access_log.CreateRunningAccessLogger(c)
		Expect(err).ToNot(HaveOccurred())

		proxy.NewProxy(proxy.ProxyArgs{
			EndpointTimeout: c.EndpointTimeout,
			Ip:              c.Ip,
			TraceKey:        c.TraceKey,
			Registry:        r,
			Reporter:        varz.NewVarz(r),
			AccessLogger:    accesslog,
		})

		b.Time("RegisterTime", func() {
			for i := 0; i < 1000; i++ {
				str := strconv.Itoa(i)
				r.Register(
					route.Uri("bench.vcap.me."+str),
					route.NewEndpoint("", "localhost", uint16(i), "", nil, -1, ""),
				)
			}
		})
	}, 10)

})
Exemplo n.º 14
0
			client.RoutesReturns(response, nil)

			err := fetcher.FetchRoutes()
			Expect(err).ToNot(HaveOccurred())

			Expect(registry.RegisterCallCount()).To(Equal(3))

			for i := 0; i < 3; i++ {
				expectedRoute := response[i]
				uri, endpoint := registry.RegisterArgsForCall(i)
				Expect(uri).To(Equal(route.Uri(expectedRoute.Route)))
				Expect(endpoint).To(Equal(
					route.NewEndpoint(expectedRoute.LogGuid,
						expectedRoute.IP, uint16(expectedRoute.Port),
						expectedRoute.LogGuid,
						nil,
						expectedRoute.TTL,
						expectedRoute.RouteServiceUrl,
					)))
			}
		})

		It("removes unregistered routes", func() {
			secondResponse := []db.Route{
				response[0],
			}

			client.RoutesReturns(response, nil)

			err := fetcher.FetchRoutes()
			Expect(err).ToNot(HaveOccurred())