forked from cloudfoundry/gorouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrumented_round_tripper.go
66 lines (54 loc) · 2.08 KB
/
instrumented_round_tripper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package dropsonde
import (
"github.com/cloudfoundry-incubator/dropsonde/emitter"
"github.com/cloudfoundry-incubator/dropsonde/events"
"github.com/cloudfoundry-incubator/dropsonde/factories"
uuid "github.com/nu7hatch/gouuid"
"log"
"net/http"
)
type instrumentedRoundTripper struct {
roundTripper http.RoundTripper
emitter emitter.EventEmitter
}
/*
Helper for creating an InstrumentedRoundTripper which will delegate to the given RoundTripper
*/
func InstrumentedRoundTripper(roundTripper http.RoundTripper, emitter emitter.EventEmitter) http.RoundTripper {
return &instrumentedRoundTripper{roundTripper, emitter}
}
/*
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
}