. "github.com/onsi/gomega" ) 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 = instrumented_handler.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() { instrumented_handler.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()) })
// InstrumentedHandler returns a Handler pre-configured to emit HTTP server // request metrics to AutowiredEmitter. func InstrumentedHandler(handler http.Handler) http.Handler { return instrumented_handler.InstrumentedHandler(handler, DefaultEmitter) }
// InstrumentedHandler returns a Handler pre-configured to emit HTTP server // request metrics to AutowiredEmitter. func InstrumentedHandler(handler http.Handler) http.Handler { return instrumented_handler.InstrumentedHandler(handler, autowiredEmitter) }
uuid "github.com/nu7hatch/gouuid" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("Sending Http events through loggregator", func() { Context("When the instrumented handler receives a request", func() { It("should emit an HttpStartStop through the firehose", func() { msgChan, errorChan := helpers.ConnectToFirehose() udpEmitter, err := emitter.NewUdpEmitter(fmt.Sprintf("localhost:%d", config.DropsondePort)) Expect(err).ToNot(HaveOccurred()) emitter := emitter.NewEventEmitter(udpEmitter, helpers.ORIGIN_NAME) done := make(chan struct{}) handler := instrumented_handler.InstrumentedHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusTeapot) close(done) }), emitter) r, err := http.NewRequest("HEAD", "/", nil) Expect(err).ToNot(HaveOccurred()) r.Header.Add("User-Agent", "Spider-Man") w := httptest.NewRecorder() handler.ServeHTTP(w, r) Eventually(done).Should(BeClosed()) receivedEnvelope := helpers.FindMatchingEnvelope(msgChan) Expect(receivedEnvelope).NotTo(BeNil()) Expect(receivedEnvelope.GetEventType()).To(Equal(events.Envelope_HttpStartStop)) event := receivedEnvelope.GetHttpStartStop() Expect(event.GetPeerType().String()).To(Equal(events.PeerType_Server.Enum().String()))