Esempio n. 1
0
/*
Wraps the given http.Handler ServerHTTP function
Will provide accounting metrics for the http.Request / http.Response life-cycle
*/
func (ih *instrumentedHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	requestId, err := uuid.ParseHex(req.Header.Get("X-CF-RequestID"))
	if err != nil {
		requestId, err = GenerateUuid()
		if err != nil {
			log.Printf("failed to generated request ID: %v\n", err)
			requestId = &uuid.UUID{}
		}
		req.Header.Set("X-CF-RequestID", requestId.String())
	}
	rw.Header().Set("X-CF-RequestID", requestId.String())

	startEvent := factories.NewHttpStart(req, events.PeerType_Server, requestId)

	err = ih.emitter.Emit(startEvent)
	if err != nil {
		log.Printf("failed to emit start event: %v\n", err)
	}

	instrumentedWriter := &instrumentedResponseWriter{writer: rw, statusCode: 200}
	ih.handler.ServeHTTP(instrumentedWriter, req)

	stopEvent := factories.NewHttpStop(req, instrumentedWriter.statusCode, instrumentedWriter.contentLength, events.PeerType_Server, requestId)

	err = ih.emitter.Emit(stopEvent)
	if err != nil {
		log.Printf("failed to emit stop event: %v\n", err)
	}
}
/*
Wraps the RoundTrip function of the given RoundTripper.
Will provide accounting metrics for the http.Request / http.Response life-cycle
Callers of RoundTrip are responsible for setting the ‘X-CF-RequestID’ field in the request header if they have one.
Callers are also responsible for setting the ‘X-CF-ApplicationID’ and ‘X-CF-InstanceIndex’ fields in the request header if they are known.
*/
func (irt *instrumentedRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	requestId, err := GenerateUuid()
	if err != nil {
		log.Printf("failed to generated request ID: %v\n", err)
		requestId = &uuid.UUID{}
	}

	httpStart := factories.NewHttpStart(req, events.PeerType_Client, requestId)

	parentRequestId, err := uuid.ParseHex(req.Header.Get("X-CF-RequestID"))
	if err == nil {
		httpStart.ParentRequestId = factories.NewUUID(parentRequestId)
	}

	req.Header.Set("X-CF-RequestID", requestId.String())

	err = irt.emitter.Emit(httpStart)
	if err != nil {
		log.Printf("failed to emit start event: %v\n", err)
	}

	resp, roundTripErr := irt.roundTripper.RoundTrip(req)

	var httpStop *events.HttpStop
	if roundTripErr != nil {
		httpStop = factories.NewHttpStop(req, 0, 0, events.PeerType_Client, requestId)
	} else {
		httpStop = factories.NewHttpStop(req, resp.StatusCode, resp.ContentLength, events.PeerType_Client, requestId)
	}

	err = irt.emitter.Emit(httpStop)
	if err != nil {
		log.Printf("failed to emit stop event: %v\n", err)
	}

	return resp, roundTripErr
}
func createEnvelope(eventType events.Envelope_EventType) *events.Envelope {

	envelope := &events.Envelope{Origin: proto.String("origin"), EventType: &eventType, Timestamp: proto.Int64(time.Now().UnixNano())}

	switch eventType {
	case events.Envelope_HttpStart:
		req, _ := http.NewRequest("GET", "http://www.example.com", nil)
		req.RemoteAddr = "www.example.com"
		req.Header.Add("User-Agent", "user-agent")
		uuid, _ := uuid.NewV4()
		envelope.HttpStart = factories.NewHttpStart(req, events.PeerType_Client, uuid)
	case events.Envelope_HttpStop:
		req, _ := http.NewRequest("GET", "http://www.example.com", nil)
		uuid, _ := uuid.NewV4()
		envelope.HttpStop = factories.NewHttpStop(req, http.StatusOK, 128, events.PeerType_Client, uuid)
	case events.Envelope_HttpStartStop:
		req, _ := http.NewRequest("GET", "http://www.example.com", nil)
		req.RemoteAddr = "www.example.com"
		req.Header.Add("User-Agent", "user-agent")
		uuid, _ := uuid.NewV4()
		envelope.HttpStartStop = factories.NewHttpStartStop(req, http.StatusOK, 128, events.PeerType_Client, uuid)
	case events.Envelope_ValueMetric:
		envelope.ValueMetric = factories.NewValueMetric("some-value-metric", 123, "km")
	case events.Envelope_CounterEvent:
		envelope.CounterEvent = factories.NewCounterEvent("some-counter-event", 123)
	case events.Envelope_LogMessage:
		envelope.LogMessage = factories.NewLogMessage(events.LogMessage_OUT, "some message", "appId", "source")
	case events.Envelope_ContainerMetric:
		envelope.ContainerMetric = factories.NewContainerMetric("appID", 123, 1, 5, 5)
	case events.Envelope_Error:
		envelope.Error = factories.NewError("source", 123, "message")
	default:
		fmt.Printf("Unknown event %v\n", eventType)
		return nil
	}

	return envelope
}
					HttpStart: factories.NewHttpStart(request, events.PeerType_Client, uuid),
				}
				message, err := proto.Marshal(httpStart)
				Expect(err).ToNot(HaveOccurred())
				_, err = unmarshaller.UnmarshallMessage(message)
				Expect(err).ToNot(HaveOccurred())
				Eventually(func() uint64 { return fakeSender.GetCounter("dropsondeUnmarshaller.httpStartReceived") }).Should(BeEquivalentTo(1))
			})

			It("HTTP stop event", func() {
				uuid, _ := uuid.NewV4()
				request, _ := http.NewRequest("GET", "https://example.com", nil)
				httpStop := &events.Envelope{
					Origin:    proto.String("origin"),
					EventType: events.Envelope_HttpStop.Enum(),
					HttpStop:  factories.NewHttpStop(request, 200, 567, events.PeerType_Client, uuid),
				}
				message, err := proto.Marshal(httpStop)
				Expect(err).ToNot(HaveOccurred())
				_, err = unmarshaller.UnmarshallMessage(message)
				Expect(err).ToNot(HaveOccurred())
				Eventually(func() uint64 { return fakeSender.GetCounter("dropsondeUnmarshaller.httpStopReceived") }).Should(BeEquivalentTo(1))
			})

			It("CounterEvent", func() {
				counterEvent := &events.Envelope{
					Origin:       proto.String("origin"),
					EventType:    events.Envelope_CounterEvent.Enum(),
					CounterEvent: factories.NewCounterEvent("counter", 5),
				}
				message, err := proto.Marshal(counterEvent)
Esempio n. 5
0
	Describe("NewHttpStop", func() {
		BeforeEach(func() {
			req.Header.Set("X-CF-ApplicationID", applicationId.String())
		})

		It("should set appropriate fields", func() {
			expectedStopEvent := &events.HttpStop{
				ApplicationId: factories.NewUUID(applicationId),
				RequestId:     factories.NewUUID(requestId),
				Uri:           proto.String("foo.example.com/"),
				PeerType:      events.PeerType_Server.Enum(),
				StatusCode:    proto.Int32(200),
				ContentLength: proto.Int64(3),
			}

			stopEvent := factories.NewHttpStop(req, 200, 3, events.PeerType_Server, requestId)

			Expect(stopEvent.GetTimestamp()).ToNot(BeZero())
			stopEvent.Timestamp = nil

			Expect(stopEvent).To(Equal(expectedStopEvent))
		})
	})

	Describe("NewLogMessage", func() {
		It("should set appropriate fields", func() {
			expectedLogEvent := &events.LogMessage{
				Message:     []byte("hello"),
				AppId:       proto.String("app-id"),
				MessageType: events.LogMessage_OUT.Enum(),
				SourceType:  proto.String("App"),