/* 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) } }
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 }
/* 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 }
} message, err := proto.Marshal(containerMetric) Expect(err).ToNot(HaveOccurred()) _, err = unmarshaller.UnmarshallMessage(message) Expect(err).ToNot(HaveOccurred()) Eventually(func() uint64 { return fakeSender.GetCounter("dropsondeUnmarshaller.containerMetricReceived") }).Should(BeEquivalentTo(1)) }) It("HTTP start event", func() { uuid, _ := uuid.NewV4() request, _ := http.NewRequest("GET", "https://example.com", nil) httpStart := &events.Envelope{ Origin: proto.String("origin"), EventType: events.Envelope_HttpStart.Enum(), 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),
Describe("NewHttpStart", func() { Context("without an application ID or instanceIndex", func() { It("should set appropriate fields", func() { expectedStartEvent := &events.HttpStart{ RequestId: factories.NewUUID(requestId), PeerType: events.PeerType_Server.Enum(), Method: events.Method_GET.Enum(), Uri: proto.String("foo.example.com/"), RemoteAddress: proto.String("127.0.0.1"), UserAgent: proto.String("our-testing-client"), } startEvent := factories.NewHttpStart(req, events.PeerType_Server, requestId) Expect(startEvent.GetTimestamp()).ToNot(BeZero()) startEvent.Timestamp = nil Expect(startEvent).To(Equal(expectedStartEvent)) }) }) Context("with an application ID", func() { It("should include it in the start event", func() { applicationId, _ := uuid.NewV4() req.Header.Set("X-CF-ApplicationID", applicationId.String()) startEvent := factories.NewHttpStart(req, events.PeerType_Server, requestId)